Camel Route 中的错误处理程序重试次数超过配置值

Errorhandler in Camel Route retrying more times than configured value

我已经设置了一个测试用例,我希望消息在发生异常后进入死信通道,重试 2 次以处理它:

public class TestObjectRoutes extends SpringRouteBuilder {
    @Override
    @Transactional
    public void configure() {
        errorHandler(transactionErrorHandler().logHandled(true)
                .onRedelivery(exchange -> System.out.println("Testing..."))
                .maximumRedeliveries(2));

        from("activemq:queueone")
                .transacted()
                .to("activemq:queuetwo")
                .process(javaThrower); // This line throws an exception
        ;
    }
}

调用路由后我可以看到以下内容:

Testing...
Testing...

2016-03-17 15:59:32.762 ERROR 3296 --- [testobject.one]] o.a.camel.processor.DeadLetterChannel    : Failed delivery for (MessageId: ID:DESKTOP-HRMD8N6-64204-1458226711533-21:2:1:1:1 on ExchangeId: ID-DESKTOP-HRMD8N6-64190-1458226703758-0-8). Exhausted after delivery attempt: 3 caught: java.lang.IllegalArgumentException: JAVA EXCEPTION. Processed by failure processor: FatalFallbackErrorHandler[sendTo(Endpoint[activemq://mydeadletterchannel] InOnly)]

这看起来和我期待的一模一样。

唯一的问题是它不止于此。它不断重试呼叫,我看到这 2 条日志行和 ERROR 7

然后它将消息移动到 DLQ,但是,它不是我定义的 DLQ 队列名称,而是移动到 "ACTIVEMQ.DLQ"。

这让我觉得另一个默认配置正在接管其他地方,但我似乎无法确定它。

这是我的 ActiveMQComponent 配置:

@Autowired
private PlatformTransactionManager platformTransactionManager;

@PostConstruct
public void init() throws Exception {

    ActiveMQComponent component = ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false");
    component.setTransactionManager(platformTransactionManager);
    component.setTransacted(true);

    camelContext.addComponent("activemq", component);
}

有谁知道为什么会发生这种情况?

编辑:

添加自定义连接工厂后,我的代码如下所示:

ActiveMQComponent component = ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false");
    component.setTransactionManager(platformTransactionManager);
    component.setTransacted(true);

    ActiveMQConnectionFactory connectionFactory = new ActiveMQConnectionFactory();
    RedeliveryPolicy redeliveryPolicy = new RedeliveryPolicy();
    redeliveryPolicy.setMaximumRedeliveries(0);
    connectionFactory.setRedeliveryPolicy(redeliveryPolicy);
    connectionFactory.setBrokerURL("vm://localhost?broker.persistent=false");
    component.setConnectionFactory(connectionFactory);

    camelContext.addComponent("activemq", component);

camel-jms 的默认 retry/redelivery 是 6,所以这就是为什么您会收到 2 条消息和 7 次错误。

使用 camel-jms tests createConnectionFactory() specifying that maximum redeliveries you want and pass this connection factory into your camel component. If using spring then the transactional client docs 中的连接工厂创建方法来构建连接工厂和 ActiveMQComponents 的示例,您只需向连接工厂 bean 添加一个重新传递策略 bean。

在 camel-jms 组件测试中还有其他使用事务和配置 JMS 消息重新传递的好例子。