Spring Boot – Batch Service

Last updated on May 30 2022
Amarjit Malik

Spring Boot – Batch Service

You can create an executable JAR file, and run the Spring Boot application by using the Maven or Gradle commands as shown below −

For Maven, you can use the command given below −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

For Gradle, you can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

Run the JAR file by using the command given here −

java –jar <JARFILE>

Now, the application has started on the Tomcat port 8080 as shown.

Page 1 Image 1 3
appstart

Now, hit the URL http://localhost:8080/ in your web browser and connect the web socket and send the greeting and receive the message.

Page 1 Image 2 1
recmessage

Batch Service is a process to execute more than one command in a single task. In this chapter, you are going to learn how to create batch service in a Spring Boot application.

Let us consider an example where we are going to save the CSV file content into HSQLDB.

To create a Batch Service program, we need to add the Spring Boot Starter Batch dependency and HSQLDB dependency in our build configuration file.

Maven users can add the following dependencies in pom.xml file.

<dependency>   <groupId>org.springframework.boot</groupId>   <artifactId>spring-boot-starter-batch</artifactId></dependency><dependency>   <groupId>org.hsqldb</groupId>   <artifactId>hsqldb</artifactId></dependency>

Gradle users can add the following dependencies in build.gradle file.

compile(“org.springframework.boot:spring-boot-starter-batch”)compile(“org.hsqldb:hsqldb”)

Now, add the simple CSV data file under classpath resources – src/main/resources and name the file as file.csv as shown −

William,JohnMike, SebastianLawarance, Lime

Next, write a SQL script for HSQLDB – under the classpath resource directory – request_fail_hystrix_timeout

DROP TABLE USERS IF EXISTS;CREATE TABLE USERS  (   user_id BIGINT IDENTITY NOT NULL PRIMARY KEY,   first_name VARCHAR(20),   last_name VARCHAR(20));

Create a POJO class for USERS model as shown −

package com.tecklearn.batchservicedemo;public class User {   private String lastName;   private String firstName;    public User() {   }   public User(String firstName, String lastName) {      this.firstName = firstName;      this.lastName = lastName;   }   public void setFirstName(String firstName) {      this.firstName = firstName;   }   public String getFirstName() {      return firstName;   }   public String getLastName() {      return lastName;   }   public void setLastName(String lastName) {      this.lastName = lastName;   }    @Override   public String toString() {      return “firstName: ” + firstName + “, lastName: ” + lastName;   }   }

Now, create an intermediate processor to do the operations after the reading the data from the CSV file and before writing the data into SQL.

package com.tecklearn.batchservicedemo; import org.slf4j.Logger;import org.slf4j.LoggerFactory;import org.springframework.batch.item.ItemProcessor; public class UserItemProcessor implements ItemProcessor<User, User> {   private static final Logger log = LoggerFactory.getLogger(UserItemProcessor.class);    @Override   public User process(final User user) throws Exception {      final String firstName = user.getFirstName().toUpperCase();      final String lastName = user.getLastName().toUpperCase();      final User transformedPerson = new User(firstName, lastName);       log.info(“Converting (” + user + “) into (” + transformedPerson + “)”);      return transformedPerson;   }}

Let us create a Batch configuration file, to read the data from CSV and write into the SQL file as shown below. We need to add the @EnableBatchProcessing annotation in the configuration class file. The @EnableBatchProcessing annotation is used to enable the batch operations for your Spring Boot application.

