DLQ-死信队列(Dead LetterQueue)用来保存处理失败或者过期的消息。
出现以下情况时,消息会被redelivered
- A transacted session is used and rollback() is called.
- A transacted session is closed before commit is called.
- A session is using CLIENT_ACKNOWLEDGE and Session.recover() iscalled.
当一个消息被redelivered超过maximumRedeliveries(缺省为6次,具体设置请参考后面的链接)次数时,会给broker发送一个"Poisonack",这个消息被认为是a poison pill,这时broker会将这个消息发送到DLQ,以便后续处理。
缺省的死信队列是ActiveMQ.DLQ,如果没有特别指定,死信都会被发送到这个队列。
缺省持久消息过期,会被送到DLQ,非持久消息不会送到DLQ
可以通过配置文件(activemq.xml)来调整死信发送策略。
1.
缺省所有队列的死信消息都被发送到同一个缺省死信队列,不便于管理。可以通过individualDeadLetterStra tegy或sharedDeadLetterStrategy 策略来进行修改。如下:
<broker...>
<destinationPolicy>
<policyMap>
<policyEntries>
<!— 设置所有队列,使用 '>' ,否则用队列名称 -->
<policyEntry queue=">">
<deadLetterStrategy>
<!--
queuePrefix:设置死信队列前缀
useQueueForQueueMessages : 设置使用队列保存死信,还可以设置useQueueForTopicMessages ,使用Topic来保存死信
-->
<individualDeadLetterStra tegy queuePrefix="DLQ." useQueueForQueueMessages ="true" />
</deadLetterStrategy>
</policyEntry>
</policyEntries>
</policyMap>
</destinationPolicy>
...
</broker>
2.
<policyEntry queue=">">
<deadLetterStrategy>
<sharedDeadLetterStrategy processNonPersistent="true" />
</deadLetterStrategy>
</policyEntry>
3.
<policyEntry queue=">">
<deadLetterStrategy>
<sharedDeadLetterStrategy processExpired="false" />
</deadLetterStrategy>
</policyEntry>
4.
对于过期的,可以通过processExpired属性来控制,对于redelivered的失败的消息,需要通过插件来实现如下:
丢弃所有死信
<beans>
<broker ...>
<plugins>
<discardingDLQBrokerPlugi n dropAll="true" dropTemporaryTopics="true" dropTemporaryQueues="true" />
</plugins>
</broker>
</beans>
丢弃指定目的死信
<beans>
<broker ...>
<plugins>
<discardingDLQBrokerPlugi n dropOnly="MY.EXAMPLE.TOPIC.29 MY.EXAMPLE.QUEUE.87" reportInterval="1000" />
</plugins>
</broker>
</beans>
注意,目的名称使用空格分隔
The reportInterval property is used to denote how frequently do we output how many messages we have dropped - use 0 to disable.
用正则表达式过滤丢弃消息:
<beans>
<broker ...>
<plugins>
<discardingDLQBrokerPlugi n dropOnly="MY.EXAMPLE.TOPIC.[0-9]{3} MY.EXAMPLE.QUEUE.[0-9]{3}" reportInterval="3000" />
</plugins>
</broker>
</beans>
Notice that the destination names use regular expressions. These match the number 000..999 at the end of each destination name.
5.
死信队列中的消息,会增加几个属性,比如原过期时间(originalExpiration),原originalDeliveryMode等
参考:
DLQ处理说明:
http://activemq.apache.org/message-redelivery-and-dlq-handling.html
individualDeadLetterStra
sharedDeadLetterStrategy属性说明:
redelivery属性说明:
http://activemq.apache.org/redelivery-policy.html