我需要一个tasklet,将所有文件存储在arraylist目录中。列表的大小存储在作业执行上下文中。稍后,可以在另一个步骤中从另一个Tasklet访问此计数。它是如何做到的。我试图将其存储在jobexecution上下文中,在运行时抛出无法修改的集合异常,
public RepeatStatus execute(StepContribution arg0, ChunkContext arg1) throws Exception { StepContext stepContext = arg1.getStepContext(); StepExecution stepExecution = stepContext.getStepExecution(); JobExecution jobExecution = stepExecution.getJobExecution(); ExecutionContext jobContext = jobExecution.getExecutionContext(); jobContext.put("FILE_COUNT",150000);
还在步骤注释中存储了步骤执行参考。仍然无法实现。请让我知道如何在两个小任务之间共享数据。
你至少有4种可能性:
设定值
public class ChangingJobExecutionContextTasklet implements Tasklet { /** {@inheritDoc} */ @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { // set variable in JobExecutionContext chunkContext .getStepContext() .getStepExecution() .getJobExecution() .getExecutionContext() .put("value", "foo"); // exit the step return RepeatStatus.FINISHED; } }
提取值
public class ReadingJobExecutionContextTasklet implements Tasklet { private static final Logger LOG = LoggerFactory.getLogger(ChangingJobExecutionContextTasklet.class); /** {@inheritDoc} */ @Override public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception { // pull variable from JobExecutionContext String value = (String) chunkContext .getStepContext() .getStepExecution() .getJobExecution() .getExecutionContext() .get("value"); LOG.debug("Found value in JobExecutionContext:" + value); // exit the step return RepeatStatus.FINISHED; } }