代码之家  ›  专栏  ›  技术社区  ›  Manas Saxena

ItemWriter spring批处理中最后处理的项

  •  1
  • Manas Saxena  · 技术社区  · 7 年前

    我想在ItemWriter中获取最后一个处理的项,并在执行上下文中写入该项的字符串值之一,我使用的是chunks writes。既然是块写,那么write方法会被多次调用,我该怎么做呢?

    @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 {
    
    
    
        }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Mahmoud Ben Hassine    7 年前

    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());
        }
    }
    

    希望这有帮助。