Java Reference
In-Depth Information
the same type of tag that you used in the job definition, it is. You can declare the steps inline if you want.
However, in this example I created a step outside of the job and made it the parent of the step within the
job. 2 I did this for two reasons: to keep the XML cleaner and to allow for easy extraction of steps into
other XML files if needed. You'll see in future chapters that the XML for steps can get quite verbose; the
approach shown here helps to keep the job readable.
Your job is configured, but you have a class in that configuration that doesn't exist: the HelloWorld
tasklet. Create the tasklet in the src/main/java/com/apress/springbatch/chapter2 directory. As you can
guess, the code is pretty simple; see Listing 2-5.
Listing 2-5. HelloWorld Tasklet
package com.apress.springbatch.chapter2;
import org.springframework.batch.core.StepContribution;
import org.springframework.batch.core.scope.context.ChunkContext;
import org.springframework.batch.core.step.tasklet.Tasklet;
import org.springframework.batch.repeat.RepeatStatus;
public class HelloWorld implements Tasklet {
private static fi nal String HELLO_WORLD = "Hello, world!";
public RepeatStatus execute( StepContribution arg0, ChunkContext arg1 ) throws Exception
System.out.println( HELLO_WORLD );
return RepeatStatus. FI NISHED;
To create the HelloWorld tasklet, you implement the Tasklet interface's single method: execute.
StepContribution and ChunkContext represent the context of the step (commit count, skip count, and
so on) in which this tasklet is being executed. Future chapters get into those in more detail.
Running Your Job
That's really it. Let's try building and running the job. To compile it, run mvn clean compile from the
root of the project. When the build is successful, run the job. Spring Batch comes with its own job runner
called CommandLineJobRunner. As you can guess, it's intended to be run from … a command line! In
this topic, you will execute your jobs from your project's target directory so that you won't need to go
through setting up the classpath. The CommandLineJobRunner takes two or more parameters: the path
to the XML file that contains the job configuration, the name of the job to be executed, and a list of job
parameters. In the case of HelloWorldJob, you only need to pass the first two parameters. To execute the
job, run the command shown in Listing 2-6.
Listing 2-6. Execute the HelloWorld Job
java -jar hello-world-0.0.1-SNAPSHOT.jar jobs/helloWorld.xml helloWorldJob
2 Chapter 4 covers the parent attribute of a step in detail.
Search WWH ::

Custom Search