Spring Batch Quick Start
To quickly setup a Spring Batch job do the following steps as summarized below in the Spring configuration file:
1. Define Spring Batch namespace in your Spring configuration file
Listing 23-20. Defining batch related schema definition in the Spring configuration file
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:batch="http://www.springframework.org/schema/batch" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch-2.0.xsd"> … </beans> |
2. Configure appropriate readers, processors and writers
Listing 23-21. Configure FlatFileItemReader and FlatFileItemWriter
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
… <bean id="itemReader"> <property name="fieldSetMapper" value="…"/> <property name="resource" value="…"/> … </bean> <bean id="itemWriter"> <property name="fieldSetCreator" value="…"/> … </bean> … |
3. Configure Job along with Step(s)
Listing 23-22. Configure job along with steps
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<job id="sampleJob"> <step id="step1"> <tasklet> <chunk reader="itemReader" writer="itemWriter" commit-interval="10"/> </tasklet> </step> … </job> |
4. Configure JobLauncher
Listing 23-23. Configure JobLauncher
1 2 3 4 5 6 7 8 9 10 11 12 13 |
… <bean id="jobLauncher" class="org.springframework.batch.core.launch.support.SimpleJobLauncher"> <property name="jobRepository" ref="jobRepository"/> </bean> <batch:job-repository id="jobRepository"/> … |
5. Load and run the job – Once Spring related configuration is done as shown earlier in file applicationContext.xml, call up the Job and JobLauncher and execute the job as shown in Listing 23-24 below.
Listing 23-24. Load and execute job
1 2 3 4 5 6 7 8 9 10 11 |
//… ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml"); jobLauncher = context.getBean(JobLauncher.class); job = context.getBean("sampleJob", Job.class); JobExecution execution = jobLauncher.run(job, new JobParameters()); //… |
Spring Batch also provides command-line utility to run the Batch job using the class org.springframework.batch.core.launch.support.CommonLineJobRunner, usage of which is shown below.
1 |
$java -classpath … org.springframework.batch.core.launch.support.CommonLineJobRunner applicationContext.xml sampleJob |
6. JobLauncher returns the result – use the “execution” object to see the exit status of the executed job.
Figure 23-14. Working of Spring Batch
Before explaining Figure 23-14 above, I would like to spend some time explaining the various batch idioms which you will come across in Spring Batch. The next section covers these in detail and once this is done in a comprehensive manner, we will come back to the Figure 23-14 and explain it completely in all aspects.
Page Visitors: 25939
Tomcy John
Latest posts by Tomcy John (see all)
- A Guide to Continuous Improvement for Architects - February 2, 2023
- Cloud-first Architecture Strategy - January 26, 2023
- Architecture Strategy and how to create One - January 24, 2023
Thanks for sharing the information but what’s the difference between CursorItemReader’s setFetchSize() and PagingItemReader’s setPageSize()? isn’t it the same?
Please provide an example for StoredProcedureItem Reader – which returns cursor and how to process cursor in processor – I am new to Spring Batch as part of my work I need to invoke Oracle Stored proc which takes one param as input and returns result set which I need to process in Spring Batch Processor.
Thanks
Laxmi