我们有一个弹出批处理作业,通过方法调用执行,我们使用以下代码发送作业的退出状态(如果发生错误):
JobExecution jeStatus = jobExplorer.getJobExecution(je.getId()); String exitDescription = jeStatus.getExitStatus().getExitDescription(); return exitDescription;
它显示来自传播异常的整个堆栈跟踪。 例如。, 我们的 ItemReader 会抛出一些处理和重新抛出的异常,以便获得一些自定义消息:
try { //Some code here which fails validation }catch(Exception e) { throw new Exception("File validation failed"); }
那么返回给客户的消息是:
> causes:org.springframework.beans.factory.BeanCreationException: Error
> creating bean with name 'scopedTarget.prjItemReader' defined in class
> path resource [com/org/prjcore/config/ImportJobConfiguration.class]:
> Bean instantiation via factory method failed; nested exception is
> org.springframework.beans.BeanInstantiationException: Failed to
> instantiate [org.springframework.batch.item.xml.StaxEventItemReader]:
> Factory method 'prjItemReader' threw exception; nested exception is
> java.lang.Exception: File validation failed \r\n\tat
> org.springframework.beans.factory.support.ConstructorResolver.instantiateUsingFactoryMethod(ConstructorResolver.java:599)\r\n\tat
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.instantiateUsingFactoryMethod(AbstractAutowireCapableBeanFactory.java:1128)\r\n\tat
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1023)\r\n\tat
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:510)\r\n\tat
> org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482)\r\n\tat
> org.springframework.beans.factory.support.AbstractBeanFactory$2.getObject(AbstractBeanFactory.java:345)\r\n\tat
> org.springframework.batch.core.scope.StepScope.get(StepScope.java:113)\r\n\tat
> org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:340)\r\n\tat
> org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:197)\r\n\tat
> org.springframework.aop.target.SimpleBeanTargetSource.getTarget(SimpleBeanTargetSource.java:35)\r\n\tat
> org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.getTarget(CglibAopProxy.java:687)\r\n\tat
> org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:637)\r\n\tat
> org.springframework.batch.item.xml.StaxEventItemReader$$EnhancerBySpringCGLIB$$c6888d6d.open()\r\n\tat
> org.springframework.batch.item.support.CompositeItemStream.open(CompositeItemStream.java:96)\r\n\tat
> org.springframework.batch.core.step.tasklet.TaskletStep.open(TaskletStep.java:310)\r\n\tat
> org.springframework.batch.core.step.AbstractStep.execute(AbstractStep.java:197)\r\n\tat
> org.springframework.batch.core.job.SimpleStepHandler.handleStep(SimpleStepHandler.java:148)\r\n\tat
> org.springframework.batch.core.job"}
上面是一个String,它也作为退出描述存储在job_execution_context中,因此spring批处理响应相同。
我需要为客户提供一个整洁的可呈现消息""文件验证失败"。
然而,通过对当前退出描述进行字符串操作可能是有可能的,是否有任何方法可以覆盖框架的这种行为,其中消息可以从异常中取出而不是发送整个堆栈跟踪。任何指针都会受到赞赏。
答案 0 :(得分:3)
您可以为步骤注册StepExecutionListener
以执行您正在寻找的内容。
public class CustomStepExecutionListener implements StepExecutionListener {
private static final String VALIDATION_FAILURE = "File validation failed";
public ExitStatus afterStep(final StepExecution stepExecution) {
ExitStatus exitStatus = stepExecution.getExitStatus();
String exitCode = exitStatus.getExitCode();
if (ExitStatus.FAILED.getExitCode().equals(exitCode)) {
String exitDescription = exitStatus.getExitDescription();
if (exitDescription.contains(VALIDATION_FAILURE)) {
return new ExitStatus(exitCode, VALIDATION_FAILURE);
}
}
return null;
}
public void beforeStep(final StepExecution stepExecution) {
//no-op
}
}