Azure Storage Queue
What is Azure Queue Storage
Azure Queue Storage is a service for storing large numbers of messages. You access messages from anywhere in the world via authenticated calls using HTTP or HTTPS. A queue message can be up to 64 KB in size. A queue may contain millions of messages, up to the total capacity limit of a storage account. Queues are commonly used to create a backlog of work to process asynchronously.
Attributes of Azure Queue Storage
- Azure storage queue lives under a storage account (Azure storage account also supports blob, file share, queues, tables and disk)
- Within a storage account, you an have multiple queues
- Every single queue can have multiple messages
- The size of a message can't be more than 64 KB
- The URL format of a queue is https://<storage account>.queue.core.windows.net/<queue>
- For example - https://learnaboutazurestorage.queue.core.windows.net/add-weatherdata
How to Create Azure Storage account and Azure Storage Queue
- Login to https://portal.azure.com/
- Create a resource group (you can ignore this step, if you already have a resource group)
- Now create a storage account under the resource group. The storage account name should be between 3 - 24 characters and can contain only lower case characters and numbers
- To create a queue - scroll to the data storage section of the storage account and click Queue - add queue - give a meaningful name for the queue and click ok
- Azure storage account and Azure storage queue is successfully created
Steps to implement Azure Queue Storage functionality
- Let's create a sample Web API and write a post method to make an entry in the queue storage
- Add Nuget package Azure.Storage.Queues
- Write a simple post method to add data to storage queue
- Create an object of QueueClient which comes from the namespace Azure.Storage.Queues and takes connectionString and queueName as parameter
- To get the connectionString, go to storage account section of Azure, click on AccessKey, and copy the value of attribute connectionString
- Copy the queueName from Queue section of storage account
- Use the SendMessageAsync method of queueClient to send a message to queue
- The method will finally look like the one below
public async Task Post([FromBody] WeatherForecast data){var connectionString = "Your Connection String";var queueName = "Your Queue Name";var message = JsonSerializer.Serialize(data);var queueClient = new QueueClient(connectionString, queueName);await queueClient.SendMessageAsync(message);}
- If you call the post method with some data (using swagger or postman), the entry will be visible under queue storage of Azure and it will look like the one below
- Each record is also assigned a unique ID, inserted time, expiration time (by default 7 days, it means that the record will automatically disappear after 7 days if no one uses this before that time) and dequeue count (default is 0)
- If we want to change the default expiration time to a different value, we can do it while creating a message into queue.
- For this, we need to use queueClient.SendMessageAsync(message, null, TimeSpan.FromDays(10)); In this code, 3rd parameter is expiry time. This message will be staying in Queue for 10 days instead of 7 days.
- If we will set the expiry time to a negative value, it means that this message will never expire. We can use queueClient.SendMessageAsync(message, null, TimeSpan.FromDays(-1)); to achieve this
- The second parameter is Visibility time - it means after how much time, the entry will appear in queue. By default all queue entries immediately appear as soon as it is created, but if we set the value of 2nd parameter to 30 seconds, then the record will appear in queue only after 30 seconds of it's creation.
- We can use queueClient.SendMessageAsync(message, TimeSpan.FromSeconds(30), TimeSpan.FromDays(10)); to achieve the above
- We can also use the standalone desktop version of the software Azure Storage Explorer to see everything under a storage account
Reference URL:
Source Code: https://github.com/kumarabhimanyu/AzureQueueWithBackgroundTask
https://docs.microsoft.com/en-us/azure/storage/queues/storage-queues-introduction
https://www.youtube.com/watch?v=5oTX6srQdOE&t=2s
Read data using background Task: https://techabhimanyu.blogspot.com/2022/08/read-azure-queue-storage-from-net-core.html
Comments
Post a Comment