Spring Batch如何在Process Listner中获取JobExecution对象

时间:2017-09-18 17:25:54

标签: spring-batch

我在项目中要求在ItemProccesor中发生什么异常需要在JobExecution上下文中存储Exception,在JobExecution结束时发送Exceptional记录的邮件但是如何在processListner中获取JobExecution对象?  我尝试在processListner中使用@beforestep,但是JobExecution对象为null有没有办法在进程Listner中获取JobExecution上下文

1 个答案:

答案 0 :(得分:0)

我在Spring批处理中遇到了上述问题的解决方案,需要在进程监听器中指定jobscope并在listner类代码中访问作业执行上下文。

 @Bean
    @JobScope
    public CaliberatedProcessorListener calibratedProcessorListener() {

        return new CaliberatedProcessorListener();
    }
public class CaliberatedProcessorListener <T, S> implements ItemProcessListener<T, S> {

    @Value("#{jobExecution}")
    public JobExecution jobExecution;

    @Override
    public void beforeProcess(T calibratedProessorInPut) {

        // // do nothing
    }

    @Override
    public void afterProcess(T calibratedProessorInput, S calibratedProessorOutPut) {

        // do nothing
    }

    @Override
    public void onProcessError(T item, Exception calibratedProcessorEx) {

        FtpEmailData ftpEmailData = (FtpEmailData) jobExecution.getExecutionContext().get("calDeviceBatchInfo");
        ftpEmailData.getExceptionList().add(new CalibratedDeviceException(calibratedProcessorEx.getMessage()));
    }

}