3

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.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
Amit
  • 167
  • 1
  • 2
  • 14
  • Possible duplicate of [How do you access the dead letter sub-queue on an Azure subscription?](https://stackoverflow.com/questions/22681954/how-do-you-access-the-dead-letter-sub-queue-on-an-azure-subscription) – Evan Mulawski Jan 20 '18 at 22:22

2 Answers2

4

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.

  • 2
    With the new version of SDK, getting deadletter queue has changed. It is now: `deadletter_queue_client = servicebus_client.get_queue_receiver(queue_name="my-queue", sub_queue="deadletter")` – dinesh ygv Jul 12 '22 at 12:43
0

Here is the sample code to read message in deadletter

from azure.servicebus import ServiceBusClient
CONNECTION_STR = "connection string"


servicebus_client = ServiceBusClient.from_connection_string(conn_str=CONNECTION_STR, logging_enable=True)

queueName ="Queue name"
with servicebus_client:
    # get the Queue Receiver object for the queue
    receiver = servicebus_client.get_queue_receiver(queue_name=queueName,sub_queue="deadletter")
    with receiver:
        for msg in receiver:
            print("Received: " + str(msg))
            # complete the message so that the message is removed from the queue
            receiver.complete_message(msg)
            
daz
  • 696
  • 10
  • 10