[英]How do I get the ID of the previous step execution in the next step of a spring batch job?
如何获得Spring Batch作业的下一步中上一步执行的ID?
我目前正在尝试对一组数据执行几个步骤,并将它们持久存储在数据库的不同行中。
每个步骤(第一个步骤除外)都将读取先前步骤的结果并对其进行进一步处理,然后将更新的结果保存在新行中。
我有一个自定义读取器,该读取器从数据库中读取数据,但是我需要将上一步执行的ID传递给当前步骤,以便它可以相应地查询存储库。
我该怎么做呢?
请按照以下步骤操作:
1.从当前的Step,将数据传递到StepExecutionContext
。
2.从当前StepExecutionContext
,将数据传递给JobExecutionContext
(通过listener
)
3. JobExecutionContext
在所有步骤之间共享,因此您现在可以获取先前Steps
数据。
另一种方法是将该临时Step数据存储在DB的表中JobExecutionId,StepExecutionId,StepData
一种选择是使用将在执行步骤之前和之后调用的StepExecutionListener
。 afterStep
方法具有StepExecution
对象。 您可以使用此StepExecution
设置下一步所需的所有参数。
例如:
currentStep.registerStepExecutionListener(new StepExecutionListener() {
@Override
public void beforeStep(StepExecution stepExecution) {}
@Override
public ExitStatus afterStep(StepExecution stepExecution) {
nextStep.setParameter(stepExecution.getExecutionContext().getString(param)
return ExitStatus.COMPLETED;
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.