A short introduction to Spring Batch 2.0
Spring Batch provides a framework in order to develop batch processes in Java. Recently version 2.0 has been released. However it’s still hard to find good samples and resources in the web, so old-fashioned development (based on RTFM) must be performed.
The main idea is to build Jobs from sequential steps. Each step can be a single task or an input/output process. Following Spring philosophy, configuration can be done using XML.
At least, two XML files must be defined:
- Launch context. Job launcher, datasources and other usual Spring resources.
- Job definition. Jobs and Spring beans implementing tasks.
A batch can be launched from command line using CommandLineJobLauncher class provided by the framework. The argument “job” indicates job name and some other job parameters can be added using “key=value” pattern.
java CommandLineJobRunner job-definition.xml job date=2008/01/01
Below a basic sample (read web server log file and insert processed lines to database) is exposed.
Launch context (job-definition.xml)
< ?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p" 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
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
<import resource="classpath:module-context.xml"/>
<!-- In memory JOB Management -->
<bean id="jobLauncher" class="es.lacaixa.premia.batch.launcher.StatisticsBatchLauncher">
<property name="jobRepository" ref="jobRepository" />
</bean>
<bean id="jobRepository" class="org.springframework.batch.core.repository.support.MapJobRepositoryFactoryBean">
<property name="transactionManager" ref="transactionManager"/>
</bean>
<bean id="transactionManager" class="org.springframework.batch.support.transaction.ResourcelessTransactionManager" />
<!-- Datasource -->
<bean id="dataSource" class="org.springframework.jdbc.datasource.SingleConnectionDataSource">
<property name="driverClassName" value="${jdbc.driver}" />
<property name="url" value="${jdbc.url}" />
<property name="username" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<bean id="transactionManagerAccess" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" lazy-init="true">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
Job definition (module-context.xml)
< ?xml version="1.0" encoding="UTF-8"?>
<beans :beans xmlns="http://www.springframework.org/schema/batch"
xmlns:beans="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:p="http://www.springframework.org/schema/p"
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.0.xsd
http://www.springframework.org/schema/batch
http://www.springframework.org/schema/batch/spring-batch-2.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-2.0.xsd">
<job id="job" job-repository="jobRepository">
<step id="stepWSLogFiles">
<tasklet>
<chunk processor="wsLogToDbTransform"
reader="flatFileWSItemReader"
writer="localDBItemWriter" commit-interval="100" />
</tasklet>
</step>
</job>
</beans><beans :bean id="localDbItemWriter" class="com.foo.PeticionDao">
<beans :property name="dataSource" ref="dataSource" />
</beans>
<beans :bean id="flatFileWSItemReader" class="org.springframework.batch.item.file.FlatFileItemReader">
<beans :property name="resource" value="${webserver.file}*" />
</beans><beans :property name="lineMapper">
</beans><beans :bean class="org.springframework.batch.item.file.mapping.DefaultLineMapper">
</beans><beans :property name="lineTokenizer">
</beans><beans :bean class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<beans :property name="delimiter" value=";" />
<beans :property name="names" value="date,hora,tm" />
</beans>
<beans :property name="fieldSetMapper">
</beans><beans :bean class="org.springframework.batch.item.file.mapping.BeanWrapperFieldSetMapper">
<beans :property name="targetType" value="com.foo.FileLogWS" />
</beans>
<beans :bean id="wsLogToDbTransform" class="com.foo.WSLogToDbTransform" />