Coding Canvas

Technical Blog of a Programming Enthusiast

  • Home
  • About Me

Using azure storage queues

Azure offers two kinds of queuing mechanism viz. storage queues and service bus queues. In this blog post, I’ll explain how to use storage queues for sending and receiving messages.

For details on the difference between storage and service bus queues please refer this msdn documentation. It also gives guidance on when to use what.

Sending messages

In the sample code below, we are sending 10 messages to a storage queue.

 public class FileUploadMessage
    {
        public string FilePath { get; set; }
        public int FileId { get; set; }
    }
public class AzureStorageQueueSender
    {
        public class FileUploadMessage
        {
            public string FilePath { get; set; }
            public int FileId { get; set; }
        }
        public async Task SendMessagesAsync()
        {
            var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
            var queueClient = storageAccount.CreateCloudQueueClient();
            var queueRef = queueClient.GetQueueReference("filequeue");

            await queueRef.CreateIfNotExistsAsync();

            for (int i = 0; i < 10; i++)
            {
                //simulate some processing by putting a delay while sending messages
                await Task.Delay(200 * i);
                var uploadMessage = new FileUploadMessage() { FileId = i, FilePath = "Something" };
                var message = new CloudQueueMessage(JsonConvert.SerializeObject(uploadMessage));
                await queueRef.AddMessageAsync(message);
            }
        }
    }

Code is self-explanatory. The only thing worth mentioning is that ‘CloudQueueMessage’ class accepts only a string argument so if you have a complex object then you need to serialize it and send.

Receiving messages

Below is the sample code for receiving messages.

public class FileUploadMessage
    {
        public string FilePath { get; set; }
        public int FileId { get; set; }
    }

    public class AzureStorageQueueReceiver
    {
        public async Task ReceiveMessagesAsync()
        {
            var storageAccount = CloudStorageAccount.Parse("UseDevelopmentStorage=true");
            var queueClient = storageAccount.CreateCloudQueueClient();
            var queueRef = queueClient.GetQueueReference("filequeue");
            await queueRef.CreateIfNotExistsAsync();

            while (true)
            {
                await queueRef.FetchAttributesAsync();
                if (queueRef.ApproximateMessageCount > 0)
                {
                    var message = await queueRef.GetMessageAsync();
					//set the next visibility timeout
                    await queueRef.UpdateMessageAsync(message, TimeSpan.FromSeconds(15),
                        Microsoft.WindowsAzure.Storage.Queue.MessageUpdateFields.Content
                        | Microsoft.WindowsAzure.Storage.Queue.MessageUpdateFields.Visibility);
                    if (message == null) continue;
                    var uploadMessage = JsonConvert.DeserializeObject<FileUploadMessage>(message.AsString);
                    Console.WriteLine($"Received message : {message.AsString}");
					//Delete the message after successfully processing it
					await queueRef.DeleteMessageAsync(message);
                    
                }
                else
                {
                    Console.WriteLine("No message on the queue");
                    await Task.Delay(1000);
                }
            }
        }
    }

The important point here is to understand how the message handling happens. Every time you pull a message from the queue you get the lease on the message for a specified time (by default it is 30 seconds).If you are not able to process the message during this time you should extend the lease else the message will appear again on the queue and would be available for processing. Once the message processing is successful the message should be deleted so that it does not reappear on the queue.

In the sample code we pull the message from the queue and in next line we update the visibility time out (i.e the time after which the message will reappear for processing on the queue based on how much time we need for processing).

Once the message has been processed successfully we delete the message. In case an exception occurs the message will automatically reappear after 45 seconds for reprocessing.

In this post, I showed a very basic example of sending and receiving using storage queues. In future posts, I’ll be writing more on specific aspects of working with storage queues. To know in-depth on azure storage accounts (i.e Azure tables ,blobs and queues) here is a very good course by alan smith.

Tagged on: Azure, azure storage account, azure storage queue

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.


Recent Posts

  • How batched state updates differ in React 17 vs React 18
  • RESTful CRUD API using Nodejs,express and mysql : Part 3 – Implementing CRUD
  • RESTful CRUD API using Nodejs,express and mysql : Part 2 – Integrate with MySQL database using ORM
  • RESTful CRUD API using Nodejs,express and mysql : Part 1 – Basic API setup
  • Azure AD Series – 2: Integrating Azure active directory authentication with a single page application (SPA)

Archives

  • September 2023
  • January 2022
  • December 2021
  • February 2021
  • June 2020
  • May 2020
  • April 2020
  • March 2020
  • February 2020
  • January 2020
  • October 2018
  • August 2018
  • July 2018
  • November 2017
  • October 2017
  • September 2017
  • December 2016
  • December 2015
  • July 2015
  • May 2015
  • January 2015
  • December 2014
  • October 2014
  • September 2014
  • August 2014
  • July 2014
  • June 2014

Tags

.NET AngularJS api application insights ASP.NET Asp.net core Authentication Azure Azure Active Directory Azure DevOps Azure key vault Azure pipelines azure storage queue C# cheat sheet Cloud design patterns CRUD docker Docker compose efcore Entity Framework Entityframework core expressjs fileupload Full Text Search HTML http JavaScript Logic Apps Mocking MongoDb Moq nodejs ORM Razor pages rest Retry Pattern secret management SQL sql server SQL Server 2008 SQL Server 2012 sql server change tracking UnitTesting user secrets

Categories

  • .Net
  • AI
  • Algorithms
  • AngularJS
  • Architecture
  • ASP. NET CORE
  • ASP.NET
  • Azure
  • C#
  • Database
  • Design Pattern
  • Docker
  • Entity framework core
  • JavaScript
  • JSON
  • NLP
  • Nodejs
  • SQL Server
  • Uncategorized
  • Unit Testing

Subscribe to my posts



Created by Webfish.
Copyright © 2025 Coding Canvas | Theme by: Theme Horse | Powered by: WordPress