<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<p>In this tutorial we will write simple Hello World application using Spring Batch 2.0. This is an introductory tutorial to Spring Batch.</p>
<h2><b>Spring Batch Introduction-</b></h2>
<p>Many applications within the enterprise domain require bulk processing to perform business critical operations. These business operations include automated, complex processing of large volumes of information that is most efficiently processed without user intervention.</p>
<h2><b>Some Batch Terminologies-</b></h2>
<p>Before we look into the details of Spring Batch, lets look at some important terms that will be used in this article and anywhere in context of Spring Batch. You have to read the following post.<br />
<b>1. <a href="https://dineshonjava.com/configuring-and-running-job-in-spring/" target="_blank" rel="noopener">Configuring and running jobs</a></b><br />
<b>2. <a href="https://dineshonjava.com/configuring-step-in-spring-batch-2/" target="_blank" rel="noopener">Configuring Steps</a></b><br />
<b>3. <a href="https://dineshonjava.com/itemreaders-and-itemwriters-in-spring/" target="_blank" rel="noopener">ItemReader &; ItemWriter</a></b><br />
<b>4. <a href="https://dineshonjava.com/the-domain-language-of-batch/" target="_blank" rel="noopener">Some Domain Params</a></b><br />
<b>5. <a href="https://dineshonjava.com/spring-batch-glossary/" target="_blank" rel="noopener">Read Spring Batch Terms</a></b></p>
<p>Spring Batch is a lightweight batch framework which enables the development of a batch applications for the daily operations of enterprise systems.</p>
<div id="ads-id" align="center"></div>
<p>In this example, we will be explaining Spring Batch classes and interfaces using a simple Hello World example. We will create a job which consists of two steps. One prints “<b>Hello World!</b>” and second prints “<b>Created By Dinesh On Java</b>”. Aim is to keep the example simple and still understand the power of Spring Batch Framework.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/01/helloBatch.jpg" border="0" /></div>
<h2><b>Create Application Context XML-</b></h2>
<p>In order to run Spring Batch, we need to configuration Spring Batch Infrastructure. This includes creation of <i><b>JobRepository</b></i>, <b><i>JobLauncher </i></b>and <b><i>TransactionManager</i></b>.</p>
<p>For each job, we will use a separate xml context definition file. However there is a number of common objects that we will need recurrently. I will group them in an <i><b>applicationContext.xml </b></i>which will be imported from within job definitions. Let&#8217;s go through these common objects:<br />
<i><b>JobLauncher-</b></i></p>
<p><b><i>JobLaunchers </i></b>are responsible for starting a Job with a given job parameters. The provided implementation, <b><i>SimpleJobLauncher</i></b>, relies on a <b><i>TaskExecutor </i></b>to launch the jobs. If no specific <i><b>TaskExecutor </b></i>is set then a <i><b>SyncTaskExecutor </b></i>is used.<br />
<b><i>JobRepository-</i></b></p>
<p>We will use the <b><i>SimpleJobRepository </i></b>implementation which requires a set of execution <b><i>Daos </i></b>to store its information.<br />
<i><b>JobInstanceDao, JobExecutionDao, StepExecutionDao-</b></i></p>
<p>These data access objects are used by <i><b>SimpleJobRepository </b></i>to store execution related information. Two sets of implementations are provided by Spring Batch: Map based (in-memory) and Jdbc based. In a real application the Jdbc variants are more suitable but we will use the simpler in-memory alternative in this example.</p>
<p>Here&#8217;s our <i><b>applicationContext.xml:</b></i></p>
<pre class="highlight"><;beans xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation=" 
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">; 
 
 <;import resource="MEMORY-JOBREPOSITORY.xml"/>; 
 
<;/beans>; 
</pre>
<p>Here&#8217;s our <i><b>MEMORY-JOBREPOSITORY.xml</b></i>:</p>
<pre class="highlight"><;beans xmlns:batch="http://www.springframework.org/schema/batch" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation=" 
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">; 
 
<;bean class="org.springframework.batch.core.repository.support.SimpleJobRepository" id="jobRepository">; 
 <;constructor-arg>;<;bean class="org.springframework.batch.core.repository.dao.MapJobInstanceDao"/>; 
 <;/constructor-arg>;<;constructor-arg>;bean class="org.springframework.batch.core.repository.dao.MapJobExecutionDao" />; 
 <;/constructor-arg>;<;constructor-arg>;<;bean class="org.springframework.batch.core.repository.dao.MapStepExecutionDao"/>; 
 <;/constructor-arg>;<;constructor-arg>;<;bean class="org.springframework.batch.core.repository.dao.MapExecutionContextDao"/>; 
 <;/constructor-arg>;<;/bean>; <;bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager"/>; <;bean class="org.springframework.batch.core.launch.support.SimpleJobLauncher" id="jobLauncher">; <;property name="jobRepository" ref="jobRepository"/>; <;/bean>; <;/beans>;</pre>
