Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to receive messages from dead letter queue from azure service bus queue

I have a queue and when I send messages to that queue I see most of the messages are going to its dead letter queue. I want to re submit them to the same queue..
It would be very helpful to me if anyone could suggest me any solution.

like image 641
Amit Avatar asked Sep 11 '25 12:09

Amit


1 Answers

One possible way is to Receive the messages from the dead letter queue and send them to the normal queue.

Python Code

Step-1: Receive the messages from the dead letter queue:

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()

Step-2: Send the messages to the normal queue:

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))

Hope this helps.

like image 114
anilkumar kasaragadda Avatar answered Sep 14 '25 05:09

anilkumar kasaragadda