1

1.Problem Summary

After I run a job, I see the TABLE batch_step_context like the pic: screenshot of the table

The beautiful information of the FIELD short_context is:

{
    "batch.taskletType": "com.a.b.job.config.BundleJobConfig$$Lambda$763/0x000000080070f840",
    "batch.stepType": "org.springframework.batch.core.step.tasklet.TaskletStep"
}

How can I add extra information in it?

such as, after a job is finish, I want my FIELD short_context is like this:

{
    "output": "hello world",
    "batch.taskletType": "com.a.b.job.config.BundleJobConfig$$Lambda$763/0x000000080070f840",
    "batch.stepType": "org.springframework.batch.core.step.tasklet.TaskletStep"
}

how can I add extra information like 'output'.

2.Part of My Code

@Configuration
public class AJobConfig {

    private final JobBuilderFactory jobBuilderFactory;

    private final StepBuilderFactory stepBuilderFactory;

    @Autowired
    public AJobConfig(JobBuilderFactory jobBuilderFactory, StepBuilderFactory stepBuilderFactory) {
        this.jobBuilderFactory = jobBuilderFactory;
        this.stepBuilderFactory = stepBuilderFactory;
    }

    @Bean
    public Job aJob(Step aStep) {
        return jobBuilderFactory.get("aJob")
                .start(aStep)
                .build();
    }

    @Bean
    public Step aStep() {
        return stepBuilderFactory.get("aStep")
                .tasklet((contribution, chunkContext) -> {

                    System.out.println("hi");
                  
                    return RepeatStatus.FINISHED;
                })
                .build();
    }
}
tequila
  • 23
  • 4

1 Answers1

0

It is a really easy question when I found some useful code at this website

Part of the useful code is :

...
        @Override
    public ExitStatus afterStep(StepExecution stepExecution) {
        fu.closeReader();
        stepExecution
          .getJobExecution()
          .getExecutionContext()
          .put("lines", this.lines);
        logger.debug("Lines Reader ended.");
        return ExitStatus.COMPLETED;
    }
...

It inspired me a lot.

What I need to do is just add a listener to the step.

This is my final code of aStep:

...
@Bean
    public Step aStep() {
        return stepBuilderFactory.get("aStep")
                .tasklet((contribution, chunkContext) -> {
                    System.out.println("hi");
                  
                    return RepeatStatus.FINISHED;
                })
                .listener(new StepExecutionListener() {
                    @Override
                    public void beforeStep(StepExecution stepExecution) {

                    }

                    @Override
                    public ExitStatus afterStep(StepExecution stepExecution) {
                        stepExecution
                                .getExecutionContext()
                                .put("output","hello world");

                        return ExitStatus.COMPLETED;
                    }
                })
                .build();
    }
...

Of cause I get what I want in the TABLE batch_step_execution_context.

enter image description here

tequila
  • 23
  • 4
  • you can also do it in the step itself (if this is part of the step's logic) with `chunkContext.getStepContext().getStepExecution().getExecutionContext().put("output", "hello world");`. This does not require a listener, but it is up to you to choose the appropriate approach for your use case. – Mahmoud Ben Hassine Oct 28 '20 at 08:50