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