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)
<!-- In memory JOB Management --> <!-- Datasource -->
Job definition (module-context.xml)