问题描述
1。问题摘要
我运行作业后,看到如图所示的表batch_step_context: screenshot of the table
FIELD short_context的美丽信息是:
{
"batch.taskletType": "com.a.b.job.config.BundleJobConfig$$Lambda$763/0x000000080070f840","batch.stepType": "org.springframework.batch.core.step.tasklet.taskletStep"
}
如何在其中添加其他信息?
例如,在工作完成之后,我希望我的FIELD short_context像这样:
{
"output": "hello world","batch.taskletType": "com.a.b.job.config.BundleJobConfig$$Lambda$763/0x000000080070f840","batch.stepType": "org.springframework.batch.core.step.tasklet.taskletStep"
}
2。我的代码的一部分
@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();
}
}
解决方法
当我在此website
找到一些有用的代码时,这是一个非常简单的问题部分有用的代码是:
...
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
fu.closeReader();
stepExecution
.getJobExecution()
.getExecutionContext()
.put("lines",this.lines);
logger.debug("Lines Reader ended.");
return ExitStatus.COMPLETED;
}
...
这启发了我很多。
我需要做的就是在该步骤中添加一个侦听器。
这是我的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();
}
...
原因是我在表batch_step_execution_context中得到了想要的东西。