<p> ;</p>
<h3><b>Create HelloWorldTasklet.java class-</b></h3>
<p>A tasklet is a class containing custom logic to be run as a part of a job. <b><i>HelloWorldTasklet </i></b>is our custom tasklet which implements <i><b>Tasklet </b></i>interface and overrides the execute() method. It is a simple tasklet that simply prints a message.</p>
<p>Note that the execute method returns <i><b>RepeatStatus </b></i>to indicate execution status of tasklet.<br />
<i><b>RepeatStatus.FINISHED</b></i> indicates that processing is finished (either successful or unsuccessful)<br />
<i><b>RepeatStatus.CONTINUABLE </b></i>indicates that processing can continue.</p>
<pre class="highlight">package com.dineshonjava.batch.hello; 
 
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; 
 
/** 
 * 
 * @author Dinesh Rajput 
 * 
 */ 
public class HelloWorldTasklet implements Tasklet { 
 
 private String message; 
 
 public void setMessage(String message) { 
 this.message = message; 
 } 
 
 @Override 
 public RepeatStatus execute(StepContribution contribution, ChunkContext context) 
 throws Exception { 
 System.out.println(message); 
 return RepeatStatus.FINISHED; 
 } 
 
} 
</pre>
<h3><b>Create Job Configuration XML-</b></h3>
<p>In this section we will see configuration to fit the custom tasklet into the Spring Batch framework.</p>
<pre class="highlight"><;beans xmlns:batch="http://www.springframework.org/schema/batch" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans" xsi:schemalocation=" 
 http://www.springframework.org/schema/beans 
 http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
 http://www.springframework.org/schema/context 
 http://www.springframework.org/schema/context/spring-context-3.0.xsd 
 http://www.springframework.org/schema/mvc 
 http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">; 
 
 <;import resource="applicationContext.xml"/>; 
 
 <;bean abstract="true" class="org.springframework.batch.core.step.tasklet.TaskletStep" id="taskletStep">; 
 <;property name="jobRepository" ref="jobRepository"/>; 
 <;property name="transactionManager" ref="transactionManager"/>; 
 <;/bean>; 
 
 <;bean class="com.dineshonjava.batch.hello.HelloWorldTasklet" id="helloTasklet">; 
 <;property name="message" value="Hello World!"/>; 
 <;/bean>; 
 
 <;bean class="com.dineshonjava.batch.hello.HelloWorldTasklet" id="createdByTasklet">; 
 <;property name="message" value="Created By Dinesh On Java"/>; 
 <;/bean>; 
 
 
 <;bean class="org.springframework.batch.core.job.SimpleJob" id="mySimpleJob">; 
 <;property name="name" value="mySimpleJob" />; 
 <;property name="steps">; 
 <;list>; 
 <;bean parent="taskletStep">; 
 <;property name="tasklet" ref="helloTasklet"/>; 
 <;/bean>; 
 
 <;bean parent="taskletStep">; 
 <;property name="tasklet" ref="createdByTasklet"/>; 
 <;/bean>; 
 <;/list>; 
 <;/property>; 
 <;property name="jobRepository" ref="jobRepository">; 
 <;/bean>; 
