加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

java – 在Spring中的步骤之间传递信息?

发布时间:2020-12-15 01:48:55 所属栏目:大数据 来源:网络整理
导读:我正在尝试制作一个Spring Batch而我没有经验. 是否可以从每个批处理步骤传递信息,或者它们是否必须完全独立? 例如,如果我有 并且getSQLs触发一个bean,该bean执行一个类,该类生成一个String类型的List.是否可以引用runSQLs触发的bean列表? (“触发”可能不

我正在尝试制作一个Spring Batch而我没有经验.

是否可以从每个批处理步骤传递信息,或者它们是否必须完全独立?

例如,如果我有

   

并且getSQLs触发一个bean,该bean执行一个类,该类生成一个String类型的List.是否可以引用runSQLs触发的bean列表? (“触发”可能不是正确的术语,但我想你知道我的意思)

更新:
所以getSQLs步骤会触发这个bean:

它触发执行此方法的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.

应该使用您的步骤配置监听器,该步骤与未来的数据共享数据:

应从执行代码块填充数据,如下所示:

// ...
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");
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读