<div dir="ltr" style="text-align: justify;">
<div dir="ltr" style="text-align: justify;">
<h2><i>Why do we need scheduling?</i></h2>
<p>Scheduling is needed if you want to automate the repetition of a task at specific intervals or particular date. You could of course manually watch the time and execute your task, albeit an inefficient task. Who wants to watch the computer every 10 seconds just to hit the Enter key? No one.</p>
<p><span style="text-align: left;">In this example we use a simple configuration and an example implementation of ItemReader and ItemWriter, as you can see below. The Job is defined to run every 10 seconds. While everything works well in the first iteration, reading and writing seems to be skipped in the subsequent iterations.</span></p>
<div dir="ltr" style="text-align: left;">
<pre class="highlight"><;batch:job id="simpleDojJob" parent="simpleJob">; 
 <;batch:step id="step1">; 
 <;batch:tasklet>; 
 <;batch:chunk reader="xmlItemReader" writer="mongodbItemWriter" commit-interval="2" processor="employeeFilter"/>; 
 <;/batch:tasklet>; 
 <;/batch:step>; 
 <;/batch:job>; 
<;bean id="runScheduler" class="com.doj.batch.scheduler.RunScheduler" />; 
 
 <;!-- Run every 10 seconds -->; 
 <;task:scheduled-tasks>; 
 <;task:scheduled ref="runScheduler" method="run" cron="*/10 * * * * *" />; 
 <;/task:scheduled-tasks>; 
</pre>
<div id="ads-id" align="center"></div>
<p>In this tutorial, we will show you how to use Spring <b><i>TaskScheduler </i></b>to schedule a batch job to run every 5 seconds.</p>
<h3><b>Tools and libraries used</b></h3>
<ul style="text-align: left;">
<li>Spring Tool Suite (STS)</li>
<li>JDK 1.6</li>
<li>Spring Core 3.2.2.RELEASE</li>
<li>Spring OXM 3.2.2.RELEASE</li>
<li>Spring Batch 2.2.0.RELEASE</li>
<li>MongoDB Java Driver 2.7.3</li>
<li>MongoDB 2.10.1</li>
</ul>
<h3><b>1. Project Directory Structure</b></h3>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/03/111.png" border="0" /></div>
<h3><b>2. Spring <i>TaskScheduler</i></b></h3>
<p>Spring 3.0 introduces a TaskScheduler for scheduling tasks. It is part of the Spring-Core, no need to declare an extra dependency. A scheduler triggers launch Spring Batch jobs, by accessing the Spring Batch runtime.</p>
<pre class="highlight"><;!-- Run every 10 seconds -->; 
 <;task:scheduled-tasks>; 
 <;task:scheduled ref="runScheduler" method="run" cron="*/10 * * * * *" />; 
 <;/task:scheduled-tasks>; 
 <;task:scheduled-tasks>; 
 <;task:scheduled ref="runScheduler" method="run" fixed-delay="10000" />; 
 <;/task:scheduled-tasks>; 
</pre>
<p>The <i><b>TaskScheduler </b></i>will schedule to run below bean.</p>
<pre class="highlight">package com.doj.batch.scheduler; 
 
import java.util.Date; 
 
import org.springframework.batch.core.Job; 
import org.springframework.batch.core.JobExecution; 
import org.springframework.batch.core.JobParameters; 
import org.springframework.batch.core.JobParametersBuilder; 
import org.springframework.batch.core.launch.JobLauncher; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@Component 
public class RunScheduler { 
 @Autowired 
 private JobLauncher jobLauncher; 
 
 @Autowired 
 private Job job; 
 
 public void run() { 
 try { 
 String dateParam = new Date().toString(); 
 JobParameters param = new JobParametersBuilder().addString("date", dateParam).toJobParameters(); 
 System.out.println(dateParam); 
 JobExecution execution = jobLauncher.run(job, param); 
 System.out.println("Exit Status : " + execution.getStatus()); 
 } catch (Exception e) { 
 //e.printStackTrace(); 
 } 
 } 
} 
</pre>
<h3><b>Input XML File</b><br />
<b>employees.xml</b></h3>
<pre class="highlight">s<;?xml version="1.0" encoding="UTF-8"?>; 
<;employees>; 
 <;employee>; 
 <;address>;delhi<;/address>; 
 <;age>;37<;/age>; 
 <;empid>;1111<;/empid>; 
 <;name>;ATUL KUMAR<;/name>; 
 <;salary>;300000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;delhi<;/address>; 
 <;age>;27<;/age>; 
 <;empid>;2222<;/empid>; 
 <;name>;Dinesh Rajput<;/name>; 
 <;salary>;60000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;delhi<;/address>; 
 <;age>;31<;/age>; 
 <;empid>;3333<;/empid>; 
 <;name>;ASHUTOSH RAJPUT<;/name>; 
 <;salary>;400000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;Kanpur<;/address>; 
 <;age>;27<;/age>; 
 <;empid>;4444<;/empid>; 
 <;name>;Adesh Verma<;/name>; 
 <;salary>;80000.0<;/salary>; 
 <;/employee>; 
 <;employee>; 
 <;address>;Noida<;/address>; 
 <;age>;37<;/age>; 
 <;empid>;5555<;/empid>; 
 <;name>;Dinesh Rajput<;/name>; 
 <;salary>;300000.0<;/salary>; 
 <;/employee>; 
