A while back my colleague Kash posted a couple of blogs showing how to get started with Azure Blobs and Tables. It's time I completed the trifecta by taking a look at the Queues SDK.
Getting started is very similar to the other services Kash covered.
We will work in a similar way, using F# scripts and the synchronous API for demonstration purposes.
In your application code you should always make use of the async API to avoid unnecessarily blocking threads.
1. Install dependencies
#r "nuget: Azure.Storage.Queues"
#r "nuget: Farmer"
open Farmer
open Farmer.Builders
open Azure.Storage.Queues
2. Deploy Azure Resources
We will once again use Farmer to quickly and easily deploy the required Azure resources, pulling the Storage connection string out of the response.
Note that some of these resource names need to be globally unique, so if your deployment fails check the error output carefully to find the cause.
let queueName = "blog-queue"
let storageAcc = storageAccount {
name "blogqueuestoragedemo"
add_queue queueName
}
let deployment = arm {
location Location.WestEurope
add_resource storageAcc
output "storageKey" storageAcc.Key
}
let deployedResources = deployment |> Deploy.execute "blogqueuestoragedemo" []
let connectionString = deployedResources.["storageKey"]
If you already have the Azure CLI installed and are logged in to your subscription, then you should just be able to execute the code above and then open portal.azure.com to see the resources.
3. Create a Queue client
let queueServiceClient = QueueServiceClient connectionString
let queueClient = queueServiceClient.GetQueueClient queueName
4. Send a couple of messages
queueClient.SendMessage("Hello from Queues 1!")
queueClient.SendMessage("Hello from Queues 2!")
5. Peek at the messages
You can peek at the messages without removing them from the queue. An optional integer sets the max messages to retrieve, with the default being 1.
queueClient.PeekMessages(2).Value
|> Array.iter (fun msg -> printfn $"{msg.Body}")
Notice the order of the messages - as you would expect, message 1 is at the front of the queue as you sent it first.
6. Receive a message
We won't pass a max message count here, so we will only pull a single message.
Once you have received a message, you have 30 seconds to process it.
let receivedMessages = queueClient.ReceiveMessages()
7. See that message 1 has been removed from the queue
If you peek at the queue during this period, you will see that message 1 has gone, and only message 2 remains
After 30 seconds have passed however, if you peek at the queue again you will see that message 1 has reappeared behind message 2 in the queue.
queueClient.PeekMessages(2).Value
|> Array.iter (fun msg -> printfn $"{msg.Body}")
8. Receive message 2
let receivedMessages2 = queueClient.ReceiveMessages()
receivedMessages2.Value
|> Array.iter (fun msg -> printfn $"{msg.Body}")
9. Delete message 2
queueClient
.DeleteMessage(
receivedMessages2.Value.[0].MessageId,
receivedMessages2.Value.[0].PopReceipt)
10. See that only message 1 is left
queueClient.PeekMessages(2).Value
|> Array.iter (fun msg -> printfn $"{msg.Body}")
As you can see, the Queues SDK is easy to get started with. As a reminder, please do use the async API in your apps. Also check out the docs for more info, and for further reading my previous WebJobs blog shows how you can integrate Queues with Event Grid.