Java Reference
In-Depth Information
Table 4-1. Contents of BATCH_JOB_PARAMS
JOB_
INSTANCE_
ID
TYPE_CD
KEY_NAME
STRING_VAL
DATE_VAL
LONG_VAL
DOUBLE_VAL
1
STRING
name
Michael
2
STRING
param1
Spring
2
LONG
param2
33
Now that you know how to get parameters into your batch jobs, how do you access them once you
have them? If you take a quick look at the ItemReader , ItemProcessor , ItemWriter , and Tasklet interfaces,
you quickly notice that all the methods of interest don't receive a JobParameters instance as one of their
parameters. There are a few different options depending on where you're attempting to access the
parameter:
ChunkContext : If you look at the HelloWorld tasklet, you see that the execute
method receives two parameters. The first parameter is
org.springframework.batch.core.StepContribution , which contains information
about where you are in the step (write count, read count, and so on). The second
parameter is an instance of ChunkContext . It provides the state of the job at the
point of execution. If you're in a tasklet, it contains any information about the
chunk you're processing. Information about that chunk includes information
about the step and job. As you might guess, ChunkContext has a reference to
org.springframework.batch.core.scope.context.StepContext , which contains
your JobParameters .
Late binding: For any piece of the framework that isn't a tasklet, the easiest way to
get a handle on a parameter is to inject it via the Spring Configuration. Given that
JobParameters are immutable, binding them during bootstrapping makes perfect
sense.
Listing 4-8 shows an updated HelloWorld tasklet that utilizes a name parameter in the output as an
example of how to access parameters from ChunkContext .
Listing 4-8. Accessing JobParameters in a Tasklet
package com.apress.springbatch.chapter4;
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;
import org.springframework.batch.item.ExecutionContext;
public class HelloWorld implements Tasklet {
private static final String HELLO_WORLD = "Hello, %s";
 
 
Search WWH ::




Custom Search