微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

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

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

例如,如果我有

   sqls" next="runsqls">
        tasklet transaction-manager="TransactionManager"
            ref="runGetsqls" />
    sqls">
        tasklet transaction-manager="TransactionManager"
            ref="runRunsqls" />
    

并且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">
    tasklet transaction-manager="TransactionManager"
        ref="runGetsqls" />
    sqls">
    tasklet transaction-manager="TransactionManager"
        ref="runRunsqls" />

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

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

原文地址:https://www.jb51.cc/spring/432720.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