Download - Spring-Batch Tutorial
![Page 1: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/1.jpg)
Spring-Batch Tutorial
Guide for Application Developers
![Page 2: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/2.jpg)
Agenda “Hello world!” job Simple job – programming a Tasklet
directly Common job – weaving standard
components Sample jobs
![Page 3: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/3.jpg)
“Hello World!” - taskletpublic class HelloWorldTasklet implements Tasklet {
public ExitStatus execute() throws Exception {
System.out.println("Hello world!");
return ExitStatus.FINISHED;
}
}
![Page 4: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/4.jpg)
“Hello World!” - taskletpublic class HelloWorldTasklet implements Tasklet {
public ExitStatus execute() throws Exception {
System.out.println("Hello world!");
return ExitStatus.FINISHED;
}
}
![Page 5: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/5.jpg)
“Hello World!” - configuration<bean id="jobConfiguration" parent="simpleJob">
<property name="name" value="helloWorldJob" />
<property name="steps">
<list>
<bean id="step1" parent="simpleStep">
<constructor-arg>
<bean class="...HelloWorldTasklet" />
</constructor-arg>
</bean>
</list>
</property>
</bean>
![Page 6: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/6.jpg)
“Hello World!” - configuration<bean id="jobConfiguration" parent="simpleJob">
<property name="name" value="helloWorldJob" />
<property name="steps">
<list>
<bean id="step1" parent="simpleStep">
<constructor-arg>
<bean class="...HelloWorldTasklet" />
</constructor-arg>
</bean>
</list>
</property>
</bean>
![Page 7: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/7.jpg)
Simple job - taskletpublic class SimpleTasklet implements Tasklet {
private InputSource inputSource;private OutputSource outputSource;
public ExitStatus execute() throws Exception {Object data = inputSource.read();
if (data != null) {outputSource.write(data);return ExitStatus.CONTINUABLE;
}return ExitStatus.FINISHED;
}public void setInputSource(InputSource inputSource) {
this.inputSource = inputSource;}public void setOutputSource(OutputSource outputSource) {
this.outputSource = outputSource;}
}
![Page 8: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/8.jpg)
Simple job - taskletpublic class SimpleTasklet implements Tasklet {
private InputSource inputSource;private OutputSource outputSource;
public ExitStatus execute() throws Exception {Object data = inputSource.read();
if (data != null) {outputSource.write(data);return ExitStatus.CONTINUABLE;
}return ExitStatus.FINISHED;
}public void setInputSource(InputSource inputSource) {
this.inputSource = inputSource;}public void setOutputSource(OutputSource outputSource) {
this.outputSource = outputSource;}
}
![Page 9: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/9.jpg)
Simple job - taskletpublic class SimpleTasklet implements Tasklet {
private InputSource inputSource;private OutputSource outputSource;
public ExitStatus execute() throws Exception {Object data = inputSource.read();
if (data != null) {outputSource.write(data);return ExitStatus.CONTINUABLE;
}return ExitStatus.FINISHED;
}public void setInputSource(InputSource inputSource) {
this.inputSource = inputSource;}public void setOutputSource(OutputSource outputSource) {
this.outputSource = outputSource;}
}
![Page 10: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/10.jpg)
Simple job - configuration<bean id="jobConfiguration" parent="simpleJob">
<property name="name" value="simpleTaskletJob" />
<property name="steps">
<list>
<bean id="step1" parent="simpleStep">
<constructor-arg>
<bean class="...SimpleTasklet">
<property name="inputSource" ref="inputSource" />
<property name="outputSource" ref="outputSource" />
</bean>
</constructor-arg>
</bean>
</list>
</property>
</bean>
![Page 11: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/11.jpg)
Simple job – input source<bean id=“inputSource" class="...SqlCursorInputSource“
scope="step" >
<aop:scoped-proxy />
<property name="dataSource" ref="dataSource" />
<property name="sql"
value="SELECT id, quantity, price, customer from TRADE" />
<property name="mapper">
<bean class="...TradeRowMapper" />
</property>
</bean>
![Page 12: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/12.jpg)
Simple job – input source<bean id=“inputSource" class="...SqlCursorInputSource“
scope="step" >
<aop:scoped-proxy />
<constructor-arg>
<ref bean="dataSource" />
</constructor-arg>
<property name="sql"
value="SELECT id, quantity, price, customer from TRADE" />
<property name="mapper">
<bean class="...TradeRowMapper" />
</property>
</bean>
![Page 13: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/13.jpg)
Simple job – output source<bean id="outputSource" class="...FlatFileOutputSource“
scope="step" >
<aop:scoped-proxy />
<property name="resource" value="file:out.txt" />
</bean>
![Page 14: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/14.jpg)
Common job - configuration<bean id="jobConfiguration" parent="simpleJob">
<property name="name" value="commonJob" /><property name="steps"> <bean id="step1" parent="simpleStep">
<constructor-arg> <bean class="...tasklet.RestartableItemProviderTasklet">
<property name="itemProvider"> <bean class="...tasklet.support.InputSourceItemProvider"> <property name="source" ref="sqlInputSource" /> </bean> </property> <property name="itemProcessor"> <bean
class="...tasklet.support.OutputSourceItemProcessor"> <property name="source" ref="xmlOutputSource" /> </bean> </property> </bean></constructor-arg>
</bean></property>
</bean>
![Page 15: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/15.jpg)
Common job – tasklet<bean class="...tasklet.RestartableItemProviderTasklet">
<property name="itemProvider"> <bean class="...tasklet.support.InputSourceItemProvider">
<property name="source" ref="sqlInputSource" /> </bean></property>
<property name="itemProcessor"> <bean class="...tasklet.support.OutputSourceItemProcessor">
<property name="source" ref="xmlOutputSource" /> </bean></property>
</bean>
![Page 16: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/16.jpg)
Common job – tasklet<bean class="...tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean class="...tasklet.support.InputSourceItemProvider">
<property name="source" ref="sqlInputSource" />
</bean>
</property>
<property name="itemProcessor">
<bean class="...tasklet.support.OutputSourceItemProcessor">
<property name="source" ref="xmlOutputSource" />
</bean>
</property>
</bean>
![Page 17: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/17.jpg)
Common job – tasklet<bean class="...tasklet.RestartableItemProviderTasklet">
<property name="itemProvider">
<bean class="...tasklet.support.InputSourceItemProvider">
<property name="source" ref="sqlInputSource" />
</bean>
</property>
<property name="itemProcessor">
<bean class="...tasklet.support.OutputSourceItemProcessor">
<property name="source" ref="xmlOutputSource" />
</bean>
</property>
</bean>
![Page 18: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/18.jpg)
Samples overview Samples project contains simple batch
jobs illustrating various capabilities of the Spring-Batch framework
See the folder src/main/resources/jobs for job configuration files
See src/test/java, package org.springframework.batch.sample for tests which launch the jobs and check the expected results
![Page 19: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/19.jpg)
simpleTaskletJob.xml Straightforwardly implemented Tasklet,
similar to the “Simple Job” example All-in-one solution to help understand
tasklet’s execution logic Standard solutions are more modular,
which makes them more flexible and reusable, but also less straightforward to understand
![Page 20: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/20.jpg)
fixedLengthImportJob.xml Clean separation of reading input and
processing data (standard from now on) Typical scenario of importing data from a
fixed-length file to database Custom DAO used for output
![Page 21: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/21.jpg)
multilineOrderJob.xml Handling of complex file format, both
reading input and writing output Single record spans multiple lines and has
nested records Custom ItemProvider and ItemProcessor
implementations handling non-standard file format
![Page 22: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/22.jpg)
tradeJob.xml shows a reasonably complex scenario,
that would resemble the real-life usage of the framework
3 steps: trade records are imported from file to
database customer account balance is adjusted report about customers is exported to a file
![Page 23: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/23.jpg)
compositeProcessorSample.xml Parallel writing to multiple outputs Example usage of composite
ItemProcessor with an injected list of ItemProcessors
![Page 24: Spring-Batch Tutorial](https://reader033.vdocuments.site/reader033/viewer/2022061520/5681402c550346895dab8e1f/html5/thumbnails/24.jpg)
restartSample.xml Simulates restart scenario, where the job
crashes on first run and succeeds after being restarted
Uses ‘hacked’ tasklet that throws exception after reading a given number of records