首页 文章

使用特定于路由的DLQ配置Java / Camel / AMQ

提问于
浏览
0

这里是Java 8 / Camel 2.19.x / AMQ 5.15.x.

我有一个Java应用程序,它使用Camel消耗AMQ队列中的消息,处理这些消息,并对它们进行处理 . 有时,路由的输出将处理结果重新放回另一个队列以进行进一步的下游处理,但不总是/必然 . 典型的Java / Camel / AMQ设置 .

我的每个路由(我使用的是Camel XML DSL)都有一个配置好的 <onException> 处理程序,通常如下所示:

<onException useOriginalMessage="true">
  <exception>java.lang.Exception</exception>

  <redeliveryPolicy logStackTrace="true"/>

  <handled>
    <constant>true</constant>
  </handled>

  <log message="${exception.stacktrace}" loggingLevel="ERROR"/>

  <rollback markRollbackOnly="true"/>

</onException>

非常简单:记录异常并回滚 .

我想做的是,作为这个 <onException> 处理程序的一部分,在特定于路由的DLQ上放置 original message (失败并导致异常被抛出, not 异常!)(通过"DLQ"我的意思是只是一个队列,可以发送失败的消息以用于审计/报告/回放目的)

这意味着,如果我的应用程序有30个路由,每个路由消耗30个不同的AMQ队列,我将有30个不同的"DLQs",其中每个 <onException> 处理程序将发送失败的消息 .

理想情况下,我希望这个配置在AMQ方面(也许可能在 activem.xml 或类似内部),所以我也没有't need to make code changes or redeploys if the DLQ destinations need to change. But if its only possible to do from inside the Camel route/configs, that'罚款 .

我想我可以修改每个路由以包含原始消息的自定义目标DLQ:

<onException useOriginalMessage="true">
  <exception>java.lang.Exception</exception>

  <redeliveryPolicy logStackTrace="true"/>

  <handled>
    <constant>true</constant>
  </handled>

  <log message="${exception.stacktrace}" loggingLevel="ERROR"/>

  <rollback markRollbackOnly="true"/>

  <to uri="activemq:fizzbuzz.dlq"/>

</onException>

但是我希望有比这更优雅的东西......

我有什么想法可以做到这一点?

1 回答

  • 1

    可能这样对你来说会更好:

    DeadLetterChannelBuilder errorHandlerBuilder = deadLetterChannel("jms:dummy");
        errorHandlerBuilder.onPrepareFailure(exchange -> {
            exchange.getIn().setHeader("CamelJmsDestinationName",exchange.getIn().getHeader("JMSDestination",String.class).concat(".DLQ"));
        });
    
        from("jms:input1")
                .to("seda:process");
    
        from("jms:input2")
                .to("seda:process");
    
        from("jms:input3")
                .to("seda:process");
    
        from("seda:process").errorHandler(errorHandlerBuilder)
                .process(exchange -> {
                    throw new RuntimeException();
                });
    

    您可以在运行时计算DLQ队列名称 . DeadLetterChannelBuilder也可以像onException一样进行配置 .

相关问题