5

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)?

Jaga
  • 393
  • 2
  • 5
  • 15

4 Answers4

3

You may read :

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.

Ortomala Lokni
  • 56,620
  • 24
  • 188
  • 240
Serkan Arıkuşu
  • 5,549
  • 5
  • 33
  • 50
  • I'm afraid the solution proposed would require additional step. Since the check is in beforeJob, I don't even want to execute the first step, just quit gracefully the job. – Jaga Sep 29 '12 at 02:22
  • Really did not get your point. Assume that your job is reading from files to db and you want it to run only on mondays and if it is another day you want no exception -> graceful job termination. You will be writing a simple step (an "if" actually) and based on your condition, the job will be ended. Which part does not suit your needs? – Serkan Arıkuşu Sep 30 '12 at 18:41
  • >>You will be writing a simple step I don't want to write this addition step instead looking for something simple and intuitive like job.complete() Another reason to avoid writing a step is (correct me if I'm wrong here) - this step is actually a decider (JobExecutionDecider class) for which I need to configure xml telling if the status is complete/success then end the job else fail the job etc..) While having something like job.complete() would be a very convenient alternative --Thanks for your time in advance – Jaga Oct 01 '12 at 06:28
1

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);
}
Abel ANEIROS
  • 6,029
  • 2
  • 25
  • 19
0

stop() instruction will be active only if transaction commit successfully. If all you chunks rollback your job doesn't stop.

I have make this workaround: Create a ChunkListener and in the method afterChunkError(ChunkContext chunkCtx) put:

StepExecution stepExecution = chunkCtx.getStepContext().getStepExecution();
JobExecution jobExecution = jobExplorer.getJobExecution(stepExecution.getJobExecutionId());

if (jobExecution.getStatus().equals(BatchStatus.STOPPING)) {
    stepExecution.setTerminateOnly();
}

This will force a "controlled" stop.

Muthu Kumaran
  • 17,682
  • 5
  • 47
  • 70
-1

Instead of invoking stop() on the job execution, try signalling it via the JobOperator as shown in Stopping a Job

reevesy
  • 3,452
  • 1
  • 26
  • 23
firefox784
  • 657
  • 1
  • 6
  • 18