Java Reference
In-Depth Information
Tasklet Step
The tasklet step is different than the others you've seen. But it should be the most familiar to you,
because it's what you used in the “Hello, World!” job. The way it's different is that in this case, you're
writing your own code to be executed as the tasklet. Using MethodInvokingTaskletAdapter is one way to
define a tasklet step. In that case, you allow Spring to forward the processing to your code. This lets you
develop regular POJOs and use them as steps.
The other way to create a tasklet step is to implement the Tasklet interface as you did when you
created the HelloWorld tasklet in Chapter 2. There, you implement the execute method required in the
interface and return a RepeatStatus object to tell Spring Batch what to do after you completed
processing. Listing 4-29 has the HelloWorld tasklet code as you constructed it in Chapter 2.
Listing 4-29. 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 final String HELLO_WORLD = "Hello, world!";
public RepeatStatus execute( StepContribution arg0,
ChunkContext arg1 ) throws Exception {
System.out.println( HELLO_WORLD );
return RepeatStatus.FINISHED;
When processing is complete in your Tasklet implementation, you return an
org.springframework.batch.repeat.RepeatStatus object. There are two options with this:
RepeatStatus.CONTINUABLE and RepeatStatus.FINISHED . These two values can be confusing at first
glance. If you return RepeatStatus.CONTINUABLE , you aren't saying that the job can continue. You're
telling Spring Batch to run the tasklet again. Say, for example, that you wanted to execute a particular
tasklet in a loop until a given condition was met, yet you still wanted to use Spring Batch to keep track of
how many times the tasklet was executed, transactions, and so on. Your tasklet could return
RepeatStatus.CONTINUABLE until the condition was met. If you return RepeatStatus.FINISHED , that means
the processing for this tasklet is complete (regardless of success) and to continue with the next piece of
You configure a tasklet step as you configure any of the other tasklet types. Listing 4-30 shows
HelloWorldJob configured using the HelloWorld tasklet.
Search WWH ::

Custom Search