我想在ItemWriter中获取最后处理的项,并在执行上下文中写入项的字符串值之一,我使用块写入。因为它是块写入,所以写方法会被多次调用,我该怎么做呢?
这是我的作者:
@Component
public class MyItemWriter implements ItemWriter<MyDbEntity>{
private JobExecution jobExecution;
@BeforeStep
public void initWriter(StepExecution stepExecution) {
jobExecution = stepExecution.getJobExecution();
}
@Override
public void write(List<? extends MyDbEntity> items) throws Exception {
}发布于 2018-11-02 20:41:55
我希望在ItemWriter中获得最后处理的项,并在执行上下文中写入项的字符串值之一。
假设您的MyDbEntity为要在执行上下文中写入的数据提供了getter getData。一个简单的方法是为每个项目用相同的键写入数据。这将擦除前一项的数据,当步骤完成时,最后一项的数据将是执行上下文中的数据。类似于:
@Override
public void write(List<? extends MyDbEntity> items) throws Exception {
for(MyDbEntity item: items) {
// write item where needed
jobExecution.getExecutionContext().put("data", item.getData());
}
}作业执行将在步骤完成后(成功与否)持久化,您可以从执行上下文中访问最后一个项的数据。
希望这能有所帮助。
https://stackoverflow.com/questions/53122030
复制相似问题