Spring batch 使用JobExecutionListener持续重新启动作业

Spring batch 使用JobExecutionListener持续重新启动作业,spring-batch,Spring Batch,我们需要一个连续运行的作业,而不需要外部调度器。 我扩展了作业执行侦听器,如下所示: @Autowired @Qualifier("myJob") private Job job; private int counter = 0; @Override public void afterJob(JobExecution jobExecution) { jobExecution.stop(); JobParameters jobParameters = new JobParame

我们需要一个连续运行的作业,而不需要外部调度器。 我扩展了作业执行侦听器,如下所示:

@Autowired
@Qualifier("myJob")
private Job job;

private int counter = 0;

@Override
public void afterJob(JobExecution jobExecution) {

   jobExecution.stop();

   JobParameters jobParameters = new JobParameters();
   JobParameter jobParameter = new JobParameter((new Integer(++counter)).toString());
   jobParameters.getParameters().put("counter", jobParameter);
   try {
      jobLauncher.run(job, jobParameters);
   } 
   catch (JobExecutionAlreadyRunningException e) {
      throw new RuntimeException(e);
   } catch (JobRestartException e) {
      throw new RuntimeException(e);
   } catch (JobInstanceAlreadyCompleteException e) {
      throw new RuntimeException(e);
   } catch (JobParametersInvalidException e) {
      throw new RuntimeException(e);
}
运行时,将引发JobExecutionAlreadyRunningException

JobExecutionAlreadyRunningException: A job execution for this job is already running: JobInstance: id=0, version=0, Job=[myJob]
我哪里做错了

感谢官方文件:

关闭不是立即的,因为没有办法强制关闭 立即关闭,特别是在当前正在执行的情况下 框架无法控制的开发人员代码,例如 商务服务。但是,一旦控制权返回到 框架,它将当前步骤执行的状态设置为 BatchStatus.STOPPED,保存它,然后对作业执行执行执行相同的操作 在完成之前

也许您有机会使用在前一个作业的线程终止后启动该作业的专用JobLauncher,或者使用与JobLauncher关联的自定义TaskExecutor