Book Image

SPRING COOKBOOK

Book Image

SPRING COOKBOOK

Overview of this book

Table of Contents (19 chapters)
Spring Cookbook
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

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 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.