spring集成如何捕获自定义异常?

pdkcd3nj  于 2021-07-23  发布在  Java
关注(0)|答案(2)|浏览(362)

似乎我不了解spring集成中有关自定义异常处理的一些基本概念(
我需要截获一些runtimeexception派生的自定义异常(从我的一些java方法抛出),并根据其类型将应用程序执行流路由到其他路由。
我使用的是SpringIntegrationversion4.3.10.release。
为此,我宣布int:exception-type-router like 这是:

<int:exception-type-router input-channel="errorChannel" default-output-channel="nullChannel">
        <int:mapping exception-type="com.surr.exception.SurrRoutingException"
                     channel="handleRedirectChannel"/>
        <int:mapping exception-type="com.surr.exception.SurrFatalException"
                     channel="surrChannelMain"/>
        <!-- some more mappings -->
</int:exception-type-router>

频道声明示例:

<int:channel id="handleRedirectChannel"
     datatype="com.surr.exception.SurrRoutingException">
      <int:queue/>
</int:channel>

抛出异常后,我无法进入相关通道。我做错了什么?也许我在应用程序初始设置中错过了smth?
非常感谢您的帮助。
在artem i的注解做出如下更改后更新:
引入了额外的错误通道:

<int:channel id="errorChannel1">
        <int:queue capacity="500"/>
</int:channel>

由于我的消息处理链中的第一条语句覆盖了“errorchannel”头:

<int:header-enricher>
            <int:header name="errorChannel" value="errorChannel1"/>
</int:header-enricher>

修改了int:exception-type-router':

<int:exception-type-router input-channel="errorChannel1" default-output-channel="nullChannel">
        <int:mapping exception-type="java.lang.Throwable"
                     channel="handleRedirectChannel"/>
        <int:mapping exception-type="java.lang.RuntimeException"
                     channel="handleRedirectChannel"/>
        <int:mapping exception-type="com.surr.exception.SurrRoutingException"
                     channel="handleRedirectChannel"/>
        <int:mapping exception-type="com.surr.exception.SurrFatalException"
                     channel="surrChannelMain"/>
</int:exception-type-router>

更改了收件人通道的声明:

<int:channel id="handleRedirectChannel">
     <int:queue capacity="50"/>
</int:channel>

<int:channel id="surrChannelMain">
     <int:queue capacity="50"/>
</int:channel>

在我的测试中,我开始沿着handleredirectchannel频道“监听”errorchannel1
到目前为止,还没有我抛出异常的任何踪迹。我怀疑存在更多的规则来捕获异常,但无法找出它们
还有一次更新,我使用messagebuilder(messagebuilder.withpayload(new myExceptionObject WithNeededArguments).build())手动发出了异常,并将其发送到“errorchannel1”。还是没有效果(
堆栈跟踪

2021-02-17 23:03:48,742  INFO [   task-scheduler-20             ] [] !! IntentsService.prepareMessages
com.azoft.fakturachat.surrogate.exception.SurrogateRoutingException: Got empty intent description by scenario: [default] and tip: [some_unknown_intent]
    at com.azoft.fakturachat.surrogate.IntentsService.fetchIntentDescription(IntentsService.java:84) ~[main/:?]
    at com.azoft.fakturachat.surrogate.IntentsService.lambda$prepareMessages$0(IntentsService.java:58) ~[main/:?]
    at java.util.stream.ReferencePipeline$3$1.accept(ReferencePipeline.java:193) ~[?:1.8.0_172]
    at java.util.Collections$2.tryAdvance(Collections.java:4717) ~[?:1.8.0_172]
    at java.util.Collections$2.forEachRemaining(Collections.java:4725) ~[?:1.8.0_172]
    at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481) ~[?:1.8.0_172]
    at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471) ~[?:1.8.0_172]
    at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708) ~[?:1.8.0_172]
    at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234) ~[?:1.8.0_172]
    at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499) ~[?:1.8.0_172]
    at com.azoft.fakturachat.surrogate.IntentsService.prepareMessages(IntentsService.java:59) ~[main/:?]
    at com.azoft.fakturachat.surrogate.IntentsService$$FastClassBySpringCGLIB$$d29d5141.invoke(<generated>) ~[main/:?]
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:721) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.aspectj.MethodInvocationProceedingJoinPoint.proceed(MethodInvocationProceedingJoinPoint.java:85) ~[spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.azoft.fakturachat.aspect.LoggingAspect.logMethod(LoggingAspect.java:31) [main/:?]
    at sun.reflect.GeneratedMethodAccessor131.invoke(Unknown Source) ~[?:?]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_172]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_172]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethodWithGivenArgs(AbstractAspectJAdvice.java:629) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.aspectj.AbstractAspectJAdvice.invokeAdviceMethod(AbstractAspectJAdvice.java:618) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.aspectj.AspectJAroundAdvice.invoke(AspectJAroundAdvice.java:70) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:92) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:656) [spring-aop-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at com.azoft.fakturachat.surrogate.IntentsService$$EnhancerBySpringCGLIB$$9cfa94da.prepareMessages(<generated>) [main/:?]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_172]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_172]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_172]
    at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_172]
    at org.springframework.expression.spel.support.ReflectiveMethodExecutor.execute(ReflectiveMethodExecutor.java:113) [spring-expression-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.getValueInternal(MethodReference.java:129) [spring-expression-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference.access$000(MethodReference.java:49) [spring-expression-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.expression.spel.ast.MethodReference$MethodValueRef.getValue(MethodReference.java:347) [spring-expression-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.expression.spel.ast.CompoundExpression.getValueInternal(CompoundExpression.java:88) [spring-expression-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.expression.spel.ast.SpelNodeImpl.getTypedValue(SpelNodeImpl.java:131) [spring-expression-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.expression.spel.standard.SpelExpression.getValue(SpelExpression.java:330) [spring-expression-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:169) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.util.AbstractExpressionEvaluator.evaluateExpression(AbstractExpressionEvaluator.java:128) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.ExpressionEvaluatingMessageProcessor.processMessage(ExpressionEvaluatingMessageProcessor.java:72) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.transformer.support.ExpressionEvaluatingHeaderValueMessageProcessor.processMessage(ExpressionEvaluatingHeaderValueMessageProcessor.java:71) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.transformer.HeaderEnricher.transform(HeaderEnricher.java:119) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.transformer.MessageTransformingHandler.handleRequestMessage(MessageTransformingHandler.java:89) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:109) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.MessageHandlerChain.handleMessageInternal(MessageHandlerChain.java:110) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.dispatcher.AbstractDispatcher.tryOptimizedDispatch(AbstractDispatcher.java:116) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.dispatcher.UnicastingDispatcher.doDispatch(UnicastingDispatcher.java:148) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.dispatcher.UnicastingDispatcher.dispatch(UnicastingDispatcher.java:121) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.channel.AbstractSubscribableChannel.doSend(AbstractSubscribableChannel.java:89) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:423) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.channel.AbstractMessageChannel.send(AbstractMessageChannel.java:373) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.integration.router.AbstractMessageRouter.handleMessageInternal(AbstractMessageRouter.java:194) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:129) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:292) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:212) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:129) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:129) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:292) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:212) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:129) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageHandler.handleMessage(AbstractMessageHandler.java:127) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.MessageHandlerChain$1.send(MessageHandlerChain.java:129) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:115) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.GenericMessagingTemplate.doSend(GenericMessagingTemplate.java:45) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.messaging.core.AbstractMessageSendingTemplate.send(AbstractMessageSendingTemplate.java:105) [spring-messaging-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutput(AbstractMessageProducingHandler.java:292) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.produceOutput(AbstractMessageProducingHandler.java:212) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractMessageProducingHandler.sendOutputs(AbstractMessageProducingHandler.java:129) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.AbstractReplyProducingMessageHandler.handleMessageInternal(AbstractReplyProducingMessageHandler.java:115) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.DelayHandler.doReleaseMessage(DelayHandler.java:376) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.DelayHandler.access$500(DelayHandler.java:83) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.DelayHandler$ReleaseMessageHandler.handleMessage(DelayHandler.java:470) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.DelayHandler.releaseMessage(DelayHandler.java:368) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.DelayHandler.access$100(DelayHandler.java:83) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.integration.handler.DelayHandler$1.run(DelayHandler.java:328) [spring-integration-core-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54) [spring-context-4.3.7.RELEASE.jar:4.3.7.RELEASE]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [?:1.8.0_172]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [?:1.8.0_172]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180) [?:1.8.0_172]
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293) [?:1.8.0_172]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149) [?:1.8.0_172]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624) [?:1.8.0_172]
    at java.lang.Thread.run(Thread.java:748) [?:1.8.0_172]
