Last time we implemented a simple PrintTasklet that prints a "Hello World!" message. In this part we will improve our tasklet a little bit by passing the message to print as a parameter through the command line. Thus, we will start usingJobParameters.
Each batch job is started with a JobParameters object; it is what Spring Batch uses to distinguish between different instances of the same job. It also serves as a holder for runtime data, just as we will be using it in this example. JobParameters are typed. They support properties of numerical, date and String types. We will pass our message as a String parameter aptly named "message".
Let's see how we will pass our message from the command line down to the tasklet. When a job is started with CommandLineJobRunner, this latter can interpret appropriately formatted arguments and transform them to JobParameters. The basic format is paramaterName(type)=value. The type can be omitted for Stringparameters.
Now we need to read the message from within our tasklet. Spring Batch provides a collection of listener interfaces that enable hooking custom code at different points of the execution of a job. In order to access JobParameters from our tasklet, we will implement the StepExecutionListener interface and read the message before executing the step; that is in the beforeStep method: As a convenience, we will subclass StepExecutionListenerSupport, which is an empty implementation ofStepExecutionListener, and redefine the method we need:
The XML configuration is straightforward:
Usually, we would register StepExecutionListeners at the TaskletStep level, butTaskletStep is smart enough to detect that its Tasklet is also a listener and registers it automatically.
Running the Job
Here's the Maven command to launch the job with CommandLineJobRunner. Notice how we pass the message as an argument this time:
mvn exec:java -Dexec.mainClass= org.springframework.batch.core.launch.support.CommandLineJobRunner -Dexec.args="simpleJob.xml simpleJob message=Hello_World!"
The code source can be downloaded here.
The next and final part will focus on an area where Spring Batch shines: item oriented jobs.