Java Reference
In-Depth Information
import org.springframework.batch.repeat.RepeatStatus;
import org.springframework.batch.item.ExecutionContext;
public class HelloWorld implements Tasklet {
private static final String HELLO_WORLD = "Hello, %s";
public RepeatStatus execute( StepContribution step,
ChunkContext context ) throws Exception {
String name =
(String) context.getStepContext()
.getJobParameters()
.get("name");
ExecutionContext jobContext = context.getStepContext()
.getStepExecution()
.getExecutionContext();
jobContext.put(“user.name", name);
System.out.println( String.format(HELLO_WORLD, name) );
return RepeatStatus.FINISHED;
}
}
ExecutionContext Persistence
As your jobs process, Spring Batch persists your state as part of committing each chunk. Part of that
persistence is the saving of the job and current step's
ExecutionContext
s. Chapter 2 went over the layout
of the tables. Let's go ahead and execute the
sampleJob
job with the updates from Listing 4-21 to see
what the values look like persisted in the database. Table 4-2 shows what the
batch_job_execution_context
table has in it after a single run with the
name
parameter set as
Michael
.
Table 4-2.
Contents of
BATCH_JOB_EXECUTION_CONTEXT
JOB_EXECUTION_ID
SHORT_CONTEXT
SERIALIZED_CONTEXT
1
{"map":{"entry":{"string":["user.name","
Michael"]}}}
NULL
Table 4-2 consists of three columns. The first is a reference to the
JobExecution
that this
ExecutionContext
is related to. The second is a JSON representation of the
Job
's
ExecutionContext
. This
field is updated as processing occurs. Finally, the
SERIALIZED_CONTEXT
field contains a serialized Java
object. The
SERIALIZED_CONTEXT
is only populated while a job is running or when it has failed.
This section of the chapter has gone through different pieces of what a job is in Spring Batch. In
order for a job to be valid, however, it requires at least one step, which brings you to the next major piece
of the Spring Batch framework: steps.