DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Thomas has posted 33 posts at DZone. View Full User Profile

Spring Batch Job Execution Listener That Add The Failure Exceptions Details In The Job Execution Exit Description

12.02.2011
| 7890 views |
  • submit to reddit
        Spring Batch Job execution listener that add the failure exceptions details in the job execution exit description.
package be.fgov.sigedis.db2p.batchprocessor.batch.listener;

import org.springframework.batch.core.ExitStatus;
import org.springframework.batch.core.JobExecution;
import org.springframework.batch.core.JobExecutionListener;
import org.springframework.stereotype.Component;

/**
 * Job execution listener that add the failure exceptions details 
 * in the job execution exit description.   
 * 
 * @author Thomas Vanstals
 * @since 1.2.0
 * 
 * @see JobExecution#getAllFailureExceptions()
 * @see ExitStatus#getExitDescription()
 */
@Component("jobFailureListener")
public class JobFailureListener implements JobExecutionListener {

 @Override
 public void beforeJob(JobExecution jobExecution) {
  // nothing to do
 }

 @Override
 public void afterJob(JobExecution jobExecution) {
  if (!jobExecution.getAllFailureExceptions().isEmpty()) {
   ExitStatus exitStatus = ExitStatus.FAILED;
   for (Throwable e : jobExecution.getAllFailureExceptions()) {
    exitStatus = exitStatus.addExitDescription(e);
   }
   jobExecution.setExitStatus(exitStatus);
  }
 }

}