Spring Batch - 步骤不再执行:步骤已经完成或不可重启

SpringBatch - Step no longer executing: Step already complete or not restartable

我有一个单步 springbatch 应用程序。职位如下:

@Bean
public Job databaseCursorJob(@Qualifier("databaseCursorStep") Step exampleJobStep,
                             JobBuilderFactory jobBuilderFactory) {
    return jobBuilderFactory.get("databaseCursorJob")
            .incrementer(new RunIdIncrementer())
            .flow(exampleJobStep)
            .end()
            .build();
}

我从 springboot 应用程序启动作业。今天下午,我试图在工作中添加第二个步骤。主要如下:

@Bean
public Job databaseCursorJob(@Qualifier("databaseCursorStep") Step exampleJobStep,
                             JobBuilderFactory jobBuilderFactory) {
    return jobBuilderFactory.get("databaseCursorJob")
            .incrementer(new RunIdIncrementer())
            .flow(exampleJobStep).next(partitionStep())
            .end()
            .build();
}

换句话说,只需添加“next(partitionStep())”。然而,自从我这样做后,作业就完成了,没有执行任何步骤(参见下面的 shell 输出)。事实上,即使删除第二步并返回原始作业后,它拒绝执行该步骤。在尝试添加第二步之前,我从来没有遇到过这个问题。我什至重新启动了我的虚拟机,它仍然跳过了这一步.在我解决这个问题之前,我已经死在水里了。感谢任何见解。谢谢。

2020-09-01 14:49:00.260  INFO 6913 --- [           main] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8087 (http) with context path ''
2020-09-01 14:49:00.263  INFO 6913 --- [           main] f.p.r.Application    : Started Application in 7.752 seconds (JVM running for 9.092)
2020-09-01 14:49:00.268  INFO 6913 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2020-09-01 14:49:00.579  INFO 6913 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=databaseCursorJob]] launched with the following parameters: [{}]
2020-09-01 14:49:00.698  INFO 6913 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Step already complete or not restartable, so no action to execute: StepExecution: id=120, version=4, name=databaseCursorStep, status=COMPLETED, exitStatus=COMPLETED, readCount=1, filterCount=0, writeCount=1 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=2, rollbackCount=0, exitDescription=
2020-09-01 14:49:00.730  INFO 6913 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [FlowJob: [name=databaseCursorJob]] completed with the following parameters: [{}] and the following status: [COMPLETED]

我的问题是,如果出现错误或卡在未知状态,我的工作将无法恢复。该步骤不是“已经完成”,它从未完成。它的状态仍然是“STARTED”,退出代码是“UNKNOWN”,因为它从未退出过。无论如何,我的作业存储库不在内存中,而是捕获到本地数据库,这就是为什么即使在重新启动 VM 后它也没有自行解决的原因(我不记得这一点真可惜)。因此,我能够通过清除作业实例历史记录来修复,但那是 band-aid。我仍然需要修复我的代码以防止它再次发生。

我还了解到我可以通过检查数据库中的作业存储库(它都在那里)来进行诊断。

我真的解决了这个问题,感谢 Hassine 先生多次在上面回复并为我指明了正确的方向。他在第一个回复中提供的 link 确实解决了将来预防的解决方案:Spring Batch error (A Job Instance Already Exists) and RunIdIncrementer generates only once