2021-02-17 23:03:48,751  INFO [   task-scheduler-20             ] [] <- IntentsService.prepareMessages, procTime=306

“errorchannel1”通道的引入只是为了测试这个问题。除此“例外类型路由器”外,没有其他使用者
异常按以下方式“抛出”:

SurrRoutingException ex1 = new SurrRoutingException(
                MessageFormat.format("Failed to find intent description by scenario: [{0}] and tip: [{1}]",
                    scenario, tip), context);
errorChannel.send(MessageBuilder.withPayload(ex1).build());

错误通道声明:

private final QueueChannel errorChannel;

@Autowired
public IntentsService(@Qualifier("errorChannel1") QueueChannel errorChannel) 
{
        this.errorChannel = errorChannel;
}
eufgjt7s

eufgjt7s1#

我想你的困惑是因为 <int:exception-type-router> 可以处理堆栈跟踪来跟踪预期的异常,但是 datatype 在信道上是负载的确切类型。
有关详细信息,请参阅文档:
https://docs.spring.io/spring-integration/docs/current/reference/html/message-routing.html#router-实现例外路由器
https://docs.spring.io/spring-integration/docs/current/reference/html/core.html#channel-数据类型通道
我想说你不需要 datatype 为了这个 handleRedirectChannel 因为你只能从路由器上用。重点 datatype 实际上是为了限制这个通道的生产者,这在我们的应用程序足够复杂的时候是有意义的,甚至在我们不能控制生产者的情况下也可以在分布式状态下工作。它看起来不像是一个例外路由器的情况。。。

nwo49xxi

nwo49xxi2#

我又回到这个问题上来了。尚未制作示例项目,但在文档中找到了一些信息。”si error handling“:”这里要理解的最重要的一点是,基于消息传递的错误处理仅适用于由在taskexecutor中执行的spring集成任务引发的异常。这不适用于处理程序引发的异常,该处理程序与发送方在同一线程内运行(例如,通过本节前面所述的directchannel)。
也许这就是问题所在?我的应用程序逻辑在不同的链和路由之间被拆分,可能其中一些不是在taskexecutor中执行的,因此全局拦截不起作用?我假设对整个应用程序使用一个全局范围的拦截器是不够的。我要验证这个假设。谢谢!

相关问题