1

I am converting Spring Batch XML based application into the Spring Boot annotations. In my XML file, I am not sure how to pass those jobParameters and execution context to tasklet ?

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:batch="http://www.springframework.org/schema/batch" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:task="http://www.springframework.org/schema/task" xmlns:util="http://www.springframework.org/schema/util"
    xsi:schemaLocation="http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/batch http://www.springframework.org/schema/batch/spring-batch.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/task  http://www.springframework.org/schema/task/spring-task.xsd
        http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd">

    <batch:job id="job1">
        <batch:step id="contextStep" >
            <batch:tasklet ref="contextTasklet" />
        </batch:step>
    </batch:job>

    <bean id="contextTasklet" class="com.XXX.batch.tasklet.ContextTasklet" scope="step">
        <property name="runMode" value="#{jobParameters['runMode']}" />
        <property name="executionContext" value="#{stepExecution.jobExecution.executionContext}" />
    </bean>
</beans>

ContextTasklet.java

@Data
@Configuration
@StepScope
public class ContextTasklet implements Tasklet{

    private String runMode;
    private ExecutionContext executionContext;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
        chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("mode",
                runMode);

        return RepeatStatus.FINISHED;
    }
}
Jeff Cook
  • 7,956
  • 36
  • 115
  • 186

1 Answers1

1

You can use the @Value annotation to inject job parameters and execution context values:

@Data
@Configuration
@StepScope
public class ContextTasklet implements Tasklet{

   @Value("#{jobParameters['runMode']}")
   private String runMode;

   @Value("#{stepExecution.jobExecution.executionContext}")
   private ExecutionContext executionContext;

   @Override
   public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {
       chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext().put("mode",
            runMode);

       return RepeatStatus.FINISHED;
    }
}
Mahmoud Ben Hassine
  • 28,519
  • 3
  • 32
  • 50
  • First this has to be set into the JobParameters and where and exactly which place it should be used was the my questions. so I added a service class and there I added the JobParams, it works now – Jeff Cook Aug 11 '18 at 21:45