How to Use Spring Batch Processing In Our Java Applications
Posted By : Ravindra Singh | 25-Nov-2018
Introduction:
Spring Batch is a Beautiful concept for processing billions of transactions every day for enterprice Appications.Spring Batch is a lightweight framework for executing theseries of jobs. Batch processing is a mode which includes executing the series of automated jobs which are complex without client interaction. In basic words, a Batch procedure handles vast volume information and runs it for quite a while.
Let me Explore More Features of Spring Framework
Numerous venture applications need to process high volume information to play out a few capacities, for example,
• Periodic time-based counts.
• Applications that are occasional and are prepared drearily over extensive datasets.
• Applications which requires preparing and approving the information accessible.
Spring Batch is a lightweight system utilized for creating clump applications that are utilized in an endeavor application. It gives an exceptionally compelling structure to preparing extensive volume bunch occupations.
There are different other tasks of Spring Batch with bulk processing are:
• Transaction management.
• Processing statistics.
• Logging and tracing.
• Job restart.
• Resource management.
The Spring Batch component architecture points are explained as follow:
Application: the application contains all jobs and codes for Spring.
Batch core: It has every one of the API classes required for controlling and propelling Batch.
Batch infrastructure: It has perusers and authors alongside administrations utilized by both above parts.
Features of spring Batch are :
a. Flexibility
The Spring Batch application is very flexible. updates is only needed in an XML file to alter the order of processing an application.
b. Scalability
The Scalibility in Spring Batch application is very easy using the portioning techniques. This techniques allow you to execute job in parallel for each thread step by step.
c. Maintainability
A Spring batch job has can be tested without affecting the other steps.
d. Reliability
The Reliability of Spring Batch applications are very smooth as you can restart any job from the point where the application failed. It is possible just because of decoupling the steps.
e. Multiple ways of launching the job
Spring Batch processing have the ablility that you can launching Spring Batch job using web applications or Java programs or even command line.
Setting up the Environment
Before we jump for the Spring Batch application example first you need to know about the setting up of an environment. Below are the steps required for setting the environment for Spring Batch:
1. First you should Install Eclipse on your machine and open new project.
2. Create a sample Spring Batch project with maven.
3. After creating maven project it will show you Pom.xml. There you have to mention all the required dependency.
In the pom.xml copy paste the below required dependencies :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
|
<project xmlns = "http://maven.apache.org/POM/4.0.0"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://maven.apache.org/POM/4.0.0
http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>example</groupId>
<artifactId>SpringBatchSample</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringBatchExample</name>
<url>http://maven.apache.org</url>
<properties>
<jdk.version>1.8</jdk.version>
<spring.version>4.3.8.RELEASE</spring.version>
<spring.batch.version>3.0.7.RELEASE</spring.batch.version>
<mysql.driver.version>5.1.25</mysql.driver.version>
<junit.version>4.11</junit.version>
</properties>
<dependencies>
<!-- Spring Core -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring jdbc, for database -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- Spring XML to/back object -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-oxm</artifactId>
<version>${spring.version}</version>
</dependency>
<!-- MySQL database driver -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>${mysql.driver.version}</version>
</dependency>
<!-- Spring Batch dependencies -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-core</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-infrastructure</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<!-- Spring Batch unit test -->
<dependency>
<groupId>org.springframework.batch</groupId>
<artifactId>spring-batch-test</artifactId>
<version>${spring.batch.version}</version>
</dependency>
<!-- Junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>spring-batch</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>2.9</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>false</downloadJavadocs>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>${jdk.version}</source>
<target>${jdk.version}</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
|
Spring Batch Example
Now after setting up environment setup for Spring batch, I will show you one example of spring Batch, Below is the following Spring Batch files that it contains :
• Configuration file: configuration file is an XML file where in all jobs and steps of a job would be mentioned.
• Context.xml: In the context file it will contains beans like repository, launcher and transaction manager.
• Tasklet class: Tasklet is an interface That will be called for perform single operation called clean and setup resaurces. In the tasklet class we will write the code for processing job.
• Launcher class: If we talk about launcher class, In this class you will be able to trigger launching the Job launcher for launch Batch Application.
jobConfig.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:batch = "http://www.springframework.org/schema/batch"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.2.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd ">
<import resource="context.xml" />
<!-- Defining a bean -->
<bean id = "tasklet" class = "a_sample.MyTasklet" />
<!-- Defining a job-->
<batch:job id = "helloWorldJob">
<!-- Defining a Step -->
<batch:step id = "step1">
<tasklet ref = "tasklet"/>
</batch:step>
</batch:job>
</beans>
|
Context.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
<beans xmlns = "http://www.springframework.org/schema/beans"
xmlns:xsi = http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.2.xsd">
<bean id = "jobRepository"
class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name = "transactionManager" ref = "transactionManager" />
</bean>
<bean id = "transactionManager"
class = "org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<bean id = "jobLauncher"
class = "org.springframework.batch.core.launch.support.SimpleJobLauncher">
<property name = "jobRepository" ref = "jobRepository" />
</bean>
</beans>
|
Tasklet.java
1
2
3
4
5
6
7
8
9
10
11
12
|
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class MyTasklet implements Tasklet {
@Override
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception {
System.out.println("Hello This is a sample example of spring batch");
return RepeatStatus.FINISHED;
}
}
|
App.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class App {
public static void main(String[] args)throws Exception {
// System.out.println("hello");
String[] springConfig = {"a_sample/job_hello_world.xml"};
// Creating the application context object
ApplicationContext context = new ClassPathXmlApplicationContext(springConfig);
// Creating the job launcher
JobLauncher jobLauncher = (JobLauncher) context.getBean("jobLauncher");
// Creating the job
Job job = (Job) context.getBean("helloWorldJob");
// Executing the JOB
JobExecution execution = jobLauncher.run(job, new JobParameters());
System.out.println("Exit Status : " + execution.getStatus());
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
The following output you will achieve:
org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFORMATION:Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@2ef1e4fa: startup date; root of context hierarchy
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions INFO: Loading XML bean definitions
org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFORMATION: Loading XML bean definitions
org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons
org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFORMATION: No TaskExecutor has been set, defaulting to a synchronous executor.
org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFORMATION: Job: [FlowJob: [name=helloWorldJob]] launched with the following parameters: [{}]
org.springframework.batch.core.job.SimpleStepHandler handleStep INFO: Executing step: [step1]
Hello This is a simple example of spring batch
org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run
INFORMATION: Job: [FlowJob: [name=helloWorldJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]
Exit Status: COMPLETED
|
Conclusion
Hence, in this blog, you understand the concept of Spring batch and the need of using batch processing. Also, I have described the features of Spring batch and its architectural components and its environment setup in detail.
Thanks,
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Ravindra Singh
Ravindra is Sr. Associate Consultant Development- Java (Backend Developer). And Familiar with AWS Cloud Machine Learning Programming (AWS Lex, Lambda, Polly, Elasticsearch ), And also having good experience in Spring Boot Microservice Architecture Applica