<;/employees>; 
</pre>
<p><b>Employee.java</b></p>
<pre class="highlight">package com.doj.batch.bean; 
 
import javax.xml.bind.annotation.XmlRootElement; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
@XmlRootElement(name="employee") 
public class Employee { 
 private int empid; 
 private String name; 
 private int age; 
 private float salary; 
 private String address; 
 /** 
 * @return the empid 
 */ 
 public int getEmpid() { 
 return empid; 
 } 
 /** 
 * @param empid the empid to set 
 */ 
 public void setEmpid(int empid) { 
 this.empid = empid; 
 } 
 /** 
 * @return the name 
 */ 
 public String getName() { 
 return name; 
 } 
 /** 
 * @param name the name to set 
 */ 
 public void setName(String name) { 
 this.name = name; 
 } 
 /** 
 * @return the age 
 */ 
 public int getAge() { 
 return age; 
 } 
 /** 
 * @param age the age to set 
 */ 
 public void setAge(int age) { 
 this.age = age; 
 } 
 /** 
 * @return the salary 
 */ 
 public float getSalary() { 
 return salary; 
 } 
 /** 
 * @param salary the salary to set 
 */ 
 public void setSalary(float salary) { 
 this.salary = salary; 
 } 
 /** 
 * @return the address 
 */ 
 public String getAddress() { 
 return address; 
 } 
 /** 
 * @param address the address to set 
 */ 
 public void setAddress(String address) { 
 this.address = address; 
 } 
 
 public String toString(){ 
 return "Name- "+name+" Age- "+age+" salary- "+salary+" address "+address; 
 } 
 
} 
</pre>
<h3><strong>4. Job configuration</strong><br />
simple-job.xml</h3>
<pre class="highlight"><;?xml version="1.0" encoding="UTF-8"?>; 
<;beans xmlns="http://www.springframework.org/schema/beans" 
 xmlns:context="http://www.springframework.org/schema/context" 
 xmlns:p="http://www.springframework.org/schema/p" 
 xmlns:task="http://www.springframework.org/schema/task" 
 xmlns:batch="http://www.springframework.org/schema/batch" 
 xmlns:mvc="http://www.springframework.org/schema/mvc" 
 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-4.0.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context-4.0.xsd 
http://www.springframework.org/schema/mvc 
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd 
http://www.springframework.org/schema/batch 
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd 
http://www.springframework.org/schema/task 
http://www.springframework.org/schema/task/spring-task-3.2.xsd">; 
 <;context:component-scan base-package="com.doj.batch" />; 
 <;import resource="applicationContext.xml"/>; 
 <;import resource="mongodbConfig.xml"/>; 
 
 <;bean id="xmlItemReader" class="org.springframework.batch.item.xml.StaxEventItemReader">; 
 <;property name="resource" value="classpath:xml/employees.xml" />; 
 <;property name="unmarshaller" ref="empUnMarshaller" />; 
 <;property name="fragmentRootElementName" value="employee" />; 
 <;/bean>; 
 <;bean id="empUnMarshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller">; 
 <;property name="classesToBeBound">; 
 <;value>;com.doj.batch.bean.Employee<;/value>; 
 <;/property>; 
 <;/bean>; 
 <;!-- write it to MongoDB, 'employee' collection (table) -->; 
 <;bean id="mongodbItemWriter" class="org.springframework.batch.item.data.MongoItemWriter">; 
 <;property name="template" ref="mongoTemplate" />; 
 <;property name="collection" value="employee" />; 
 <;/bean>; 
 
 <;bean id="employeeFilter" class="com.doj.batch.process.EmployeeFilter">;<;/bean>; 
 
 <;batch:job id="simpleDojJob" parent="simpleJob">; 
 <;batch:step id="step1">; 
 <;batch:tasklet>; 
 <;batch:chunk reader="xmlItemReader" writer="mongodbItemWriter" commit-interval="2" processor="employeeFilter"/>; 
 <;/batch:tasklet>; 
 <;/batch:step>; 
 <;/batch:job>; 
 
 <;bean id="runScheduler" class="com.doj.batch.scheduler.RunScheduler" />; 
 
 <;!-- Run every 10 seconds -->; 
 <;task:scheduled-tasks>; 
 <;task:scheduled ref="runScheduler" method="run" cron="*/10 * * * * *" />; 
 <;/task:scheduled-tasks>; 
