Projects

Microsoft Azure: Working with Azure Queue storage

Taking this Microsoft Learn module, I was able to create an app that interacts with Azure Queue storage. Azure Queue storage, as Microsoft describes, implements the publish-subscribe pattern in the cloud to allow communication between applications. Below I showcase parts of the app I created. The full code project can be found here.

This function creates a queue in Azure Queue storage and then subsequently sends a message to that queue.

        // this sends a message to the queue
        // best practice is only for the publisher to create the queue.
        static async Task SendArticleAsync(string newsMessage)
        {
            // create a CloudQueue object that we can use to work with the queue.
            CloudQueue queue = GetQueue();
            // create the queue if necessary, or return false if the queue already exists.
            // this ensures the queue is ready for use
            bool createdQueue = await queue.CreateIfNotExistsAsync();
            if (createdQueue)
            {
                Console.WriteLine("The queue of news articles was created.");
            }
            // represents a message
            CloudQueueMessage articleMessage = new CloudQueueMessage(newsMessage);
            // use the CloudQueue object to send the message to the queue
            await queue.AddMessageAsync(articleMessage);
        }

This function reads the next message in the queue in Azure Queue storage, processes it, and the deletes it from the queue. As you can see, I don’t actually process the text. I just capture it and return it. Obviously, in a production situation the next message in the queue could require data processing.

 // read next message in queue, process it, and delete it from the queue
        static async Task<string> ReceiveArticleAsync()
        {
            // get our object we can use to create, delete, and check for an existing queue.
            // this is called our queue reference
            CloudQueue queue = GetQueue();
            // check if the queue exists.
            // if we attempt to retrieve a message from a non-existent queue, the API will throw an exception
            bool exists = await queue.ExistsAsync();
            if (exists)
            {
                // represents a message
                // get the message. i.e. the next message in the queue
                // the return value will be null if the queue is empty
                CloudQueueMessage retrievedArticle = await queue.GetMessageAsync();
                if (retrievedArticle != null)
                {
                    // this is where processing would be performed
                    // this gets the contents of the message
                    string newsMessage = retrievedArticle.AsString;
                    // delete the message after processing completes
                    await queue.DeleteMessageAsync(retrievedArticle);
                    return newsMessage;
                }

            }
            return "<queue empty or not created>";
        }

And that’s it for this post! Thank you for reading!

Leave a Reply