sureshhewabi
sureshhewabi

Reputation: 91

Spring batch execute last step even get an exception

I want to run a spring batch job which has set of steps and finally I want to send a notification to redis containing the status of the Job execution. Let's say if all the steps are executed, I should send "Pass". If there was any execution or any error, I want to pass "Fail". So my last step will be notification to redis updating the status regardless of it finished fine or got an exception.

My question is:

  1. Can I achieve this in Spring Batch?
  2. Can I use notification function as a last step or should I use any specific method for this?
  3. How can I get the status of jobs?

I know I can get the job status like :

 JobExecution execution = jobLauncher.run(job, params);
 System.out.println("Exit Status : " + execution.getStatus());

But I call the job in command-line like java -jar app.jar ----spring.batch.job.names=myjobnamehere so that I do not use a JobExecution object.

Upvotes: 0

Views: 1702

Answers (1)

Mahmoud Ben Hassine
Mahmoud Ben Hassine

Reputation: 31720

You can use a JobExecutionListener for that. In the afterJob method, you have a reference to the JobExecution from which you can get the status of the job and send the notification as required.

You can find an example in the getting started guide (See JobCompletionNotificationListener).

Upvotes: 2

Related Questions