【发布时间】:2018-11-03 12:26:26
【问题描述】:
我正在尝试获取上次完成作业的执行上下文。下面是代码:
@Component
public class MyJobDataReader implements ItemReader<MyDbEntity>, StepExecutionListener {
@Autowired
@Qualifier("jobService")
JobService jobService;
@Override
public void beforeStep(StepExecution stepExecution) {
Collection<JobExecution> jobExecutions = null;
try {
jobExecutions = jobService.listJobExecutionsForJob("myjob", 0, jobService.countJobExecutionsForJob("myjob"));
} catch (NoSuchJobException e) {
//log
}
JobExecution lastCompletedJobExecution =
jobExecutions
.stream()
.filter(param -> param.getExitStatus().getExitCode().equals(ExitStatus.COMPLETED.getExitCode()))
.findFirst()
.orElse(null);
Long myExecutionContextParam =
lastCompletedJobExecution
.getExecutionContext()
.getLong(contextParam);
}
}
但我得到 myExecutionContextParam 也是 null ,即使我看到 BATCH_JOB_EXECUTION_CONTEXT 表中的值用于获取的作业执行。
【问题讨论】:
-
你分享的代码没有编译通过,它应该实现
ItemReader接口的方法read。此外,MyJobDataReader应在该步骤中注册为侦听器,以便调用beforeStep。否则需要实现ItemStreamReader,它会自动注册。
标签: spring-batch