jakeMantle
jakeMantle

Reputation: 189

In Azure Service Bus, how to read the latest messages in the Dead Letter Queue?

There are about 9000 messages in a dead letter queue in a service bus. My requirement is I should be reading only the lastest messages inserted in the queue in peek mode. But, in the azure documentation, there is no parameters to read the latest messages but rather from the start of first message.

So, is there a way to get the latest messages and not delete it from the DLQ?

This is the current code:

servicebus_client = ServiceBusClient.from_connection_string(conn_str=cs,
                                                            # logging_enable=True,
                                                            retry_total=5, retry_backoff_factor=5, retry_mode="fixed",
                                                            transport_type=TransportType.AmqpOverWebsocket
                                                            )

dlq_receiver = servicebus_client.get_queue_receiver(queue_name=QUEUE_NAME, 
                                                    sub_queue=ServiceBusSubQueue.DEAD_LETTER, 
                                                    receive_mode=ServiceBusReceiveMode.PEEK_LOCK)

with dlq_receiver:
        while True:
            received_msgs = dlq_receiver.receive_messages(max_message_count=1, max_wait_time=5)
            for msg in received_msgs:
                    try:
                        decoded_value = msg.body.decode('utf-8')
                    except UnicodeDecodeError:
                        decoded_value = gzip.decompress(msg.body)
                        decoded_value = decoded_value.decode('utf-8')
                        print(decoded_value)
                    print("Body type: {}".format(msg.body_type))
                    print("Time to live: {}".format(msg.time_to_live))
                    print("Sequence number: {}".format(msg.sequence_number))
                    print("Enqueue Sequence number: {}".format(msg.enqueued_sequence_number))
                    print("Partition Key: {}".format(msg.partition_key))
                    print("Locked until: {}".format(msg.locked_until_utc))
                    print("Lock Token: {}".format(msg.lock_token))
                    print("Dead Letter Reason: {}".format(msg.dead_letter_reason))
                    print("Enqueued time: {}".format(msg.enqueued_time_utc))

For the input, I could notice that some of the messages are re-read(i.e., I am seeing the same sequence number). How to acheive my usecase?

Upvotes: 0

Views: 936

Answers (1)

Sean Feldman
Sean Feldman

Reputation: 26047

Azure Service Bus doesn't read messages from the end of the queue. It always retrieves messages in the order those messages have been enqueued. If you need a custom way to access your data, use a database or a service that supports querying (database, blobs with Blob Index, etc). You can combine a queue with an index to keep messages in the queue and be able to receive it later by its sequence, but this approach is somewhat more complicated.

Upvotes: 1

Related Questions