4

I understand that Azure Queue is not strict FIFO.
And Visual Studio Server Explorer shows only 32 messages. I have some 88 messages in the queue.
Is it possible to peek through all the messages in an Azure queue with out dequeing any of it?

Srinivas
  • 2,479
  • 8
  • 47
  • 69

2 Answers2

12

Simple answer to your question is "No, you can't do that". Reason being Peeking at messages does not alter their visibility so unless your messages are being dequeued by some other process, repeated peeking will return same messages.

Only alternative to fetch all messages would be to Get messages (32 at a time) with long visibility timeout period and then repeating this process again and again till the time there are no messages in a queue that can be dequeued. However you run the risk of messages not getting processed with this approach as they have been dequeued and thus are not visible to any other callers.

Gaurav Mantri
  • 128,066
  • 12
  • 206
  • 241
-1

@gaurav-mantri is right that you can't peek at more than 32 messages, but you can receive unlimited messages without deleting them (as he says though, they will temporarily be made invisible, for the length of time specified by the visibility timeout). here's an example in python of how to do just this:

from azure.identity import DefaultAzureCredential
from azure.storage.queue import QueueClient, BinaryBase64DecodePolicy, BinaryBase64EncodePolicy

def main():
    counter = 0
    credential = DefaultAzureCredential()
    account_url = "https://my-queue-url.net"
    queue_name = "my_queue"
    visibility_timeout = 3000  # set this high to prevent requeuing during operation
    client = QueueClient(account_url=account_url,
                         queue_name=queue_name,
                         credential=credential,
                         message_encode_policy=BinaryBase64EncodePolicy(),
                         message_decode_policy=BinaryBase64DecodePolicy())
    message_queue = client.receive_messages(visibility_timeout=visibility_timeout)
    with open("my_messages.txt", mode="w") as f:

        for message in message_queue:
            f.write(f"{message.content.decode('utf-8')}\n")
            counter += 1
            print(counter)

if __name__ == "__main__":
    main()
tvt173
  • 1,746
  • 19
  • 17
  • How does this help? With your solution, the messages just become visible again after a time period and you'll end up consuming the same messages again (if you decided to read again). As written though, your code only runs once in your code (meaning, it only returns up to 32 messages) - it does *not* read unlimited messages - you'd need some type of infinite (or near-infinite) loop to continue reading more messages Also: note that this question is almost a decade old, with an accepted answer. – David Makogon Jul 28 '23 at 02:22
  • Note that the messages are *not* temporarily dequeued either: they are just made invisible until they are explicitly deleted by the code that read the queue message (or the invisibility timeout occurs, at which point they become visible again (and also at which point the original caller can *not* delete the item, as the pop receipt will now be expired) – David Makogon Jul 28 '23 at 02:24
  • not true... i just read ~400k messages with this code. just trying to be helpful, giving a more in-depth answer to a problem i just worked through. thanks for the technical corrections. will fix in the answer – tvt173 Jul 28 '23 at 02:45