[英]Spring Batch Partitioning, how to stop a Job once any of the partitioned step throws exception
I am running a Spring Batch Job with Partitioned Step and if 1 of partitioned fails or throws exception i am required to stop the job immediately, how to stop a Job once any of the partitioned step throws exception, as currently other partitioned steps keep running till end and after they complete, the Job stops with unsuccessful return code. 我正在运行带有分区步骤的Spring Batch作业,并且如果其中1个分区失败或引发异常,我需要立即停止作业,一旦任何分区步骤引发异常,如何停止作业,因为当前其他分区步骤一直运行直到结束并完成后,作业将以失败的返回代码停止。
Code ::stepBuilderFactory.get("masterStep").allowStartIfComplete(true).partitioner(multithreadedPartitionerStep) .partitioner("multithreadedPartitionerStep", extractJobPartitioner).gridSize(gridSize) .taskExecutor(taskExecutor).build(); 代码:: stepBuilderFactory.get(“ masterStep”)。allowStartIfComplete(true).partitioner(multithreadedPartitionerStep).partitioner(“ multithreadedPartitionerStep”,extractJobPartitioner).gridSize(gridSize).taskExecutor(taskExecutor).build();
If I understood correctly, you want Spring Batch Job to attain a certain behaviour based on your step(s) output. 如果我理解正确,那么您希望Spring Batch Job根据您的步骤输出实现某种行为。
As @Jim mentioned above, show us your partition handler. 如上文@Jim所述,向我们展示您的分区处理程序。
First thing first - Job is a job. 第一件事-工作就是工作。 It consists of steps (partitioned may be).
它由步骤组成(可以分区)。 If any of the step's state goes stale it will reflect on the Job too (in generic terms)
如果任一步骤的状态过时,它也会在Job上反映(以通用术语表示)
Also by now, you might have moved to a newer version of Spring Batch by now I guess. 同样,到现在为止,我猜您可能已经迁移到了较新版本的Spring Batch。 Luckily, in 4.x world you have three options other than regular flow.
幸运的是,在4.x世界中,除了常规流程外,您还有三个选择。
Reference link - Spring Guides 参考链接- 春季指南
And you tell the Spring Batch what to do when at what steps. 然后您告诉Spring Batch在什么步骤执行什么操作。 Here is the quick glimpse of above link:
这是上面链接的快速浏览:
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(step1())
.next(step2())
.on("FAILED").end()
.from(step2()).on("*").to(step3())
.end()
.build();
}
In the following scenario, if step2 fails, then the Job stops with a BatchStatus of FAILED/COMPLETE/WHATEVER and an ExitStatus of EARLY TERMINATION and step3 does not execute. 在以下情况下,如果步骤2失败,则作业以BatchStatus为FAILED / COMPLETE / WHEREVER的状态停止,而ExitStatus为EARLY TERMINATION的状态终止,并且步骤3不执行。 Otherwise, execution moves to step3.
否则,执行转到步骤3。 Additionally, if step2 fails and the Job is restarted, then execution begins again on step2.
另外,如果步骤2失败并且作业重新启动,则执行将再次从步骤2开始。
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(step1())
.next(step2()).on("FAILED").fail()
.from(step2()).on("*").to(step3())
.end()
.build();
}
And 和
In the following scenario, if step1 finishes with COMPLETE/FAILED/WHATEVER, then the job stops. 在以下情况下,如果步骤1以COMPLETE / FAILED / WHEREVER结尾,则作业将停止。 Once it is restarted, execution begins on step2.
重新启动后,将在步骤2开始执行。
@Bean
public Job job() {
return this.jobBuilderFactory.get("job")
.start(step1()).on("COMPLETED").stopAndRestart(step2())
.end()
.build();
}
In this partitioned step, if any of the incorrect number of fields are found in any of the csv, it is reported correctly. 在此分区步骤中,如果在任何csv中找到任何不正确的字段数,则将正确报告该字段。 Here asynctaskexecutor and threadpooltaskexecutor are used for partitioned step.
这里,asynctaskexecutor和threadpooltaskexecutor用于分区步骤。
Hope it helps a bit and fits your scenario somehow. 希望它能有所帮助并以某种方式适合您的情况。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.