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(

ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded

Today blog post will be started with the following error when running DB tests on the CI machine: threw exception: System.InvalidOperationException: The Entity Framework provider type 'System.Data.Entity.SqlServer.SqlProviderServices, EntityFramework.SqlServer' registered in the application config file for the ADO.NET provider with invariant name 'System.Data.SqlClient' could not be loaded. Make sure that the assembly-qualified name is used and that the assembly is available to the running application. See http://go.microsoft.com/fwlink/?LinkId=260882 for more information. at System.Data.Entity.Infrastructure.DependencyResolution.ProviderServicesFactory.GetInstance(String providerTypeName, String providerInvariantName) This error happened only on the Continuous Integration machine. On the devs machines, everything has fine. The classic problem – on my machine it’s working. The CI has the following configuration: TeamCity .NET 4.51 EF 6.0.2 VS2013 It see

Navigating Cloud Strategy after Azure Central US Region Outage

 Looking back, July 19, 2024, was challenging for customers using Microsoft Azure or Windows machines. Two major outages affected customers using CrowdStrike Falcon or Microsoft Azure computation resources in the Central US. These two outages affected many people and put many businesses on pause for a few hours or even days. The overlap of these two issues was a nightmare for travellers. In addition to blue screens in the airport terminals, they could not get additional information from the airport website, airline personnel, or the support line because they were affected by the outage in the Central US region or the CrowdStrike outage.   But what happened in reality? A faulty CrowdStrike update affected Windows computers globally, from airports and healthcare to small businesses, affecting over 8.5m computers. Even if the Falson Sensor software defect was identified and a fix deployed shortly after, the recovery took longer. In parallel with CrowdStrike, Microsoft provided a too