简体   繁体   English

Spring Batch分区,如何在任何分区步骤引发异常后停止作业

[英]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世界中,除了常规流程外,您还有三个选择。

  1. Ending at a Step 一步一步结束
  2. Failing a Step 失步
  3. Stopping a Job at a Given Step 在给定步骤停止作业

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.

相关问题 Spring批处理:传播在分区步骤中遇到的异常(停止作业执行) - Spring batch : Propagate exception encountered in partitioned step (Stop job execution) Spring Batch分区步骤 - Spring Batch partitioning a step 分区作业完成后不能自行停止? 春批 - Partitioned Job can't stop by itself after finishing? Spring Batch Spring Batch 3.0:StepExecutionListener 用于分区 Step 并将执行上下文值级联到分区作业 - Spring Batch 3.0 : StepExecutionListener for a partitioned Step and cascading of execution context values to the partitioned job 如何从 Spring 批处理作业中终止步骤 - How to terminate step from a Spring batch job Spring-batch 分区步骤重复处理 - Spring-batch partitioned step duplicated processing Spring 批量分区:具有多个步骤的从步骤 - Spring Batch Partitioning : Slave step with multiple steps 我们的 spring 批处理作业只有一个步骤。 我们在步骤中有读取器、处理器、写入器操作。 如何停止进入处理器步骤 - Our spring batch job is having only one step. We have reader, processor,writer Operations in the step. How to stop proceeding to processor step Spring批处理如何使一个步骤失败并因此从分区器内部执行作业 - Spring batch how to fail a step and therefor the job from inside a Partitioner 如何在不运行整个作业的情况下测试 Spring Batch 步骤 - How to test Spring Batch step without running entire job
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM