ram
ram

Reputation: 777

Handling application specific exceptions in Spring Batch Tasklet approach

How can we gracefully handle application specific exceptions in Spring Batch?

Case:

Consider a Spring Batch application(tasklet approach) containing three steps A, B, C.

Step A retrieves data, and in case no data is available, the job should skip the remaining steps & should shut down gracefully providing an error message.

Currently, if we throw an application specific exception, the entire trace is logged and job completes. In case there is a StepExecutionListener implementation, the afterStep method is executed before the job terminates.

Is there a way to supress the trace?

Additional notes:

  1. Step A should be in failed status.
  2. Can we use setTerminateOnly() in Step Execution for such purposes? If not, what is the general use of setTerminateOnly?

Upvotes: 2

Views: 4019

Answers (1)

Ashok Prajapati
Ashok Prajapati

Reputation: 374

you can set the exitStatus and batchStatus on StepExecution and call stepExecution.setTerminateOnly() to terminate the job. it will end up the job with job interruption exception. In the exit status you can set the message of you choice, for example - stepExecution.setExitStatus(new ExitStatus("FAILED", "Stopped job due to some error")). You can see this message in batch admin console.

Upvotes: 1

Related Questions