我正在尝试制作一个Spring Batch而我没有经验.
是否可以从每个批处理步骤传递信息,或者它们是否必须完全独立?
例如,如果我有
sqls" next="runsqls">
sqls" />
sqls">
sqls" />
并且getsqls触发一个bean,该bean执行一个类,该类生成一个String类型的List.是否可以引用runsqls触发的bean列表? (“触发”可能不是正确的术语,但我想你知道我的意思)
更新:
所以getsqls步骤会触发这个bean:
sqls" class="myTask"
scope="step">
它触发执行此方法的myTask类:
@Override
public RepeatStatus execute(StepContribution contribution,ChunkContext chunkContext) throws Exception {
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("theListKey",sourceQueries);
return RepeatStatus.FINISHED;
}
我是否需要以某种方式将stepExecution传递给execute方法?
最佳答案
Spring Batch支持将数据推送到将来的作业步骤,这可以通过ExecutionContext完成,更确切地说是JobExecutionContext.这里我指的是example from the official documentation,因为它是我的最终参考:
To make the data available to future Steps,it will have to be
“promoted” to the Job ExecutionContext after the step has finished.
Spring Batch provides the ExecutionContextPromotionListener for this
purpose.
应该使用您的步骤配置监听器,该步骤与未来的数据共享数据:
sqls" next="runsqls">
sqls" />
sqls">
sqls" />
应从执行代码块填充数据,如下所示:
// ...
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("theListKey",yourList);
然后在后续步骤中,可以使用@BeforeStep a注释的后计算挂钩检索此List,如下所示:
@BeforeStep
public void retrieveSharedData(StepExecution stepExecution) {
JobExecution jobExecution = stepExecution.getJobExecution();
ExecutionContext jobContext = jobExecution.getExecutionContext();
this.myList = jobContext.get("theListKey");
}