ActiveMQ的消息重发策略和DLQ处理
在以下三种情况中,ActiveMQ消息会被重发给客户端/消费者:
1.使用一个事务session,并且调用了rollback()方法;
2.一个事务session,关闭之前调用了commit;
3.在session中使用CLIENT_ACKNOWLEDGE签收模式,并且调用了Session.recover()方法。
Broker根据自己的规则,通过BrokerInfo命令包和客户端建立连接,向客户端传送缺省发送策略。但是客户端可以使用ActiveMQConnection.getRedeliveryPolicy()方法覆盖override这个策略设置。
RedeliveryPolicy policy = connection.getRedeliveryPolicy();policy.setInitialRedeliveryDelay(500);policy.setBackOffMultiplier(2);policy.setUseExponentialBackOff(true);policy.setMaximumRedeliveries(2);
<broker...> <destinationPolicy> <policyMap> <policyEntries> <!-- Set the following policy on all queues using the '>' wildcard --> <policyEntry queue=">"> <deadLetterStrategy> <!-- Use the prefix 'DLQ.' for the destination name, and make the DLQ a queue rather than a topic --> <individualDeadLetterStrategy queuePrefix="DLQ." useQueueForQueueMessages="true" /> </deadLetterStrategy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy> ...</broker>
<broker...> <destinationPolicy> <policyMap> <policyEntries> <!-- Set the following policy on all queues using the '>' wildcard --> <policyEntry queue=">"> <!-- Tell the dead letter strategy not to process expired messages so that they will just be discarded instead of being sent to the DLQ --> <deadLetterStrategy> <sharedDeadLetterStrategy processExpired="false" /> </deadLetterStrategy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>...</broker>
<broker...> <destinationPolicy> <policyMap> <policyEntries> <!-- Set the following policy on all queues using the '>' wildcard --> <policyEntry queue=">"> <!-- Tell the dead letter strategy to also place non-persisted messages onto the dead-letter queue if they can't be delivered. --> <deadLetterStrategy> <sharedDeadLetterStrategy processNonPersistent="true" /> </deadLetterStrategy> </policyEntry> </policyEntries> </policyMap> </destinationPolicy>...</broker>