Nenad Bozic
Nenad Bozic

Reputation: 3784

Throwing exceptions in spring batch and failing gracefully

In @BeforeStep of ItemReader we are calling soap client to fill in list of items to process later in read() method. That call can throw exception because of communication errors between our application and 3rd party server. We have scheduler which is triggered every 10 minutes and tries to restart failed jobs and would make this job pass (since communication errors happen but are fixed after some time).

We would like to automate job execution and handle all exceptions in job flow, catch them on one place and fail job gracefully (preferably with reason of mistake), and have some mechanism in place which attempts to restart jobs but stop attempts after N tries and notify via email someone to take a look at reason of error. Full log of exceptions with exceptions after restart attempts will be huge so we would like to avoid that.

  1. Do you see any problems with this approach?
  2. Is there a class which is catching exceptions in job and setting status, fail reason etc. (I know that for reades, processors and writers there are listeners that have onReadError but what if error happens in @BeforeStep?
  3. How to fail job gracefully from inside job components or some kind of listener which is invoked on exceptions?

Upvotes: 3

Views: 2193

Answers (1)

Nenad Bozic
Nenad Bozic

Reputation: 3784

Regarding restarting jobs which failed for N times we implemented separate job which is scheduled, and runs each 10 minutes, checks meta tables of spring batch and does restart attempt. We created custom JobInstanceDao with query which will pick up only instances which failed less than N times.

Regarding failing job within job itself, we added JobExecutionDecider implementation, it is checking failure exceptions, and on exception in which we are interested we are invoking logic to set FlowExecutionStatus. Basically we imported csv file, we want to complete the job if parsing exception happens (since this is not something we can recover) and we want to fail job if some other exception happen since that can be recoverable and restart logic should pick it up.

In addition to all this we used spring retry mechanism to job which is doing soap calls which made our job robust against client server communication errors.

Upvotes: 2

Related Questions