Using job parameters
In this recipe, you'll learn how to retrieve and use a job parameter value in Tasklet
.
Getting ready
We'll use the job defined in the Creating a job recipe.
How to do it…
Follow these steps to use the job parameters:
In the
Task1
class, add@StepScope
to theexecute()
method:@StepScope public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { ...
In the
execute()
method, retrieve a job parameter value by using the job parameter name:String test = (String)chunkContext.getStepContext().getJobParameters(). get("test")
Run the job with a parameter named
test
:mvn compile exec:java - Dexec.mainClass=org.springframework.batch.core.launch. support.CommandLineJobRunner - Dexec.args="com.spring_cookbook.batch.BatchConfig job1 test=hello"
How it works…
The String
test will contain the hello
parameter value passed on the command line. This recipe will also work if the job is launched from a controller method.