<;/beans>; 
</pre>
<p>First we created an abstract bean (&#8220;<b><i>taskletStep</i></b>&#8220;) using class <i><b>TaskletStep</b></i>. We injected job repository and transaction manager into this tasklet. Later we created two instance of our <b><i>HelloWorldTasklet </i></b>class, one of which takes &#8220;<b>Hello World!</b>&#8221; as message and another takes &#8220;<b>Created By Dinesh On Java</b>&#8221; as message.<br />
Finally we defined our batch job (&#8220;<b><i>mySimpleJob</i></b>&#8220;) consisting of two steps using class <i><b>SimpleJob</b></i>. Property &#8220;steps&#8221; takes list of tasklet steps which will be executed sequentially one after another.<br />
As the two tasklet instances which we created earlier represent a step in our batch application, they use taskletStep abstract bean as parent.</p>
<h3><b>Running the Job-</b></h3>
<p>Now we need something to kick-start the execution of our jobs. Spring Batch provides a convenient class to achieve that from the command line: <i><b>CommandLineJobRunner</b></i>. In its simplest form this class takes 2 arguments:<br />
1. the xml application context containing the job to launch(<b><i>simpleJob.xml</i></b>) and<br />
2. the bean id of that job(<i><b>mySimpleJob</b></i>).<br />
It naturally requires a <i><b>JobLauncher </b></i>to be configured in the application context.</p>
<p>First right click to project &#8220;<i><b>springBatch2HelloWorld</b></i>&#8221; and click on <i><b>Run As </b></i>and go to <i><b>Run Configurations&#8230; </b></i>as follows&#8230;</p>
<div class="separator" style="clear: both; text-align: center;"><img class="aligncenter" src="https://dineshonjava.com/wp-content/uploads/2013/01/firststep1.jpg" width="640" height="533" border="0" /></div>
<p>After clicking on the <b><i>Run Configurations&#8230;</i></b> we get the following window and put the information as on <i><b>Project </b></i>field with value &#8220;<b><i>springBatch2HelloWorld</i></b>&#8221; and <i><b>Main Class</b></i> field with value &#8220;<b><i>org.springframework.batch.core.launch.support.CommandLineJobRunner</i></b>&#8221;</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/01/secondStep.jpg" width="640" height="420" border="0" /></div>
<p>Now switch to Arguments tabs on the above window we get the following window and put the value on the <b>Program arguments</b> as &#8220;<b>config/simpleJob.xml mySimpleJob</b>&#8220;. and click on the <i><b>Run </b></i>button.</p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2013/01/thirdStep.jpg" width="640" height="480" border="0" /></div>
<p>After clicking Run button if every thing is Ok then we will get the following output on the console.</p>
<div style="background-color: #ff99cc; border-width: thin; border: solid;"><b>OUTPUT:</b><br />
Jan 15, 2013 12:00:27 AM org.springframework.context.support.AbstractApplicationContext prepareRefresh<br />
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@1100d7a: startup date [Tue Jan 15 00:00:27 IST 2013]; root of context hierarchy<br />
Jan 15, 2013 12:00:27 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions<br />
INFO: Loading XML bean definitions from class path resource [config/simpleJob.xml]<br />
Jan 15, 2013 12:00:27 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions<br />
INFO: Loading XML bean definitions from class path resource [config/applicationContext.xml]<br />
Jan 15, 2013 12:00:27 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions<br />
INFO: Loading XML bean definitions from class path resource [config/MEMORY-JOBREPOSITORY.xml]<br />
Jan 15, 2013 12:00:27 AM org.springframework.beans.factory.support.DefaultListableBeanFactory preInstantiateSingletons<br />
INFO: Pre-instantiating singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d85f79: defining beans [jobRepository,transactionManager,jobLauncher,taskletStep,helloTasklet,createdByTasklet,mySimpleJob]; root of factory hierarchy<br />
Jan 15, 2013 12:00:27 AM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet<br />
INFO: No TaskExecutor has been set, defaulting to synchronous executor.<br />
Jan 15, 2013 12:00:27 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run<br />
INFO: Job: [SimpleJob: [name=mySimpleJob]] launched with the following parameters: [{}]<br />
Jan 15, 2013 12:00:27 AM org.springframework.batch.core.job.AbstractJob handleStep<br />
INFO: Executing step: [TaskletStep: [name=taskletStep$child#1ea5671]]<br />
Hello World!<br />
Jan 15, 2013 12:00:27 AM org.springframework.batch.core.job.AbstractJob handleStep<br />
INFO: Executing step: [TaskletStep: [name=taskletStep$child#1d15445]]<br />
Created By Dinesh On Java<br />
Jan 15, 2013 12:00:27 AM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run<br />
INFO: Job: [SimpleJob: [name=mySimpleJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]<br />
Jan 15, 2013 12:00:27 AM org.springframework.context.support.AbstractApplicationContext doClose<br />
INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@1100d7a: startup date [Tue Jan 15 00:00:27 IST 2013]; root of context hierarchy<br />
Jan 15, 2013 12:00:27 AM org.springframework.beans.factory.support.DefaultSingletonBeanRegistry destroySingletons<br />
INFO: Destroying singletons in org.springframework.beans.factory.support.DefaultListableBeanFactory@1d85f79: defining beans [jobRepository,transactionManager,jobLauncher,taskletStep,helloTasklet,createdByTasklet,mySimpleJob]; root of factory hierarchy</div>
<h3 style="text-align: center;"><u><b>Download SourceCode+Libs</b></u></h3>
<div style="text-align: center;"><a href="https://sites.google.com/site/dinesh9582486434/my-forms/springBatch2HelloWorld.zip?attredirects=0&;d=1" target="_blank" rel="noopener"><u><b>springBatch2HelloWorld.zip</b></u></a></div>
</div>
<p> ;</p>
<div style="text-align: center;"><b><;<;<a href="https://dineshonjava.com/repeattemplate-in-spring-batch-20/">Previuos</a><;<; <a href="https://dineshonjava.com/spring-tutorial/">Index </a>>;>; <a href="https://dineshonjava.com/spring-batch-itemreader-itemwriter/">Next</a>>;>;</b></div>
<p> ;</p>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/repeattemplate-in-spring-batch-20/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/integration-spring-mvc3-and-mongodb/">Next</a> 
									 </div> 
									</div>
<script type="text/javascript">
jQuery(document).ready(function($) {
 $.post('https://dineshonjava.com/wp-admin/admin-ajax.php', {action: 'mts_view_count', id: '603'});
});
</script>
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…