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:

  1. In the Task1 class, add @StepScope to the execute() method:
    @StepScope
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext)
            throws Exception {
    ...
  2. In the execute() method, retrieve a job parameter value by using the job parameter name:
    String test = (String)chunkContext.getStepContext().getJobParameters(). get("test")
  3. 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 ...

Get Spring Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.