java—当ApacheCamel拦截器抛出一个异常时出现不适当的异常

q9yhzks0  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(421)

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

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

我希望申请通知的路线:

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\u cathed属性包含的不是我抛出的异常,而是 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$8(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$1.doInProducer(ProducerCache.java:529)
    at org.apache.camel.impl.ProducerCache$1.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$1.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$1.evaluate(TestWatcher.java:55)
    at org.junit.rules.TestWatcher$1.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$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.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$1.doInProducer(ProducerCache.java:529)
    at org.apache.camel.impl.ProducerCache$1.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

所有可能理解的错误都发生在方法中 public boolean process(final Exchange exchange, final AsyncCallback callback)org.apache.camel.processor.FatalFallbackErrorHandler 当试图在deque中输入null值时。
如何在错误处理程序中获得所需的异常?

jslywgbw

jslywgbw1#

interceptSendToEndpoint("direct:PsMessageLogger_updateMessage") 查找实际字符串 direct:PsMessageLogger_updateMessage 试图拦截时。在你的 AnalisingResponse 路由,你在你的处理器中动态地做它,这就是为什么它不能拦截。
在您的第三个处理器中 AnalisingResponse ,删除生产者模板并设置 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");

相关问题