Skip to main content

Service Bus Queues from Windows Azure - How to iterate

In one of my last post I described how we can use Service Bus Queue from Windows Azure. Now, we will see if we can iterate through this queue.
In a normal MSMQ from Windows we could very simple iterate through the messages from the queue. We could get an iterator from this queue and after that is very simple to iterate.
Queue myQueue = ...
IEnumerator myQueueEnumerator = myQueue.GetEnumerator();
while(myQueueEnumerator.MoveNext())
{
var ourItemFromQueue = myQueueEnumerator.Current;
}

The GetEnumerator method generate an enumerator for queue with the current state of it. For example after we obtain the iterator we add another item in the queue, this item will not be obtained in the current iterator.
So we saw how we can iterate in through a MSMSQ. Nothing special, an old problem with classic solution. But how we can iterate through a Service Bus Queue?
If we look on the API that is available for QueueClient we will see that we don’t have any method to get the iterator for the current queue. In theory we should not be able to iterate the queue.
If you rememeber, in the last post that that describe how we can use a Service Bus Queue I told you about two ways to get a message from a queue. The receive&delete pattern when a message is automaticaly deleted from the queue when is send to the client.
The other option to consume messages from a Service Bus Queue is to get each message from the queue and send a confirmation call to the queue. This confirmation message notify the queue that the message was processed with success and can be removed from the queue. If we don’t send this confirmation message, after a specific type the message will be unlocked from the queue an other consumers will be able to process it. This expiration time period can be set when we create the queue.
If we set this experiation time period to a higher value (default value is 60 secconds), we can extract all the messages that a queue have using the Receive method and without calling the Complete() method. In this way all the messages that we will received will be automatically unlock after the given periond of time will pass and other consumer will be able to consume it.
QueueClient qc = ...;
int maxQueueNumber = 100;
List<BrokeredMessage> messages = new List<BrokeredMessage>();
try
{
while ( messages < maxQueueNumber )
{
messages.Add(qc.Receive(0));
}
}
catch(TimeoutException ex)
{
...
}
We specify the Receive tymeout to 0, because we don’t want to wait other messages to be added to the queue. The TimeoutException is throw when we will not have any more messages to the queue. Also we need to set a maxim limit of messages that we want to load from the queue. Be aware that there is a risk to add the same message twice in the queue if the confirmation timeout will expire. Because of this we should check if the message was already added to the queue. In this way we can detect the we excited this time and we don’t need anymore to iterate thgouth the Service Bus Queue. Each BrokeredMessage has a unique id that can be used for this check. The above code can be rewrite like this:
QueueClient qc = ...;
int maxQueueNumber = 100;
Dictionary<string,BrokeredMessage> messages = new Dictionary<,stringBrokeredMessage>();
try
{
while ( messages < maxQueueNumber )
{
BrokeredMessage message = qc.Receive(0);
string messageId = message.MessageId;
if (!messages.ContainsKey(messageId))
{
messages.Add(messageId,message);
}
else
{
break;
}
}
}
catch(TimeoutException ex)
{
...
}
This solution can work pretty good when we have queues that are not very big. Be aware that for each message that we received from the queue we will do two transcations and this can increase the our costs.
When we should use this „iteration“. Normally, if we use queues we should not have the need to iterate through a queue. The need of this is a warning for us that maybe soemthing we don’t do or use as we should. But a valid scenario to iterate thgouth a queue is when we want to debug our application and we need what messages we have in a queue. In this case this is the only way to see the messages from our Service Bus Queue.

Comments

Popular posts from this blog

Windows Docker Containers can make WIN32 API calls, use COM and ASP.NET WebForms

After the last post , I received two interesting questions related to Docker and Windows. People were interested if we do Win32 API calls from a Docker container and if there is support for COM. WIN32 Support To test calls to WIN32 API, let’s try to populate SYSTEM_INFO class. [StructLayout(LayoutKind.Sequential)] public struct SYSTEM_INFO { public uint dwOemId; public uint dwPageSize; public uint lpMinimumApplicationAddress; public uint lpMaximumApplicationAddress; public uint dwActiveProcessorMask; public uint dwNumberOfProcessors; public uint dwProcessorType; public uint dwAllocationGranularity; public uint dwProcessorLevel; public uint dwProcessorRevision; } ... [DllImport("kernel32")] static extern void GetSystemInfo(ref SYSTEM_INFO pSI); ... SYSTEM_INFO pSI = new SYSTEM_INFO(

Azure AD and AWS Cognito side-by-side

In the last few weeks, I was involved in multiple opportunities on Microsoft Azure and Amazon, where we had to analyse AWS Cognito, Azure AD and other solutions that are available on the market. I decided to consolidate in one post all features and differences that I identified for both of them that we should need to take into account. Take into account that Azure AD is an identity and access management services well integrated with Microsoft stack. In comparison, AWS Cognito is just a user sign-up, sign-in and access control and nothing more. The focus is not on the main features, is more on small things that can make a difference when you want to decide where we want to store and manage our users.  This information might be useful in the future when we need to decide where we want to keep and manage our users.  Feature Azure AD (B2C, B2C) AWS Cognito Access token lifetime Default 1h – the value is configurable 1h – cannot be modified

What to do when you hit the throughput limits of Azure Storage (Blobs)

In this post we will talk about how we can detect when we hit a throughput limit of Azure Storage and what we can do in that moment. Context If we take a look on Scalability Targets of Azure Storage ( https://azure.microsoft.com/en-us/documentation/articles/storage-scalability-targets/ ) we will observe that the limits are prety high. But, based on our business logic we can end up at this limits. If you create a system that is hitted by a high number of device, you can hit easily the total number of requests rate that can be done on a Storage Account. This limits on Azure is 20.000 IOPS (entities or messages per second) where (and this is very important) the size of the request is 1KB. Normally, if you make a load tests where 20.000 clients will hit different blobs storages from the same Azure Storage Account, this limits can be reached. How we can detect this problem? From client, we can detect that this limits was reached based on the HTTP error code that is returned by HTTP