【问题标题】:Spring Batch re-run steps multiple times on a schdeuleSpring Batch 按计划多次重新运行步骤
【发布时间】:2022-01-07 00:51:45
【问题描述】:

我已经配置了一个调度程序来以特定的时间间隔调用我的作业(出于测试目的,我尝试了 30 秒的时间间隔)。它成功地调用了作业,没有任何问题。但是一旦在第一次运行期间作业执行成功它就不会再次重新运行该步骤(尽管我在每次调用中都传递了一个新的作业参数)。即使完成,我也想运行相同的步骤,因为源数据库中可能有新数据。我试过了

.allowStartIfComplete(true)

但它没有在该步骤中调用阅读器。

主要要求是重新运行相同的批处理作业步骤,无论是否成功退出

@Scheduled(fixedRate=30*1000)
@SchedulerLock(name = "job-1", lockAtLeastFor = "15S", lockAtMostFor = "20S")
public void batchJob() throws InterruptedException {
JobParameters Parameters = new JobParametersBuilder()
    .addLong("startAt", System.currentTimeMillis()).toJobParameters();
try {
    jobLauncher.run(job, Parameters);
} catch (JobExecutionAlreadyRunningException | JobRestartException
    | JobInstanceAlreadyCompleteException | JobParametersInvalidException e) {

    e.printStackTrace();
}

}

     // Executes the job
  @Bean
  public Job jobUpdate(JobCompletionNotificationListener listener, Step step1) {
    return this.jobBuilderFactory.get("employee")
        .listener(listener)
        .incrementer(new RunIdIncrementer())
        .start(step1)
        .build();
  }


  @Bean
  public Step step1() {
    return stepBuilderFactory.get("step1")
        .allowStartIfComplete(true)
        .<Employee, Employee>chunk(2)
        .reader(reader())
        .processor(processor())
        .faultTolerant().skipPolicy(httpClientExceptionSkipper())
        .writer(compositeItemWriter())
        .build();
  }

下面的初始运行后来自控制台日志

2021-11-30 14:40:29.731  INFO --- o.s.b.c.j.SimpleStepHandler : Executing step: [step1]
2021-11-30 14:40:29.747  INFO --- o.s.b.c.s.AbstractStep : Step: [step1] executed in 16ms
2021-11-30 14:40:29.752  INFO --- l.d.d.b.n.JobCompletionNotificationListener : !!! JOB FINISHED! Time to verify the results
2021-11-30 14:40:29.756  INFO --- o.s.b.c.l.s.SimpleJobLauncher$1 : Job: [SimpleJob: [name=employee]] completed with the following 

如果有人能对此有所了解,真的很感激。

【问题讨论】:

    标签: java spring spring-batch batch-processing


    【解决方案1】:

    尝试对您在 step1 方法中使用的阅读器 bean 使用 @StepScope。

    它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-11
      • 2023-03-13
      • 2020-12-20
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      • 2021-06-09
      相关资源
      最近更新 更多