Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

spring batch| Graceful job termination within the job

After launching a job, in the before job - there are certain occasions where we want to gracefully terminate the job (i.e. dont run the job at all but neither complain i.e .no exception). The current way of doing this looks like invoking jobExecution.stop - However, this results in JobInteruptedException which further results in logger.error invocation.

Is there any other better programmatic alternative (without manual intervention)?

like image 263
Jaga Avatar asked Oct 22 '22 08:10

Jaga


2 Answers

You may read :

  • Section 5.3.3 Configuring for Stop and
  • section 5.3.4. Programmatic Flow Decisions.

Just introduce an end element for your first step based on condition:

The 'end' element instructs a Job to stop with a BatchStatus of COMPLETED.

like image 142
Serkan Arıkuşu Avatar answered Oct 31 '22 09:10

Serkan Arıkuşu


I solved the problem adding a flag boolean executeTheJob in my "before job" listener that I set to false when I don't want to execute the job.

Then I handle that in my firstStep with this configuration:

<step id="firstStep" >
    <tasklet ref="myFirstTasklet"/>
    <stop on="STOPPED" restart="firstStep" />
    <next on="COMPLETED" to="nextStep"/>
</step>

And at the beginning of my first tasklet I have this:

if (executeTheJob == false) {
    contribution.setExitStatus(ExitStatus.STOPPED);
}
like image 34
Abel ANEIROS Avatar answered Oct 31 '22 11:10

Abel ANEIROS