当 apache 骆驼拦截器抛出一个异常时不适当的异常

Inappropriate exception when apache camel interceptor throws one

我创建了一个拦截器来测试 Camel 路由中的异常处理程序。 这是一个拦截器:

context.getRouteDefinition("AnalisingResponse").adviceWith(context,
            new AdviceWithRouteBuilder() {
                @Override
                public void configure() {
                    interceptSendToEndpoint("direct:PsMessageLogger_updateMessage")
                        .routeId("interceptor")
                        .skipSendToOriginalEndpoint()
                        .throwException(exception);
                }
            });

我希望申请的路线 adviceWith:

      from("direct:AnalisingResponse")
            .routeId("AnalisingResponse")
            .log("start PsASetStatementProcessor_AnalisingResponse")
            .process(exchange -> {
                exchange.setProperty(CONDITION,
                    bodyDataContainer(exchange)
                        .getValue("/*[1]/Body/container/Msg/BSMessage/BSError/@Cod")  // TODO local-name
                );
            })
            .process((BaseAction) exchange -> {
                DataContainer localBody = propertyDataContainer(exchange, LOCAL_BODY);

                Long serviceId = Long.parseLong(localBody.getValue("/Message/ServiceID"));
                Long taskId = Long.parseLong(localBody.getValue("/Message/TaskID"));
                Long statusId = 39L;
                Long mtType = 0L;

                String requestVar = bsDbSwiftCorpService.updateTask(taskId, serviceId, mtType, statusId);

                exchange.setProperty(REQUEST_VAR, requestVar);
            })
            .process((BaseAction) exchange -> {
                DataContainer localBody = propertyDataContainer(exchange, LOCAL_BODY);
                localBody.replace("./Message/MessageStatusID", "3");
                exchange.setProperty(LOCAL_BODY, localBody);

                template.sendBody(PsMessageLogger.DIRECT_ROUTE_UPDATE_MESSAGE, localBody.copy());
            });

这是一条路线,我想拦截:

        from("direct:PsMessageLogger_updateMessage")
                .routeId("PsMessageLogger_updateMessage")
                .log("start PsMessageLogger_updateMessage")
                .onException(Exception.class)
                    .handled(true)
                    .to("direct:PsMessageLogger_updateMessage_errorHandler")
                .end()
                .process((BaseAction) exchange -> {
                    DataContainer body = bodyDataContainer(exchange);

                    Long messageID = new Long(body.getValue("/Message/MessageID"));
                    Long messageStatusID = new Long(body.getValue("/Message/MessageStatusID"));
                    Long taskID = new Long(body.getValue("/Message/TaskID"));
                    Long taskStatusGenID = new Long(body.getValue("/Message/TaskStatusGenID"));

                    bsMessageLoggerToDbService.updateMessage(messageID, messageStatusID, taskID, taskStatusGenID);
                });

但结果是 EXCEPTION_CAUGHT 属性 不包含我抛出的异常,而是 org.apache.camel.CamelExecutionException 原因是 NullPointerException。这是它的堆栈跟踪:

org.apache.camel.CamelExecutionException: Exception occurred during execution on the exchange: Exchange[ID-SERVO-1598117736905-0-3]
    at org.apache.camel.util.ObjectHelper.wrapCamelExecutionException(ObjectHelper.java:1842)
    at org.apache.camel.util.ExchangeHelper.extractResultBody(ExchangeHelper.java:715)
    at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:515)
    at org.apache.camel.impl.DefaultProducerTemplate.extractResultBody(DefaultProducerTemplate.java:511)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:163)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:168)
    at ru.vtb.swiftcorp.psASetStatementProcessor.proxy.services.PsASetStatementProcessor.lambda$configure(PsASetStatementProcessor.java:149)
    at ru.vtb.swiftcorp.common.BaseAction.process(BaseAction.java:30)
    at org.apache.camel.processor.DelegateSyncProcessor.process(DelegateSyncProcessor.java:63)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
    at org.apache.camel.processor.ChoiceProcessor.process(ChoiceProcessor.java:117)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:76)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:76)
    at org.apache.camel.processor.SendProcessor.process(SendProcessor.java:148)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:548)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:138)
    at org.apache.camel.processor.Pipeline.process(Pipeline.java:101)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.component.direct.DirectProducer.process(DirectProducer.java:76)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:186)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:86)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:529)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:494)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:369)
    at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:494)
    at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:229)
    at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:144)
    at org.apache.camel.builder.DefaultFluentProducerTemplate.send(DefaultFluentProducerTemplate.java:306)
    at ru.vtb.swiftcorp.psASetStatementProcessor.proxy.services.PsASetStatementProcessorTest.testSuccessRoute_caseExceptionOnPsMessageLogger_updateMessage(PsASetStatementProcessorTest.java:546)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.junit.runners.model.FrameworkMethod.runReflectiveCall(FrameworkMethod.java:50)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.internal.runners.statements.RunAfters.evaluate(RunAfters.java:27)
    at org.junit.rules.TestWatcher.evaluate(TestWatcher.java:55)
    at org.junit.rules.TestWatcher.evaluate(TestWatcher.java:55)
    at org.junit.rules.RunRules.evaluate(RunRules.java:20)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access[=13=]0(ParentRunner.java:58)
    at org.junit.runners.ParentRunner.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:220)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:53)