package com.tecklearn.batchservicedemo; import javax.sql.DataSource;import org.springframework.batch.core.Job;import org.springframework.batch.core.Step; import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;import org.springframework.batch.core.launch.support.RunIdIncrementer;import org.springframework.batch.item.database.BeanPropertyItemSqlParameterSourceProvider;import org.springframework.batch.item.database.JdbcBatchItemWriter;import org.springframework.batch.item.file.FlatFileItemReader;import org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper;import org.springframework.batch.item.file.mapping.DefaultLineMapper;import org.springframework.batch.item.file.transform.DelimitedLineTokenizer;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.context.annotation.Bean;import org.springframework.context.annotation.Configuration;import org.springframework.core.io.ClassPathResource; @Configuration@EnableBatchProcessingpublic class BatchConfiguration {   @Autowired   public JobBuilderFactory jobBuilderFactory;    @Autowired   public StepBuilderFactory stepBuilderFactory;    @Autowired   public DataSource dataSource;    @Bean   public FlatFileItemReader<User> reader() {      FlatFileItemReader<User> reader = new FlatFileItemReader<User>();      reader.setResource(new ClassPathResource(“file.csv”));      reader.setLineMapper(new DefaultLineMapper<User>() {         {            setLineTokenizer(new DelimitedLineTokenizer() {               {                  setNames(new String[] { “firstName”, “lastName” });               }            });            setFieldSetMapper(new BeanWrapperFieldSetMapper<User>() {               {                  setTargetType(User.class);               }            });         }      });      return reader;   }   @Bean   public UserItemProcessor processor() {      return new UserItemProcessor();   }   @Bean   public JdbcBatchItemWriter<User> writer() {      JdbcBatchItemWriter<User> writer = new JdbcBatchItemWriter<User>();      writer.setItemSqlParameterSourceProvider(new BeanPropertyItemSqlParameterSourceProvider<User>());      writer.setSql(“INSERT INTO USERS (first_name, last_name) VALUES (:firstName, :lastName)”);      writer.setDataSource(dataSource);      return writer;   }   @Bean   public Job importUserJob(JobCompletionNotificationListener listener) {      return jobBuilderFactory.get(“importUserJob”).incrementer(         new RunIdIncrementer()).listener(listener).flow(step1()).end().build();   }   @Bean   public Step step1() {      return stepBuilderFactory.get(“step1”).<User, User>chunk(10).reader(reader()).processor(processor()).writer(writer()).build();   }}

The reader() method is used to read the data from the CSV file and writer() method is used to write a data into the SQL.

Next, we will have to write a Job Completion Notification Listener class – used to notify after the Job completion.

package com.tecklearn.batchservicedemo; import java.sql.ResultSet;import java.sql.SQLException;import java.util.List; import org.slf4j.Logger;import org.slf4j.LoggerFactory; import org.springframework.batch.core.BatchStatus;import org.springframework.batch.core.JobExecution;import org.springframework.batch.core.listener.JobExecutionListenerSupport;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.jdbc.core.JdbcTemplate;import org.springframework.jdbc.core.RowMapper;import org.springframework.stereotype.Component; @Componentpublic class JobCompletionNotificationListener extends JobExecutionListenerSupport {   private static final Logger log = LoggerFactory.getLogger(JobCompletionNotificationListener.class);   private final JdbcTemplate jdbcTemplate;    @Autowired   public JobCompletionNotificationListener(JdbcTemplate jdbcTemplate) {      this.jdbcTemplate = jdbcTemplate;   }   @Override   public void afterJob(JobExecution jobExecution) {      if (jobExecution.getStatus() == BatchStatus.COMPLETED) {         log.info(“!!! JOB FINISHED !! It’s time to verify the results!!”);          List<User> results = jdbcTemplate.query(            “SELECT first_name, last_name FROM USERS”, new RowMapper<User>() {                        @Override            public User mapRow(ResultSet rs, int row) throws SQLException {               return new User(rs.getString(1), rs.getString(2));            }         });          for (User person : results) {            log.info(“Found <” + person + “> in the database.”);         }      }   }}

Now, create an executable JAR file, and run the Spring Boot application by using the following Maven or Gradle commands.

For Maven, use the command as shown −

mvn clean install

After “BUILD SUCCESS”, you can find the JAR file under the target directory.

For Gradle, you can use the command as shown −

gradle clean build

After “BUILD SUCCESSFUL”, you can find the JAR file under the build/libs directory.

Run the JAR file by using the command given here −

java –jar <JARFILE>

You can see the output in console window as shown −

Page 7 Image 3 1
output

So, this brings us to the end of blog. This Tecklearn ‘Spring Boot – Batch Service’ blog helps you with commonly asked questions if you are looking out for a job in Java Programming. If you wish to learn Spring Boot and build a career Java Programming domain, then check out our interactive, Java and JEE Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/java-and-jee-training/

Java and JEE Training

About the Course

