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

Spring集成。如何捕获自定义异常?,spring,spring-integration,spring-integration-dsl,Spring,Spring Integration,Spring Integration Dsl,似乎我不了解Spring集成中自定义异常处理的一些基本概念( 我需要截获一些RuntimeException派生的自定义异常(从Java方法抛出),并根据其类型将应用程序执行流路由到其他路由 我使用的是Spring集成版本4.3.10.0 为此,我声明int:exception类型路由器如下: <int:exception-type-router input-channel="errorChannel" default-output-channel="null

似乎我不了解Spring集成中自定义异常处理的一些基本概念(

我需要截获一些RuntimeException派生的自定义异常(从Java方法抛出),并根据其类型将应用程序执行流路由到其他路由

我使用的是Spring集成版本4.3.10.0

为此,我声明int:exception类型路由器如下:

<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>
错误通道声明:

private final QueueChannel errorChannel;

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

我认为您的困惑是因为
可以处理堆栈跟踪以跟踪预期的异常,但通道上的
数据类型是负载的确切类型

有关更多信息,请参阅文档:


我想说,你不需要一个
数据类型
用于
HandlerRedirectChannel
,因为你只从那个路由器上使用它。
数据类型
的目的实际上是限制这个通道的生产者,当我们的应用程序足够复杂时,这是有意义的,当我们无法控制生产者时,它甚至可以在分布式状态下工作。对于您的异常路由器来说,这看起来不像是一个案例…

我又回到了这个问题。还没有制作一个示例项目,但在文档中找到了一些信息。“SI错误处理”:“这里需要了解的最重要的一点是,基于消息传递的错误处理仅适用于在TaskExecutor中执行的Spring集成任务引发的异常。这不适用于在与发送方相同的线程中运行的处理程序引发的异常(例如,通过本节前面介绍的DirectChannel)“


也许这就是问题所在?我的应用程序逻辑被划分在不同的链和路由之间,可能其中一些不是在TaskExecutor中执行的,因此全局拦截不起作用?我假设每个应用程序只有一个全局范围的拦截器是不够的。我将验证这个假设。谢谢!

Yo你可以而且应该编辑你的问题,将你认为可能有助于他人回答问题的任何信息包括在内。如果你可以将评论中的信息移到你的问题中,那就更好了。在发送后,你可能会有一些错误,所以在你的问题中看到这一点会很好。事实上,没有。我在单元测试中运行了我的链,在测试结束时,我尝试重新分析d来自通道的消息。其他测试没有异常运行正常,但此测试失败:路由未发生Hi Artem!非常感谢您的建议。不幸的是,它没有帮助(看来拦截机制根本不起作用,或者不知怎么被破坏了。我将更新最初的帖子以反映我最近的更新。仍然不知道你的应用程序发生了什么。你有没有可能与我们共享一个简单的项目,让我们复制和使用它?这样我们就可以调试你的流程,看看有什么问题。)继续。很难保证。事实上,我正在开发企业应用程序,当然不能分享。准备类似的项目样本需要一些时间,现在我因为这个问题超出了项目时间限制((.也许您知道一些调试/诊断提示,有助于找出直接发送到“errorChannel1”的消息丢失的确切位置和原因?感谢您的帮助您可以打开
org.springframework.integration
类别的调试日志记录级别,查看消息在流中的传播方式。您可以在
Er中设置断点rorMessageExceptionTypeRouter.getChannelKeys()
查看您的邮件发生了什么。您的日志中可能已经有一些堆栈跟踪,您没有与我们共享以获取更多信息。您在配置和解释中显示的内容必须正常工作。我们缺少一些您没有与我们共享的详细信息。可能您在该
errorChannel1
上有更多的消费者,因此您的邮件在那里旅行-不是到这个路由器。等等。你混合了太多的东西和一点旁道。如果你能提供一个简单的项目来玩就太好了。但正如你所解释的,它确实会失败。也许随着婴儿步骤的复制,你会在你的项目中看到一些你目前没有与我们分享的东西
<int:channel id="errorChannel1">
        <int:queue capacity="500"/>
</int:channel>
<int:header-enricher>
            <int:header name="errorChannel" value="errorChannel1"/>
</int:header-enricher>
<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>
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
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;
}