配置死信队列。 jms 消费者的 DLQ

Configure dead letter queue. DLQ for a jms consumer

这是一个相当简单的问题...我有一个使用队列 (CONSUMER) 的 spring 项目。 现在我想为我正在使用的每个队列配置单独的死信队列。

但是,在我看来,各个死信队列的配置必须在代理服务(SERVER)中完成,而不是在 CONSUMER 中完成。真的是这样吗?

我下面的代码将不起作用,对吗?

@Bean
public DeadLetterStrategy deadLetterStrategy(){
    IndividualDeadLetterStrategy dlq = new IndividualDeadLetterStrategy();
    dlq.setQueueSuffix(".DLQ");
    dlq.setUseQueueForQueueMessages(true);
    return dlq;
}

@Bean
public ActiveMQConnectionFactory consumerActiveMQConnectionFactory() {
    var activeMQConnectionFactory = new ActiveMQConnectionFactory();
    activeMQConnectionFactory.setBrokerURL(brokerUrl);

    RedeliveryPolicy policy = activeMQConnectionFactory.getRedeliveryPolicy();
    policy.setMaximumRedeliveries(maximumRedeliveries);
    policy.setInitialRedeliveryDelay(0);
    policy.setBackOffMultiplier(3);
    policy.setUseExponentialBackOff(true);

    return activeMQConnectionFactory;
}

@Bean
public DefaultJmsListenerContainerFactory jmsListenerContainerFactory() {
    var factory = new DefaultJmsListenerContainerFactory();
    factory.setSessionAcknowledgeMode(JmsProperties.AcknowledgeMode.CLIENT.getMode());
    factory.setConcurrency(factoryConcurrency);
    factory.setConnectionFactory(consumerActiveMQConnectionFactory());

    return factory;
}

@Bean
public BrokerService broker() throws Exception {
    final BrokerService broker = new BrokerService();
    broker.addConnector(brokerUrl);
    broker.setPersistent(false);
    broker.setDestinationPolicy(policyMap());
    return broker;
}

@Bean
public PolicyMap policyMap() {
    PolicyMap destinationPolicy = new PolicyMap();
    List<PolicyEntry> entries = new ArrayList<PolicyEntry>();
    PolicyEntry queueEntry = new PolicyEntry();
    queueEntry.setQueue(">"); // In activemq '>' does the same thing as '*' does in other languages
    queueEntry.setDeadLetterStrategy(deadLetterStrategy());
    entries.add(queueEntry);
    destinationPolicy.setPolicyEntries(entries);
    return destinationPolicy;
} }
@JmsListener(destination = "myqueue")
    public void onMessage(Message message, Session session) throws JMSException {
        try {
            stuff()
            message.acknowledge();
        } catch (Exception ex) {
            session.recover();
        }
    }

ActiveMQ 5.x 中的 JMS 消费者无法配置代理方死信策略,这必须在配置 XML 中的代理处或通过程序化代理配置完成。如果您的代理只是一个内存代理,您可以在 spring 中配置它,但是这对大多数应用程序几乎没有用处。

有关代理配置的更多帮助,请参阅 broker documentation