简体   繁体   中英

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.

Code ::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.

As @Jim mentioned above, show us your partition handler.

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)

Also by now, you might have moved to a newer version of Spring Batch by now I guess. Luckily, in 4.x world you have three options other than regular flow.

  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. 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. Otherwise, execution moves to step3. Additionally, if step2 fails and the Job is restarted, then execution begins again on step2.

@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. Once it is restarted, execution begins on step2.

@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. Here asynctaskexecutor and threadpooltaskexecutor are used for partitioned step.

Hope it helps a bit and fits your scenario somehow.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM