【问题标题】:Get last completed jobs execution context spring batch获取最后完成的作业执行上下文春季批处理
【发布时间】: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


【解决方案1】:

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

【讨论】:

  • 我收到“无法加载作业执行的执行上下文”错误 - 对此有何想法?
  • 能否分享完整的工作代码。我有兴趣查看 JobService 类。
猜你喜欢
  • 2020-04-23
  • 2018-10-04
  • 1970-01-01
  • 1970-01-01
  • 2014-06-30
  • 2017-05-12
  • 2018-02-23
  • 2021-12-28
  • 2012-10-19
相关资源
最近更新 更多