【问题标题】:How to get the start or end date of the last execution of a Spring Batch job that has a completed status?如何获取具有已完成状态的 Spring Batch 作业的最后执行的开始或结束日期?
【发布时间】:2022-01-04 04:11:53
【问题描述】:

我正在编写一个 Spring Batch 进程(Spring boot 2.x.x)。该作业根据上次更新日期从表中选择一些行 -

(select * from tablename where last_upd_dt >= :lastruntime).

:lastruntime 是作业最后一次(成功)完成运行的日期。我正在尝试使用 JobListener/JobExecutionListener 从 Listener 类中的 beforeJob 方法中获取此日期。

有没有办法利用 Spring Batch 框架提供的类?否则,我将不得不从另一个表中写入和检索此信息。

【问题讨论】:

    标签: java spring-boot spring-batch


    【解决方案1】:

    您可以使用JobExplorer API。它允许您获取作业实例的作业执行。一旦你得到它,你可以根据需要过滤结果并从 JobExecution 对象中获取开始/结束日期。

    【讨论】:

      【解决方案2】:

      我能够使用 JobExplorer API。

      private Date fetchPreviousJobCompletetionDate() {
          Date previousJobCompleteDate = null;
          try {
              Integer jobInstanceCount = jobExplorer.getJobInstanceCount("myjob");
              List<JobInstance> jobInstances = jobExplorer.getJobInstances("myjob", 0, jobInstanceCount);
              List<JobExecution> jobExecutions = jobInstances.stream().map(jobExplorer::getJobExecutions)
                      .flatMap(List<JobExecution>::stream)
                      .filter(param -> param.getExitStatus().equals(ExitStatus.COMPLETED)).collect(Collectors.toList());
              if (CollectionUtils.isNotEmpty(jobExecutions)) {
                  Optional<JobExecution> jobExecutionOptional = jobExecutions.stream().sorted((JobExecution execution1, JobExecution execution2) -> execution2.getStartTime()
                          .compareTo(execution1.getStartTime())).findFirst();
                  if (jobExecutionOptional.isPresent()) {
                      previousJobCompleteDate = jobExecutionOptional.get().getStartTime();
                  }
              }
          } catch (NoSuchJobException exception) {
              log.error("Exception occurred {}", exception.getMessage());
          }
          return previousJobCompleteDate;
      }
      

      【讨论】:

        猜你喜欢
        • 2019-11-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-30
        • 2016-10-03
        • 2020-07-04
        • 1970-01-01
        相关资源
        最近更新 更多