Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Batch accessing job parameter inside step

I have a following Spring Batch Job config:

@Configuration
@EnableBatchProcessing
public class JobConfig {

    @Autowired
    private JobBuilderFactory jobBuilderFactory;

    @Autowired
    private StepBuilderFactory stepBuilderFactory;

    @Bean
    public Job job() {
        return jobBuilderFactory.get("job")
                .flow(stepA()).on("FAILED").to(stepC())
                .from(stepA()).on("*").to(stepB()).next(stepC())
                .end().build();
    }

    @Bean
    public Step stepA() {
        return stepBuilderFactory.get("stepA").tasklet(new RandomFailTasket("stepA")).build();
    }

    @Bean
    public Step stepB() {
        return stepBuilderFactory.get("stepB").tasklet(new PrintTextTasklet("stepB")).build();
    }

    @Bean
    public Step stepC() {
        return stepBuilderFactory.get("stepC").tasklet(new PrintTextTasklet("stepC")).build();
    }

}

I'm starting the job with a following code:

    try {
                    Map<String,JobParameter> parameters = new HashMap<>();
                    JobParameter ccReportIdParameter = new JobParameter("03061980");
                    parameters.put("ccReportId", ccReportIdParameter);

                    jobLauncher.run(job, new JobParameters(parameters));
                } catch (JobExecutionAlreadyRunningException | JobRestartException | JobInstanceAlreadyCompleteException
                        | JobParametersInvalidException e) {
                    e.printStackTrace();
                }

How to access ccReportId parameter from job steps ?

like image 524
alexanoid Avatar asked Sep 08 '15 18:09

alexanoid


People also ask

How do you access job parameters in Spring Batch?

JobParameters can be used for identification or even as reference data during the job run. They have reserved names, so to access them we can use Spring Expression Language. For example to access a property 'abc' on job parameters: we can access it using the syntax #{jobParameters[abc]} .

How can we share data between the different steps of a job in Spring Batch?

Passing data between steps. In Spring Batch, ExecutionContext of execution context that can be used in the scope of each step and job is provided. By using the execution context, data can be shared between the components in the step.

What is chunking in Spring Batch?

Spring Batch uses a 'Chunk-oriented' processing style within its most common implementation. Chunk oriented processing refers to reading the data one at a time and creating 'chunks' that are written out within a transaction boundary.


1 Answers

Tasklet.execute() method takes parameter ChunkContext, where Spring Batch injects all the metadata. So you just need to dig into job parameters via these metadata structures:

chunkContext.getStepContext().getStepExecution()
      .getJobParameters().getString("ccReportId");

or other option is to access job parameters map this way:

chunkContext.getStepContext().getJobParameters().get("ccReportId");

but this gives you Object and you need to cast it to string.

like image 193
luboskrnac Avatar answered Sep 28 '22 11:09

luboskrnac