Sunday, October 6, 2013

Spring Batch "Hello World" 2

Posted in Java by Tareq Abedrabbo on Sunday October 05, 2008
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:
  1. public class ParameterPrintTasklet  
  2.       extends StepExecutionListenerSupport  
  3.       implements Tasklet {  
  4.   
  5.   private String message;  
  6.   
  7.   public ExitStatus execute() throws Exception {  
  8.       System.out.println(message);  
  9.       return ExitStatus.FINISHED;  
  10.   }  
  11.   
  12.   public void beforeStep(StepExecution stepExecution) {  
  13.       JobParameters jobParameters = stepExecution.getJobParameters();  
  14.       message = jobParameters.getString("message");  
  15.   }  
  16. }  
The XML configuration is straightforward:
  1. <beans xmlns="http://www.springframework.org/schema/beans"  
  2.         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
  3.         xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">  
  4.   <import resource="applicationContext.xml" />  
  5.   
  6.   <bean id="print" class="helloworld.ParameterPrintTasklet"/>  
  7.   
  8.   <bean id="simpleJob" class="org.springframework.batch.core.job.SimpleJob">  
  9.     <property name="name" value="simpleJob" />  
  10.     <property name="steps">  
  11.       <list>  
  12.         <bean class="org.springframework.batch.core.step.tasklet.TaskletStep">  
  13.           <property name="tasklet" ref="print"/>  
  14.           <property name="jobRepository" ref="jobRepository"/>  
  15.         </bean>  
  16.       </list>  
  17.     </property>  
  18.     <property name="jobRepository" ref="jobRepository"/>  
  19.   </bean>  
  20. </beans>  
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.

What's Next?

The next and final part will focus on an area where Spring Batch shines: item oriented jobs.

No comments: