Spring Batch:如何根据阅读器结果使用决策程序?

时间:2019-04-02 11:32:34

标签: spring-batch

我是Spring Batch的新手。我发现通过使用ExecutionContextPromotionListener,我可以设置键值对并在以后的步骤中使用它们。

<step id="step1">....</step>
<decision id="decision1">.... />

当我使用Tasklet代替阅读器时,我做了以下事情:

  1. 在我的批处理配置文件中使用预期的密钥创建了ExecutionContextPromotionListener的bean。

  2. Listener注册到我的步骤。

  3. executionContext中的键值对放入chunkContext内的Tasklet中,如下所示:

  4. 现在Decider可以从步骤执行上下文中读取并决定。

但是,我想根据上一步的Reader做出决定。因此,在我的决定中,如何从Reader中获得价值?还是我的方法是错误的。请提出建议。

1 个答案:

答案 0 :(得分:0)

一种简单的方法是,您可以从步骤中使用执行上下文,并将值传递到下一步。

因此,在第一步中,请执行以下操作

// ...
ExecutionContext stepContext = this.stepExecution.getExecutionContext();
stepContext.put("DATA_KEY", dataToShare );

然后在下一步中可以使用执行上下文阅读此内容。

ExecutionContext jobContext = jobExecution.getExecutionContext();
dataToShare = jobContext.get("DATA_KEY");

您只需要管理密钥-用于第一步并在下一步中阅读的密钥

相关问题