Spring Batch Simple Example Part 3

In this post I will explain how to combine the reader and writer in a job and how to run the job.

We define a job which will transfer the employee records from one file to another file in batches.

<batch:job id="importEmployees">
        <batch:step id="readWriteEmployees">
                <batch:chunk reader="reader" writer="writer" commit-interval="50"/>
    <bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
    <bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
        <property name="transactionManager" ref="transactionManager"/>
    <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher">
        <property name="jobRepository" ref="jobRepository"/>

In the above xml configuation we defined a job bean with id “importEmployees”. Added a step in the job with id “readWriteEmployees”. The step is further defined using transactional
and repeatable process called Tasklet. In the above example we have defined a Chunk oriented tasklet.

In chunk oriented tasklet, data are read one at a time and written to destination file in chunks. The number of items in a chunk can be configured by setting the commit-interval
attribute. The Tasklet uses the bean with id “reader” to read the elements and “writer” bean to write the elements.

The job also requires a job repository where job runtime informmation is stored. In our example we are using an in-memory repository. By default job searches of job repository
bean definition with id “jobRepository” in the configuration file. The job repository requires a transaction manager which is declared with bean definition of id “transactionManager”.

Next we define JobLauncher bean which is responsible for launching the jobs.

Below is java main class code which will start the job

package package1;

import java.util.Date;

import org.springframework.batch.core.Job;
import org.springframework.batch.core.JobParameters;
import org.springframework.batch.core.JobParametersBuilder;
import org.springframework.batch.core.JobParametersInvalidException;
import org.springframework.batch.core.launch.JobLauncher;
import org.springframework.batch.core.repository.JobExecutionAlreadyRunningException;
import org.springframework.batch.core.repository.JobInstanceAlreadyCompleteException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.batch.core.repository.JobRestartException;

public class Example1 {
    public static void main(String[] args) throws JobParametersInvalidException, JobInstanceAlreadyCompleteException, JobExecutionAlreadyRunningException, JobRestartException {
        ApplicationContext context = new ClassPathXmlApplicationContext("package1\\job.xml");
        JobLauncher jobLauncher = (JobLauncher)context.getBean("jobLauncher");
        JobParameters jobParameters = new JobParametersBuilder().addDate("date", new Date()).toJobParameters();
        Job job = (Job)context.getBean("importEmployees");
        jobLauncher.run(job, jobParameters);

We create an application context and retrieve the job launcher and job instance. Create job parameter which has launch date as one of the parameter.

At last we run the job using the below code
jobLauncher.run(job, jobParameters);


Jun 24, 2018 12:37:56 PM org.springframework.context.support.ClassPathXmlApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@32a1bec0: startup date [Sun Jun 24 12:37:56 IST 2018]; root of context hierarchy
Jun 24, 2018 12:37:56 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [package1/job.xml]
Jun 24, 2018 12:37:57 PM org.springframework.batch.core.launch.support.SimpleJobLauncher afterPropertiesSet
INFO: No TaskExecutor has been set, defaulting to synchronous executor.
Jun 24, 2018 12:37:57 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=importEmployees]] launched with the following parameters: [{date=1529824077670}]
Jun 24, 2018 12:37:57 PM org.springframework.batch.core.job.SimpleStepHandler handleStep
INFO: Executing step: [readWriteEmployees]
Jun 24, 2018 12:37:58 PM org.springframework.batch.core.launch.support.SimpleJobLauncher run
INFO: Job: [FlowJob: [name=importEmployees]] completed with the following parameters: [{date=1529824077670}] and the following status: [COMPLETED]

Leave a Reply