gpt4 book ai didi

java - spring batch自定义退出描述

转载 作者:行者123 更新时间:2023-11-29 04:30:14 26 4
gpt4 key购买 nike

我们有一个通过方法调用执行的 spring 批处理作业,我们使用以下代码发回作业的退出状态(如果发生错误):

          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"}

上面是一个字符串,它也作为退出描述存储在 job_execution_context 中,因此 spring batch 响应相同。

我需要为客户提供一条简洁的可呈现消息,如“文件验证失败”。

然而,通过对当前退出描述进行字符串操作是可能的,有什么方法可以覆盖框架的这种行为,在这种情况下,可以从异常中取出消息而不是发送整个堆栈跟踪。任何指针将不胜感激。

最佳答案

您可以为您的步骤注册一个 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
}

}

关于java - spring batch自定义退出描述,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44047480/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com