如何从天蓝色服务总线队列中接收来自死信队列的消息

时间:2016-10-04 18:51:50

标签: azure azure-functions azure-queues

我有一个队列,当我向该队列发送消息时,我发现大多数消息都会进入死信队列。我想重新提交他们到同一个队列..
如果有人能建议我任何解决方案,对我来说会非常有帮助。

1 个答案:

答案 0 :(得分:0)

一种可能的方法是从死信队列中接收消息并将其发送到普通队列。

Python代码

步骤1:从死信队列中接收消息:

from azure.servicebus import ServiceBusClient
import json
connectionString = "Your Connection String to Service Bus"
serviceBusClient = ServiceBusClient.from_connection_string(connectionString)
queueName = "Your Queue Name created in the Service Bus"
queueClient = serviceBusClient.get_queue(queueName)
with queueClient.get_deadletter_receiver(prefetch=5) as queueReceiver:
messages = queueReceiver.fetch_next(timeout=100)
for message in messages:
    # message.body is a generator object. Use next() to get the body.
    body = next(message.body)
    # Store the body in some list so that we can send them to normal queue.
    message.complete()

步骤2:将邮件发送到普通队列:

from azure.servicebus import ServiceBusClient, Message
connectionString = "Your Service Bus Connection String"
serviceBusClient = ServiceBusClient.from_connection_string(connectionString)
queueName = "Your Queue name"
queueClient = serviceBusClient.get_queue(queueName)

messagesToSend = [<List of Messages that we got from the dead letter queue>]

with queueClient.get_sender() as sender:
    for msg in messagesToSend:
        sender.send(Message(msg))

希望这会有所帮助。