0

I'm configuring a (long running) job in Spring Batch with an async JobLauncher, and I have two REST endpoint:

  1. /start --> will start the job asynchronously and return the job_execution_id to the client
  2. /status/{job_execution_id} --> will return the status of the job execution based on data stored in JobExecutionContext

In the /status endpoint, I would like to inform the client about any exceptions occurred during the process. However, I'm not able to retrieve them in the way I was doing with the sync version of the same job:

   jobExecution.getAllFailureExceptions() --> empty list
   stepExecution.getFailureExceptions()  --> empty list

Is there a way to tell Spring Batch to store the exception stacktrace (or at least the exception message), so I can retrieve it later?

Thanks Giulio

2 Answers 2

1

Failure exceptions are added after the job execution is finished (more precisely right before the job is about to finish). So they are not available while the job is running. That's why you can't get them if you call the /status endpoint while the job is running asynchronously in the background.

The same applies for step failure exceptions, but those should be available as soon as the step is finished (while eventual subsequent steps are still running and the surrounding job as well).

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the answer, I completely understand your point about job failure exceptions. I was expecting, as you are saying, that step failure exceptions were stored in the database and could be retrieved via the JobExecution object, but it seems not to be the case: this works only in synchronous mode. I found a working solution using a StepExecutionListener (afterStep) to save the exception (if any) in the JobExecutionContext, so it will be serialized in the database and /status endpoint is able to access the exception: does it make sense to you?
I was expecting, as you are saying, that step failure exceptions were stored in the database and could be retrieved via the JobExecution object: I did not say that, and I hope my answer did not imply that. Failure exceptions are transient and therefore they are not persisted in the database. They are only accessible through the in-memory StepExecution/JobExecution object after the step/job is finished.
Thanks for the clarification and sorry for the misunderstanding, of course your answer didn't imply that the exceptions were stored in the database. In fact, also looking at the code, I came to the conclusion that I have to store the exception by myself, and, in fact, I'm already successfully doing it with a StepExecutionListener. Thanks again for the support.
0

The same happens when you use Remote Chunking. There is no way that you can use the FaultTolerance with SkipPolicy, the manager configuration won't pick the listener.

For your particular case, very sad i had to use for example: StepExecutionListener and use the afterStep to get the exitDescription from ExitStatus. Well the best solution is using the Worker and handle there the exception.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.