我在执行以下操作(简化)时遇到了弹簧批处理流程的问题
给出以下MVE:
@Configuration
@EnableBatchProcessing
public class BatchExample {
@Autowired
private JobBuilderFactory jobBuilderFactory;
@Autowired
private StepBuilderFactory stepBuilderFactory;
@Bean
public Job exampleJob() {
final FlowBuilder<Flow> innerFlowBuilder = new FlowBuilder<>("innerFlow");
final Flow innerFlow = innerFlowBuilder
.start(dummyStep("first innerflow step"))
.next(dummyStep("second innerflow step"))
.next(dummyStep("last innerflow step"))
.build();
final FlowBuilder<Flow> outerFlowBuilder = new FlowBuilder<>("outerFlow");
final Flow outerFlow = outerFlowBuilder
.start(dummyStep("always execute me"))
.next(decide()).on("CONTINUE").to(innerFlow)
.from(decide()).on("COMPLETED").end("COMPLETED")
.build();
return jobBuilderFactory.get("exampleJob")
.start(outerFlow)
.end()
.build();
}
private Step dummyStep(String arg) {
return stepBuilderFactory.get("step_" + arg)
.tasklet(dummyTasklet(arg))
.build();
}
private Tasklet dummyTasklet(String arg) {
return new DummyTasklet(arg);
}
private DummyDecider decide() {
return new DummyDecider();
}
class DummyTasklet implements Tasklet {
private final String arg;
DummyTasklet(String arg) {
this.arg = arg;
}
@Override
public RepeatStatus execute(StepContribution stepContribution, ChunkContext chunkContext) throws Exception {
System.out.println("hello from dummy tasklet: " + arg);
return RepeatStatus.FINISHED;
}
}
class DummyDecider implements JobExecutionDecider {
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
final Random random = new Random();
final int i = random.nextInt();
if (i % 2 == 0) {
return new FlowExecutionStatus("CONTINUE");
} else {
return FlowExecutionStatus.COMPLETED;
}
}
}
}
执行代码时,“继续”流程进行得很顺利,但“完成”流程始终会以失败的作业状态退出。
Job: [FlowJob: [name=exampleJob]] completed with the following
parameters: [{}] and the following status: [FAILED]
如何使工作完成并显示为COMPLETED状态?换句话说,我对流的编码做错了什么?
该代码可以在Spring Boot应用程序中运行
@SpringBootApplication
public class FlowApplication implements ApplicationRunner {
public static void main(String[] args) {
SpringApplication.run(FlowApplication.class, args);
}
@Autowired
private Job exampleJob;
@Autowired
private JobLauncher jobLauncher;
@Override
public void run(ApplicationArguments args) throws Exception {
jobLauncher.run(exampleJob, new JobParameters());
}
}
修改
实施建议的答案时
final Flow outerFlow = outerFlowBuilder
.start(dummyStep("always execute me"))
.on("*").to(decide())
.from(decide()).on("CONTINUE").to(innerFlow)
.from(decide()).on("COMPLETED").end("COMPLETED")
.build();
并迫使决策者“继续”:
class DummyDecider implements JobExecutionDecider {
@Override
public FlowExecutionStatus decide(JobExecution jobExecution, StepExecution stepExecution) {
return new FlowExecutionStatus("CONTINUE");
}
}
内部流程不再启动:
Executing step: [step_always execute me]
hello from dummy tasklet: always execute me
Job: [FlowJob: [name=exampleJob]] completed with the following parameters: [{}] and the following status: [FAILED]
Job: [FlowJob: [name=exampleJob]] launched with the following parameters: [{}]
Step already complete or not restartable, so no action to execute: StepExecution: id=0, version=3, name=step_always execute me, status=COMPLETED, exitStatus=COMPLETED, readCount=0, filterCount=0, writeCount=0 readSkipCount=0, writeSkipCount=0, processSkipCount=0, commitCount=1, rollbackCount=0, exitDescription=
Job: [FlowJob: [name=exampleJob]] completed with the following parameters: [{}] and the following status: [FAILED]
答案 0 :(得分:0)
您的outerFlow
应该定义如下:
final Flow outerFlow = outerFlowBuilder
.start(dummyStep("always execute me"))
.on("*").to(decide())
.from(decide()).on("CONTINUE").to(innerFlow)
.from(decide()).on("COMPLETED").end("COMPLETED")
.build();
我用您的示例对此进行了测试(谢谢您的MVE!),当决策者返回COMPLETED
时,工作不会失败。
原因是,如果没有将dummyStep("always execute me")
步骤的所有结果路由到.on("*").to(decide())
到决策者,则流程定义不正确。
希望这会有所帮助。