问题描述
我有一个要求,其中一个tasklet将所有文件存储在arraylist目录中.列表的大小存储在作业执行上下文中.稍后,可以在另一个步骤中从另一个Tasklet访问此计数.它是如何做到的.我试图将其存储在jobexecution上下文中,在运行时抛出不可修改的集合异常,
I have a requirement in which a tasklet, stores all the files in the directories in an arraylist. The size of the list is stored in the job execution context. Later this count is accessed from another tasklet in another step. How do it do this. I tried to store in jobexecution context, at runtime throws unmodifiable collection exception,
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);
还在stepstep注释中存储了stepexecut参考.仍然没有办法.请让我知道如何在两个tasklet之间共享数据.
also stored the stepexection reference in beforestep annotation .still not possioble.kindly let me know ,how to share data between two tasklets.
推荐答案
您至少有4种可能性:
- 使用ExecutionPromotionListener将将数据传递到以后的步骤
- 使用(spring)bean来保存步骤间数据,例如一个ConcurrentHashMap
- 如果不采取进一步措施,将无法重新启动该数据
- use the ExecutionPromotionListener to pass data to future steps
- use a (spring) bean to hold inter-step data, e.g. a ConcurrentHashMap
- without further action this data won't be accessible for a re-start
从Tasklet访问JobExecution的代码示例:
Code Example for accessing JobExecution from Tasklet:
-
设置值
setting a value
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;
}
}
提取值
extracting a value
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;
}
}
我在我的spring-batch-examples github存储库中为前3个解决方案创建了代码示例,请参见模块 complex 并打包 interstepcommunication
i created code examples for the first 3 solutions in my spring-batch-examples github repository, see module complex and package interstepcommunication
这篇关于从Tasklet存储在JobExecutionContext中并在另一个Tasklet中访问的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!