Caused by: java.lang.NullPointerException
    at java.util.ArrayDeque.addFirst(ArrayDeque.java:233)
    at java.util.ArrayDeque.push(ArrayDeque.java:508)
    at org.apache.camel.processor.FatalFallbackErrorHandler.process(FatalFallbackErrorHandler.java:79)
    at org.apache.camel.processor.RedeliveryErrorHandler.deliverToFailureProcessor(RedeliveryErrorHandler.java:1063)
    at org.apache.camel.processor.RedeliveryErrorHandler.process(RedeliveryErrorHandler.java:474)
    at org.apache.camel.processor.CamelInternalProcessor.process(CamelInternalProcessor.java:201)
    at org.apache.camel.processor.DelegateAsyncProcessor.process(DelegateAsyncProcessor.java:97)
    at org.apache.camel.impl.InterceptSendToEndpointProcessor.process(InterceptSendToEndpointProcessor.java:80)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:186)
    at org.apache.camel.processor.SharedCamelInternalProcessor.process(SharedCamelInternalProcessor.java:86)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:529)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:494)
    at org.apache.camel.impl.ProducerCache.doInProducer(ProducerCache.java:369)
    at org.apache.camel.impl.ProducerCache.sendExchange(ProducerCache.java:494)
    at org.apache.camel.impl.ProducerCache.send(ProducerCache.java:229)
    at org.apache.camel.impl.DefaultProducerTemplate.send(DefaultProducerTemplate.java:144)
    at org.apache.camel.impl.DefaultProducerTemplate.sendBody(DefaultProducerTemplate.java:161)
    ... 66 more

所有可能理解的错误发生在 org.apache.camel.processor.FatalFallbackErrorHandler 的方法 public boolean process(final Exchange exchange, final AsyncCallback callback) 中,当试图将 null 值放入双端队列时。

如何在错误处理程序中获取我需要的异常?

interceptSendToEndpoint("direct:PsMessageLogger_updateMessage") 在尝试拦截时寻找实际的字符串 direct:PsMessageLogger_updateMessage。在您的 AnalisingResponse 路由中,您是在处理器中动态执行的,这就是它无法拦截的原因。

在您 AnalisingResponse 的第 3 个处理器中,删除生产者模板并将 localBody 设置为您的消息正文,如下所示:exchange.getIn().setBody(localBody)。然后在process方法后面,加一个to("direct:PsMessageLogger_updateMessage").

您的 AnalisingResponse 路线应如下所示:

from("direct:AnalisingResponse")
    .routeId("AnalisingResponse")
    .log("start PsASetStatementProcessor_AnalisingResponse")
    .process(exchange -> {
        //...
    })
    .process((BaseAction) exchange -> {
        //...
    })
    .process((BaseAction) exchange -> {
        DataContainer localBody = propertyDataContainer(exchange, LOCAL_BODY);
        localBody.replace("./Message/MessageStatusID", "3");
        exchange.setProperty(LOCAL_BODY, localBody);
        
        exchange.getIn().setBody(localBody);
    })
    .to("direct:PsMessageLogger_updateMessage");