<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>In this tutorial, we will show you how to use Spring TaskScheduler to schedule a batch job to run every 5 seconds.
<!-- 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>
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(); } } }
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>
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; } }
<?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>
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"); } }
Labels: Spring Batch