Java and JEE Certification Training is designed by professionals as per the industrial requirements and demands. This training encompasses comprehensive knowledge on basic and advanced concepts of core Java & J2EE along with popular frameworks like Hibernate, Spring & SOA. In this course, you will gain expertise in concepts like Java Array, Java OOPs, Java Function, Java Loops, Java Collections, Java Thread, Java Servlet, and Web Services using industry use-cases and this will help you to become a certified Java expert.

Why Should you take Java and JEE Training?

  • Java developers are in great demand in the job market. With average pay going between $90,000/- to $120,000/- depending on your experience and the employers.
  • Used by more than 10 Million developers worldwide to develop applications for 15 Billion devices.
  • Java is one of the most popular programming languages in the software world. Rated #1 in TIOBE Popular programming languages index (15th Consecutive Year)

What you will Learn in this Course?

Introduction to Java

  • Java Fundamentals
  • Introduction to Java Basics
  • Features of Java
  • Various components of Java language
  • Benefits of Java over other programming languages
  • Key Benefits of Java

Installation and IDE’s for Java Programming Language

  • Installation of Java
  • Setting up of Eclipse IDE
  • Components of Java Program
  • Editors and IDEs used for Java Programming
  • Writing a Simple Java Program

Data Handling and Functions

  • Data types, Operations, Compilation process, Class files, Loops, Conditions
  • Using Loop Constructs
  • Arrays- Single Dimensional and Multi-Dimensional
  • Functions
  • Functions with Arguments

OOPS in Java: Concept of Object Orientation

  • Object Oriented Programming in Java
  • Implement classes and objects in Java
  • Create Class Constructors
  • Overload Constructors
  • Inheritance
  • Inherit Classes and create sub-classes
  • Implement abstract classes and methods
  • Use static keyword
  • Implement Interfaces and use it

Polymorphism, Packages and String Handling

  • Concept of Static and Run time Polymorphism
  • Function Overloading
  • String Handling –String Class
  • Java Packages

Exception Handling and Multi-Threading

  • Exception handling
  • Various Types of Exception Handling
  • Introduction to multi-threading in Java
  • Extending the thread class
  • Synchronizing the thread

File Handling in Java

  • Input Output Streams
  • Java.io Package
  • File Handling in Java

Java Collections

  • Wrapper Classes and Inner Classes: Integer, Character, Boolean, Float etc
  • Applet Programs: How to write UI programs with Applet, Java.lang, Java.io, Java.util
  • Collections: ArrayList, Vector, HashSet, TreeSet, HashMap, HashTable

Java Database Connectivity (JDBC)

  • Introduction to SQL: Connect, Insert, Update, Delete, Select
  • Introduction to JDBC and Architecture of JDBC
  • Insert/Update/Delete/Select Operations using JDBC
  • Batch Processing Transaction
  • Management: Commit and Rollback

Java Enterprise Edition – Servlets

  • Introduction to J2EE
  • Client Server architecture
  • URL, Port Number, Request, Response
  • Need for servlets
  • Servlet fundamentals
  • Setting up a web project in Eclipse
  • Configuring and running the web app with servlets
  •  GET and POST request in web application with demo
  • Servlet lifecycle
  • Servlets Continued
  • Session tracking and filter
  • Forward and include Servlet request dispatchers

Java Server Pages (JSP)

  • Fundamentals of Java Server Page
  • Writing a code using JSP
  • The architecture of JSP
  • JSP Continued
  • JSP elements: Scriptlets, expressions, declaration
  • JSP standard actions
  • JSP directives
  • Introduction to JavaBeans
  • ServletConfig and ServletContext
  • Servlet Chaining
  • Cookies Management
  • Session Management

Hibernate

  • Introduction to Hibernate
  • Introduction to ORM
  • ORM features
  • Hibernate as an ORM framework
  • Hibernate features
  • Setting up a project with Hibernate framework
  • Basic APIs needed to do CRUD operations with Hibernate
  • Hibernate Architecture

POJO (Plain Old Java Object)

  • POJO (Plain Old Java Object)
  • Persistent Objects
  • Lifecycle of Persistent Object

Spring

  • Introduction to Spring
  • Spring Fundamentals
  • Advanced Spring

Got a question for us? Please mention it in the comments section and we will get back to you.

 

 

 

 

0 responses on "Spring Boot - Batch Service"

Leave a Message

Your email address will not be published. Required fields are marked *