如何在Spring Batch中的步骤之间传递List <object>

时间:2019-11-28 12:04:37

标签: spring spring-batch

我尝试使用以下代码将列表从步骤1传递到步骤2:

public class MyJob {

        @Bean
        public ItemReader<Object> itemReader() {
           //return a List from Database
        }

        @Bean
        public ItemWriter<Object> itemWriter() {
            return new ItemWriter<Object>() {

                private StepExecution stepExecution;

                @Override
                public void write(List<? extends Object> items) {

                        ExecutionContext stepContext = this.stepExecution.getExecutionContext();
                        stepContext.put("databaseList", entrustCustomer);
                }

                @BeforeStep
                public void saveStepExecution(StepExecution stepExecution) {
                    this.stepExecution = stepExecution;
                }
            };
        }

        @Bean
        public Step step1() {
            return steps.get("step1")
                    .<Object, Object>chunk(1)
                    .reader(itemReader())
                    .writer(itemWriter())
                    .listener(promotionListener())
                    .build();
        }

        @Bean
        public Step step2() {
            return steps.get("step2")
                    .tasklet((contribution, chunkContext) -> {
                        List<Object> a = 
                     (List<Object>)ChunkContext.getStepContext().getJobExecutionContext().get("count");
                    //Code to print out List a

                        return RepeatStatus.FINISHED;
                    })
                    .build();
        }

        @Bean
        public ExecutionContextPromotionListener promotionListener() {
            ExecutionContextPromotionListener listener = new ExecutionContextPromotionListener();
            listener.setKeys(new String[] {"count"});
            return listener;
        }

        @Bean
        public Job job() {
            return jobs.get("job")
                    .start(step1())
                    .next(step2())
                    .build();
        }

    }

但是,我的代码只是打印出列表的最后一行。当我将sysout放到步骤1的itemWriter中打印项目时,它逐个返回每个对象。
如何在步骤之间传递列表?
如果列表很大,有什么问题吗?

0 个答案:

没有答案