生活随笔
收集整理的這篇文章主要介紹了
ActiveMQ的消息重发策略和DLQ处理
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
2019獨(dú)角獸企業(yè)重金招聘Python工程師標(biāo)準(zhǔn)>>>
ActiveMQ的消息重發(fā)策略和DLQ處理 博客分類: MQ
在以下三種情況中,ActiveMQ消息會(huì)被重發(fā)給客戶端/消費(fèi)者:? 1.使用一個(gè)事務(wù)session,并且調(diào)用了rollback()方法;? 2.一個(gè)事務(wù)session,關(guān)閉之前調(diào)用了commit;? 3.在session中使用CLIENT_ACKNOWLEDGE簽收模式,并且調(diào)用了Session.recover()方法。? Broker根據(jù)自己的規(guī)則,通過BrokerInfo命令包和客戶端建立連接,向客戶端傳送缺省發(fā)送策略。但是客戶端可以使用ActiveMQConnection.getRedeliveryPolicy()方法覆蓋override這個(gè)策略設(shè)置。?
Java代碼
RedeliveryPolicy?policy?=?connection.getRedeliveryPolicy();?? policy.setInitialRedeliveryDelay(500 );?? policy.setBackOffMultiplier(2 );?? policy.setUseExponentialBackOff(true );?? policy.setMaximumRedeliveries(2 );?? 一旦消息重發(fā)嘗試超過重發(fā)策略中配置的maximumRedeliveries(缺省為6次)時(shí),會(huì)給broker發(fā)送一個(gè)"Poison ack",通知它,這個(gè)消息被認(rèn)為是一個(gè)毒丸(a poison pill),接著broker會(huì)將這個(gè)消息發(fā)送到DLQ(Dead Letter Queue),以便后續(xù)分析處理。? 缺省死信隊(duì)列(Dead Letter Queue)叫做ActiveMQ.DLQ;所有的未送達(dá)消息都會(huì)被發(fā)送到這個(gè)隊(duì)列,以致會(huì)非常難于管理。你可以設(shè)置activemq.xml文件中的destination policy map的"individualDeadLetterStrategy"屬性來修改.?
Java代碼
<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>?? 自動(dòng)丟棄過期消息(Expired Messages) ? 一些應(yīng)用可能只是簡(jiǎn)單的丟棄過期消息,而不想將它們放到DLQ中,完全跳過了DLQ。在dead letter strategy死信策略上配置processExpired屬性為false,可以實(shí)現(xiàn)這個(gè)功能。?
Java代碼
<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>?? 將非持久消息(non-persistent messages)放入死信隊(duì)列 ? ActiveMQ缺省不會(huì)將未發(fā)到的非持久消息放入死信隊(duì)列。如果一個(gè)應(yīng)用程序并不想將消息message設(shè)置為持久的,那么記錄下來那些未發(fā)送到的消息對(duì)它來說往往也是沒有價(jià)值的。不過如果想實(shí)現(xiàn)這個(gè)功能,可以在dead-letter strategy死信策略上設(shè)置processNonPersistent="true"?
Java代碼
<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>?? http://sharong.iteye.com/blog/1987171
轉(zhuǎn)載于:https://my.oschina.net/xiaominmin/blog/1597342
總結(jié)
以上是生活随笔 為你收集整理的ActiveMQ的消息重发策略和DLQ处理 的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔 推薦給好友。