<;/beans>; 
</pre>
<h3><b>Run It-</b></h3>
<p>Loads the Spring application context, the scheduler will be run automatically.<br />
<b>TestBatch.java</b></p>
<pre class="highlight">package com.doj.batch.test; 
 
import org.springframework.context.ApplicationContext; 
import org.springframework.context.support.ClassPathXmlApplicationContext; 
 
/** 
 * @author Dinesh Rajput 
 * 
 */ 
public class TestBatch { 
 
 /** 
 * @param args 
 */ 
 public static void main(String[] args) { 
 ApplicationContext context = new ClassPathXmlApplicationContext("simple-job.xml"); 
 } 
 
} 
</pre>
<p><b>Output, it insert the xml content every 10 seconds in mongodb.</b></p>
<p><i>&#8230;&#8230;&#8230;&#8230;</i><br />
<i>&#8230;&#8230;&#8230;&#8230;..</i><br />
<i>Mar 9, 2014 5:43:18 PM org.springframework.oxm.jaxb.Jaxb2Marshaller createJaxbContextFromClasses</i><br />
<i>INFO: Creating JAXBContext with classes to be bound [class com.doj.batch.bean.Employee]</i><br />
<i>Sun Mar 09 17:43:20 IST 2014</i><br />
<i>Mar 9, 2014 5:43:20 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run</i><br />
<i>INFO: Job: [FlowJob: [name=simpleDojJob]] launched with the following parameters: [{date=Sun Mar 09 17:43:20 IST 2014}]</i><br />
<i>Mar 9, 2014 5:43:20 PM org.springframework.batch.core.job.AbstractJob handleStep</i><br />
<i>INFO: Executing step: [TaskletStep: [name=step1]]</i><br />
<i>Name- ATUL KUMAR Age- 37 salary- 300000.0 address delhi</i><br />
<i>Mar 9, 2014 5:43:20 PM org.springframework.oxm.jaxb.Jaxb2Marshaller processSource</i><br />
<i>Name- ASHUTOSH RAJPUT Age- 31 salary- 400000.0 address delhi</i><br />
<i>Mar 9, 2014 5:43:20 PM org.springframework.oxm.jaxb.Jaxb2Marshaller processSource</i><br />
<i>Name- Dinesh Rajput Age- 37 salary- 300000.0 address Noida</i><br />
<i>Exit Status : COMPLETED</i><br />
<i>Mar 9, 2014 5:43:20 PM org.springframework.batch.core.launch.support.SimpleJobLauncher$1 run</i><br />
<i>INFO: Job: [FlowJob: [name=simpleDojJob]] completed with the following parameters: [{date=Sun Mar 09 17:43:20 IST 2014}] and the following status: [COMPLETED]</i><br />
<i>Sun Mar 09 17:43:30 IST 2014</i><br />
<i>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;.</i><br />
<i>&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;&#8230;</i></p>
<div class="separator" style="clear: both; text-align: center;"><img src="https://dineshonjava.com/wp-content/uploads/2014/03/111-1.png" border="0" /></div>
</div>
</div>
<h2><b>Download Source Code with Jars</b><br />
<b><a href="https://sites.google.com/a/dineshonjava.com/dineshonjava/dineshonjava/SpringBatchTaskSchedulerExample.zip?attredirects=0&;d=1" target="_blank" rel="noopener">SpringBatchTaskSchedulerExample.zip</a></b></h2>
<p> ;</p>
<div style="text-align: center;"><b><;<;<a href="https://dineshonjava.com/multiresourceitemreader-in-spring-batch/">Previuos</a><;<; <a href="https://dineshonjava.com/spring-tutorial/">Index </a>>;>; <a href="https://dineshonjava.com/spring-batch-taskscheduler-example/">Next</a>>;>;</b></div>
</div>
<div class="wp-post-navigation"> 
									 <div class="wp-post-navigation-pre"> 
									 <a href="https://dineshonjava.com/multiresourceitemreader-in-spring-batch/">Previous</a> 
									 </div> 
									 <div class="wp-post-navigation-next"> 
									 <a href="https://dineshonjava.com/spring-application-context-provider/">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: '220'});
});
</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…