Reputation: 8627
I'm using a tasklet and a StepExecutionListener
but it seems there's no listener callback for a case where my tasklet throws an exception. For various other listener types – ChunkListener
, ItemProcessListener
, etc. – there is but none of those listeners work with tasklets.
All I want is an event after my tasklet executes regardless of whether it threw an exception or not. Is it possible to do that? It doesn't appear to be supported in the API.
Edit: Responding to @danidemi I'm registering the listener and tasklet using the programmatic API like this:
steps.get(name)
.listener(listener)
.tasklet(tasklet)
.build()
Where steps
is an instance of StepBuilderFactory
.
Upvotes: 2
Views: 13721
Reputation: 69
I guess its too late now. But I recently came across this problem. It is easier to handle exception with ChunkListener, however exception handling can be done in Tasklet's RepeatStatus execute(StepContribution s, ChunkContext chunkContext)
method (well, it is the only method that Tasklet
interface has ^^). What you need is a try/catch
block to catch the exceptions. However you will need to throw
the caught exception again in order to roll back the transaction.
Here is a code-snippet. In my case, I had to stop the job if some data could not be read due to Database server shutdown.
@Override
public RepeatStatus execute(StepContribution s, ChunkContext chunkContext){
try{
getAllUsersFromDb(); // some operation that could throw an exception
// doesn't hurt to put all suspicious codes in this block tbh
}catch(Exception e){
if(e instanceof NonSkippableReadException){
chunkContext.getStepContext().getStepExecution().getJobExecution().stop();
}
throw e;
}
return RepeatStatus.FINISHED;
}
Upvotes: -1
Reputation: 18403
You can
or in StepExecutionListener.afterStep(StepExecution stepExecution)
lookup into stepExecution.getFailureExceptions()
Upvotes: 4