如何将数据放入作者内部的工作范围地图?

How to put data to job scope Map inside the writer?

我按照以下方式开始工作:

jobExecution = jobLauncher.run(job, jobParameters);
jobExecution... /// I want to get Map with results here

我还有以下作家:

@Component
public class MyWriter implements ItemWriter<MyBean> {

    @Override
    public void write(@NonNull List<? extends MyBean> items) throws Exception {
             MyResult result = someComponent.doSmth(items);
        }
    }
}

我想将结果放入 Map 以收集单个作业执行中的所有结果。

如何实现?

不确定为什么以及如何在 Map 中收集结果,但是如果 List(之后您可以转换为 Map)也可以,那么这是一个想法:

与其在自定义 ItemWriter 中调用 someComponent.doSmth,我宁愿在 ItemProcessorprocess 方法中调用(它一次只处理一个项目) time, mind you), return 结果,然后让 ListItemWriter 接收所有结果并将它们存储在 List 中,您可以通过方法 ListItemWriter.getWrittenItems.

您可以将结果放在作业执行上下文中,例如:

@Component
public class MyWriter implements ItemWriter<MyBean> {

    private JobExecution jobExecution;

    @BeforeStep
    public void beforeStep(StepExecution stepExecution) {
        jobExecution = stepExecution.getJobExecution();
    }

    @Override
    public void write(@NonNull List<? extends MyBean> items) throws Exception {
        MyResult result = someComponent.doSmth(items);
        jobExecution.getExecutionContext().put("result", result); // not sure how you are planning to use a map here
    }
}

然后从返回的作业执行的执行上下文中获取:

jobExecution = jobLauncher.run(job, jobParameters);
// I want to get Map with results here
MyResult result = jobExecution.getExecutionContext().get("result");