获取最后完成的作业执行上下文春季批处理

时间:2018-11-03 12:26:27

标签: spring-batch

我正在尝试获取最后完成的作业的执行上下文。下面是代码:

@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表中看到了所获取的作业执行的值,我也将BATCH_JOB_EXECUTION_CONTEXT也设置为null。

1 个答案:

答案 0 :(得分:0)

问题是jobService.listJobExecutionsForJob返回的JobExecution字段很少,然后我不得不打电话给jobService.getJobExecution(jobExecutionId)以获取完全丰富的JobExecution