Mikhail Gordienko
Mikhail Gordienko

Reputation: 119

How to prevent Spring Batch Job to from being restarted on step fail?

I have simple single-step job:

@Bean(name = "restProcessorJob")
public Job job(@Qualifier("step") Step step) throws Exception {
    return jobBuilderFactory.get("restProcessorJob")
            .start(step)
            .build();
}

And if there was exception during step execution, Batch framework will try to execute it again immediately.

2016-12-12 18:49:45.558  INFO 10872 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=restProcessorJob]] launched with the following parameters: [{id=1481568585432}]
2016-12-12 18:49:45.572  INFO 10872 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step]
2016-12-12 18:49:45.597 DEBUG 10872 --- [           main] o.s.web.client.RestTemplate              : Created GET request for "http://myhost:myport.."
2016-12-12 18:49:45.646 DEBUG 10872 --- [           main] o.s.web.client.RestTemplate              : Setting request Accept header to [application/json, application/*+json]
2016-12-12 18:49:46.670 ERROR 10872 --- [           main] o.s.batch.core.step.AbstractStep         : Encountered an error executing step step in job restProcessorJob

org.springframework.web.client.ResourceAccessException: I/O error on GET request Connection refused: connect; nested exception is java.net.ConnectException: Connection refused: connect
    ... 

2016-12-12 18:49:46.689  INFO 10872 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=restProcessorJob]] completed with the following parameters: [{id=1481568585432}] and the following status: [FAILED]
2016-12-12 18:49:46.690  INFO 10872 --- [           main] o.s.b.a.b.JobLauncherCommandLineRunner   : Running default command line with: []
2016-12-12 18:49:46.727  INFO 10872 --- [           main] o.s.b.c.l.support.SimpleJobLauncher      : Job: [SimpleJob: [name=restProcessorJob]] launched with the following parameters: [{id=1481568585432}]
2016-12-12 18:49:46.736  INFO 10872 --- [           main] o.s.batch.core.job.SimpleStepHandler     : Executing step: [step]
...

Is there any way to configure such behavior and do not run the job twice on fail?

EDIT:

It seems that I've found the source of problem. Spring boot application tries to launch every CommandLineRunner it founds in the context. In my case it is the org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner. I have been trying to exclude this class:

@EnableAutoConfiguration(excludeName="org.springframework.boot.autoconfigure.batch.JobLauncherCommandLineRunner")

But without success...

Upvotes: 1

Views: 5335

Answers (2)

Nikhil Pareek
Nikhil Pareek

Reputation: 764

Use .preventRestart with an incrementer

@Bean(name = "restProcessorJob")
public Job job(@Qualifier("step") Step step) throws Exception {
    return jobBuilderFactory.get("restProcessorJob")
            .start(step)
            .incrementer(new RunIdIncrementer())
            .preventRestart()
            .build();
}

Upvotes: 1

Galeixo
Galeixo

Reputation: 102

You can configure it in your config file, you can put the skip limit to 0

Upvotes: 0

Related Questions