Unable to get queue length / message count from Azure
Asked Answered
F

2

16

I have a Use Case where I need to queue a select number of messages when the current queue length drops below a specified value. Since I'm running in Azure, I'm trying to use the RetrieveApproximateMessageCount() method to get the current message count. Everytime I call this I get an exception stating StorageClientException: The specified queue does not exist.. Here is a review of what I've done:

  1. Created the queue in the portal and have successfully queued messages to it.

  2. Created the storage account in the portal and it is in the Created/Online state

  3. Coded the query as follows (using http and https options):

    var storageAccount = new CloudStorageAccount(
            new StorageCredentialsAccountAndKey(_messagingConfiguration.StorageName.ToLower(),
            _messagingConfiguration.StorageKey), false);
    
    var queueClient = storageAccount.CreateCloudQueueClient();
    var queue = queueClient.GetQueueReference(queueName.ToLower());
    int messageCount;
    
    try
    {
        messageCount = queue.RetrieveApproximateMessageCount();
    }
    catch (Exception)
    {
        //Booom!!!!! in every case
    }
    
    // ApproximateMessageCount is always null
    
    messageCount = queue.ApproximateMessageCount == null ? 0 : queue.ApproximateMessageCount.Value;
    
  4. I've confirmed the name is cased correctly with not special characters, numbers, or spaces and the resulting queue Url appears as though its correct formed based on the API documentations (e.g. http://myaccount.queue.core.windows.net/myqueue)

Can anyone help shed some light on what I'm doing wrong.


EDIT

I've confirmed that using the MessageFactory I can create a QueueClient and then enqueue/dequeue messages successfully. When I use the CloudStorageAccount the queue is never present so the counts and GetMessage routines never work. I am guessing these are not the same thing??? Assuming, I'm correct, what I need is to measure the length of the Service Bus Queue. Is that possible?

Flute answered 20/6, 2012 at 23:49 Comment(9)
And... does the queue exist? :-) What happens if you call queue.AddMessage(...) or queue.GetMessage(...)?Bark
@smarx I can queue and dequeue with no trouble. In my mind, that means it really exists. Nevermind the fact I created it in the portal, so it should exist. (Boggle!)Flute
@smarx I should have mentioned that the enqueue and dequeue are done in seperate processes so it must exist.Flute
You can't create a queue in the portal. I'm sticking with "The queue doesn't exist." Try putting queue.GetMessage() right before queue.RetrieveApproximateMessageCount(). I'm betting it fails with the same error.Bark
..or try adding a 'CreateIfNotExists'. You might be connecting to a different storage account in the other process?Ginsberg
Try firing up fiddler and verifying the queue names used by both the enqueue/dequeue operations as well as the attempt to get the queque count. You can also try using a 3rd party storage utility to verify outside of your codee that the queue does exist and has contents. Some of these tools will support displaying the queue depth and thus can be used to verify that the operation is working. If the tools are displaying things properly, then there is something wrong in your code, either addressability or something not existing when you expect it too.Litton
When I go to Services --> Service Bus --> Queues in the portal I can see the Queue exists and has messages in it. When I use the MessagingFactory I can create a QueueClient and Enqueue/Dequeue with no problem, but can't see the length. When I use the CloudStorageAccount and generate a CloudQueue instance the queue does not exist. I'm guessing these are not the same queues? How can I get length of a Service Bus Queue? Have I just completely jumbled seperate tech?Flute
You are using two different queuing technologies. It sounds like you've created a Service Bus Queue, but the code you're giving is for an Azure Storage Queue. They are not the same thing, despite having similar goals.Reinsure
@breischl I finally came to understand that. Do you know if there is a way to get the Length of a Service Bus Queue? Then again, maybe I should withdraw this and post a different question.Flute
H
69

RetrieveApproximateMessageCount() has been deprecated

if you want to use ApproximateMessageCount to get result try this

CloudQueue q = queueClient.GetQueueReference(QUEUE_NAME);
q.FetchAttributes();
qCnt = q.ApproximateMessageCount;
Holmun answered 25/4, 2014 at 7:37 Comment(3)
Such a simple answer for a simple problem. Nice.Polypetalous
Would be so nice if the ApproximateMessageCount threw a pleasant descriptive exception telling you to have to fetch the attributes first.Janenejanenna
CloudQueue is now, also, deprecated (only in the legacy SDK) so this is out of date nowLucrative
B
9

The CloudQueue method has been deprecated (along with the v11 SDK).

The following snippet is the current replacement (from the Azure Docs)

//-----------------------------------------------------
// Get the approximate number of messages in the queue
//-----------------------------------------------------
public void GetQueueLength(string queueName)
{
    // Get the connection string from app settings
    string connectionString = ConfigurationManager.AppSettings["StorageConnectionString"];

    // Instantiate a QueueClient which will be used to manipulate the queue
    QueueClient queueClient = new QueueClient(connectionString, queueName);

    if (queueClient.Exists())
    {
        QueueProperties properties = queueClient.GetProperties();

        // Retrieve the cached approximate message count.
        int cachedMessagesCount = properties.ApproximateMessagesCount;

        // Display number of messages.
        Console.WriteLine($"Number of messages in queue: {cachedMessagesCount}");
    }
}

https://learn.microsoft.com/en-us/azure/storage/queues/storage-dotnet-how-to-use-queues?tabs=dotnet#get-the-queue-length

Bonnet answered 3/12, 2020 at 6:32 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.