11
votes

I am using the PeekBatch(<messageCount>) method on the QueueClient (Windows Azure Service Bus package version 2.1.2.0).

It works fine the first time, and returns my single message that exists in my queue, but subsequent calls return nothing. After five minutes, the call will then return the message again.

Five minutes is the maximum lock time on a BrokeredMessage, so I am wondering if PeekBatch is actually locking those messages like it does on a receive, even though peeking should not lock, as far as I know.

I am trying to build an MVC view to be able to see what is actually sitting in my queue, but this one is getting in the way. Can anyone provide any guidance on this?

Update: This only seems to be happening when I cache my QueueClient using a static property. If I create the QueueClient fresh every time, the PeekBatch works as expected. I still have no idea why reusing a QueueClient causes this. Microsoft seems to recommend reusing the QueueClient though, instead of recreating it every time, so I am still at a loss here.

1

1 Answers

21
votes

The QueueClient is being somewhat helpful. In the Peek methods (Peek and PeekBatch) you can simply call them, or you can give a specific sequence number to retrieve a specific message after a certain sequence number. If you simply call Peek, or in your case PeekBatch, without a sequence number then it will retrieve the very first message, or messages, in the queue. Once the message is returned the QueueClient keeps track of the last sequence number it pulled. Each subsequent call to Peek will fetch the next message in the queue. The idea is that you are "browsing" the messages and not just interested in the first message on the queue every time.

So, if you were in a loop and called peek repeatedly until it didn't return a message you would essentially have browsed all message in the queue.

Since you are calling PeekBatch without a sequence number the QueueClient is remembering the last set it got, then the next call would actually try to get the next set after the last message it browsed. This is why when you recreate the QueueClient it seems to reset. The reason it seemed to reset on it's own after 5 minutes seems odd, but it maybe just cleaning out the browsing values after some point that is tied to the Timeout operation on the queue. By then the sequence number would be pretty far off anyway if it was a busy queue.

If you need to really only look at the very first message then call peek only once. It will only return the first message. If you need to continually pull the first message every time do a Peek(0). If you want the first say 10 messages every time then call PeekBatch(0, 10); that will be like saying give me the first ten messages that have a sequence number greater than 0.

The guidance to reuse the QueueClient is sound. It's doing all sorts of caching of information and things. You don't want to have to recreate it each time.