将失败的步骤添加到执行上下文的步骤侦听器

Step Listener to add failed steps to execution context

我想知道是否有办法使用 afterStep 并制作一个步骤执行侦听器来检查作业中是否有任何失败的步骤,并将该步骤名称和退出状态添加到执行上下文中。

虽然我工作中的一个步骤失败了,但我们 return RepeatStatus.FINISHED。我创建了一个电子邮件报告,我想包括失败的步骤名称和状态。这是我的电子邮件 tasklet

public class SendEmailTasklet implements Tasklet {

    final static Logger LOGGER = LoggerFactory.getLogger(SendEmailTasklet.class);

    @Autowired
    public JavaMailSender emailSender;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
        ExecutionContext ec = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();

        //obtain email address and program name from the execution context
        String programName = ec.getString(AbstractSetupTasklet.BATCH_PROGRAM_NAME);
        String toEmail = jobParameters.getString("TOEMAIL");

        if(StringUtils.isEmpty(toEmail)) {

            LOGGER.info("No email address associated with the user. Job status is " + programStatus);

            return RepeatStatus.FINISHED;
        }
        else {

            //construct the message
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(toEmail);
            message.setSubject("Batch Reporting");
            message.setText("The batch program " + programName + " has exited with a status of " + programStatus);
            emailSender.send(message);

            LOGGER.info("Email succesfully sent to user at " + toEmail);

            return RepeatStatus.FINISHED;
        }
    }


}

如上面的代码所示,我想 return programStatus 或 'the job failed on X step with a status of X'

编辑:

对于任何想知道的人,我将在下面 post 我完成的代码。我创建了一种新方法来构建电子邮件消息以减少重复代码

public class SendEmailTasklet implements Tasklet {

    final static Logger LOGGER = LoggerFactory.getLogger(SendEmailTasklet.class);

    @Autowired
    public JavaMailSender emailSender;

    @Override
    public RepeatStatus execute(StepContribution contribution, ChunkContext chunkContext) throws Exception {

        //Get the job info
        JobParameters jobParameters = chunkContext.getStepContext().getStepExecution().getJobParameters();
        ExecutionContext ec = chunkContext.getStepContext().getStepExecution().getJobExecution().getExecutionContext();

        //Get the step info
        JobExecution jobExecutions = chunkContext.getStepContext().getStepExecution().getJobExecution();
        Collection<StepExecution> stepExecution = jobExecutions.getStepExecutions();

        //Get the email address and program name
        String programName = ec.getString(AbstractSetupTasklet.BATCH_PROGRAM_NAME);
        String toEmail = jobParameters.getString("TOEMAIL");


        //If no email address exists, do not send the email.
        if(StringUtils.isEmpty(toEmail)) {

            LOGGER.info("No email address associated with the user.");
            return RepeatStatus.FINISHED;

        }
        else {

            //Check for the first failed step
            for (StepExecution step : stepExecution) {
                if(step.getExitStatus().equals(ExitStatus.FAILED)) {
                    String failedStep = step.getStepName();
                    sendBatchReportEmail(toEmail, programName, failedStep);
                    LOGGER.info(programName + " has failed on the step " + failedStep);
                    break;
                }
            }
            sendBatchReportEmail(toEmail, programName, null);
            LOGGER.info("No email address associated with the user.");
            return RepeatStatus.FINISHED;
        }

    }

    public void sendBatchReportEmail(String toEmail, String programName, String stepName) {
        if(Utils.isEmpty(stepName)) {
            //construct the message
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(toEmail);
            message.setSubject("Batch Reporting");
            message.setText("The batch program " + programName + " has completed.");
            emailSender.send(message);
            LOGGER.info("Email succesfully sent to user at " + toEmail);            
        }
        else {
          //construct the message
            SimpleMailMessage message = new SimpleMailMessage();
            message.setTo(toEmail);
            message.setSubject("Batch Reporting");
            message.setText("The batch program " + programName + " has failed on step: " + stepName);
            emailSender.send(message);
            LOGGER.info("Email succesfully sent to user at " + toEmail + "and has failed on the step: " + stepName);
        }

    }

}

您可以使用 chunkContext.getStepContext().getStepExecution().getJobExecution() 从块上下文访问作业执行。

作业执行后,您可以使用 jobExecution.getStepExecutions() 获取所有步骤执行并遍历它们以检查最后一个失败的步骤。

最后一个失败的 StepExecution 为您提供创建消息所需的步骤名称、退出代码和描述,然后将其添加到作业执行上下文。

希望对您有所帮助。