Azure Service Bus 死信隊列產生的原因
服務總線中有幾個活動會導致從消息引擎本身將消息推送到 DLQ。 如
- 超過 MaxDeliveryCount
- 超過 TimeToLive
- 處理訂閱規則時的錯誤
- 應用程序主動設置信息進入死信隊列
進入死信隊列(DLQ)里面的數據 不會自動執行清理操作。 消息將保留在 DLQ 中,直到顯式從 DLQ 中檢索它們以及對死信消息調用 Complete() 為止。可以使用Service Bus Explorer工具來查看死信隊列中消息
也同時可以看見該條消息進入死信隊列的根本原因:
Moving messages to the DLQ
There are several activities in Service Bus that cause messages to get pushed to the DLQ from within the messaging engine itself. An application can also explicitly move messages to the DLQ.
As the message gets moved by the broker, two properties are added to the message as the broker calls its internal version of the DeadLetter method on the message:
DeadLetterReason
andDeadLetterErrorDescription
.Applications can define their own codes for the
DeadLetterReason
property, but the system sets the following values.
MOVING MESSAGES TO THE DLQ DeadLetterReason DeadLetterErrorDescription HeaderSizeExceeded The size quota for this stream has been exceeded. TTLExpiredException The message expired and was dead lettered. See the Exceeding TimeToLive section for details. Session ID is null. Session enabled entity doesn't allow a message whose session identifier is null. MaxTransferHopCountExceeded The maximum number of allowed hops when forwarding between queues. Value is set to 4. MaxDeliveryCountExceededExceptionMessage Message could not be consumed after maximum delivery attempts. See the Exceeding MaxDeliveryCount section for details.
死信問題:
一:隊列中存在死信需求如何處理,讓這個隊列恢復正常?
死信是由於客戶端無法正常消費消息產生的,Service Bus會將這些無法正常消費的消息移動到另外的死信隊列中,這個是不會影響原有隊列的消息的消費和使用。
二:隊列中出現死信后是否可以重新發送消息?
死信是已經發送到service bus但消費端無法正常消費的消息,這不影響再次向隊列發送消息。
三:消息到死信隊列后,怎么操作可以讓消息重新到活動隊列?
不能使消息重新回到活動隊列。因為死信隊列的用途是存放無法傳遞給任何接收方的消息或無法處理的消息。 然后,可從 DLQ 中刪除和檢查這些消息。 應用程序可能程序員的幫助下,更正問題並重新提交消息,記錄出錯的實際情況和執行更正操作。
如需要通過程序的方式獲取死信隊列中的消息,獲取消息的方式和正常隊列一樣,把queueName變為死信隊列的路徑,通過QueueClient.FormatDeadLetterPath(queueName)方式獲取。也可以直接指定為以下格式的路徑:
<queue path>/$deadletterqueue <topic path>/Subscriptions/<subscription path>/$deadletterqueue
參考代碼如:
根據Azure官方目前的獲取Queue中消息的方法,只需替換QueueName
參考資料
服務總線死信隊列概述:https://docs.azure.cn/zh-cn/service-bus-messaging/service-bus-dead-letter-queues