Spring Batch Example
This is a basic guide to get you started with Spring Batch Framework. The framework has alot to get your head around.
Key Terms with references
- Job – Contains steps for the process execution (describes what job is and how it is executed). It is entire set of batch work.
- Job Instance – Represents the given job during the execution process (organises the job process with job parameters)
- Job Parameters – are type properties that are parameters for the job instance.
- Job Execution – is what happend during job run (i.e. status, failures etc.) – Each run of Job Instance results in Job Execution.
- Step Execution – is a run of a single step execution in Job Instance.
- Job Repository – represents a persistent store (i.e. database) for the jobs to run.
- Tasklet – is to setup or cleanup resources before the main business logic is executed.
- Job Launcher – is responsible for starting a Job with given job parameters
Example
We will be using SimpleJobLauncher that relies on a TaskExecutor to launch the jobs. If there is no specific TaskExecutor is set then a SyncTaskExecutor is used. We will use the SimpleJobRepository implementation which requires a set of execution Daos to store its information.
Tasklet Example
public class PrintTasklet implements Tasklet
{
private String message;
public void setMessage(String message)
{
this.message = message;
}
public ExitStatus execute() throws Exception
{
System.out.println("**** "+message+" ******");
return ExitStatus.FINISHED;
}
}
Luncher Context Example
<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-2.5.xsd"> <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository"/> </bean> <bean id="jobRepository" class="org.springframework.batch.core.repository.support.SimpleJobRepository"> <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> </bean> </beans>
Job XML Example
<bean id="hello" class="eg.spring.batch.PrintTasklet"> <property name="message" value="Hello"/> </bean> <bean id="space" class="eg.spring.batch.PrintTasklet"> <property name="message" value=" "/> </bean> <bean id="world" class="eg.spring.batch.PrintTasklet"> <property name="message" value="World!"/> </bean> <bean id="taskletStep" abstract="true" class="org.springframework.batch.core.step.tasklet.TaskletStep"> <property name="jobRepository" ref="jobRepository"/> </bean> <bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob"> <property name="name" value="simpleJob" /> <property name="steps"> <list> <bean parent="taskletStep"> <property name="tasklet" ref="hello"/> </bean> <bean parent="taskletStep"> <property name="tasklet" ref="space"/> </bean> <bean parent="taskletStep"> <property name="tasklet" ref="world"/> </bean> </list> </property> <property name="jobRepository" ref="jobRepository"/> </bean> </beans>
Running the Example
Download spring batch version 1.2 and add all the jars into the classpath. Also add in launcherContext.xml and simpleJob.xml in classpath as well as PrintTaskLet.class or in a jar file. Ensure that PrintTasklet class is in package “eg.spring.batch”.
Then from command prompt:
java org.springframework.batch.core.launch.support.CommandLineJobRunner simpleJob.xml simpleJob
Resourcess