This is the second post about Service Bus Queues from Windows Azure. In the first one I made a small introduction to Service Bus Queues. Today we will see how we can create a new queue in Service Bus namespace and how we can use it.
So, first of all you need a Windows Azure account. After you enter Windows Azure portal we need to go to Services tab and select Service Bus. On this page we will need to create a new namespace for our service. The namespace is a unique name that is used to identify a unique service (in our case the Service Bus). After we create this namespace, we already have the Service Bus Queue created automatically. In this moment we don’t need to pay for anything because we didn’t create a queue.
To be able to access the queue we need the “Default Issuer” and “Default Key”. These two strings represent our security credentials that can be used to access the queue from our application. These values can be found if we select the Service Bus namespace that we created already and click on the “View” button from the “Default Key” panel (it is in the right of the page).
Now, that we have this credentials we need to add them to our applications. To be able to access the Service Bus Queue we will security information plus the endpoint (the namespace to our Service Bus).
There are a lot of places where we can store these values. The simplest one is to hardcode this value in our application, but is not the best one. If we create a website we can use configuration file of our web application to store this information.
So now, we can look in the code to see what we need to do (write). After we add the namespaces that are required (Microsoft.ServiceBus and Microsoft.ServiceBus.Messaging) we can create a new instance to our namespace manager. Using this manager we will be able to create new Service Bus Queues and access them.
When we try to consume a message from the queue we have to options. We can get a message to the queue in a transactional manner. Basically we get the message from the queue and after that we need to confirm that we processed the message (the default timeout is 60 seconds). In this period of time the message will be locked in the queue and no other receiver will be able to process it. Note: the message is not removed from the queue, is only locked. If the consumers don’t notify the queue that he processed the message, it will be unlocked and other consumers will be able to consume the message.
Another approach that can be used is to extract the message from the queue and automatically delete the message from the queue. In this way any message that is extracted from the queue is deleted automatically.
In this blog we saw how we can use Service Bus Queues in our applications. In the next step I will describe a mechanism on how we can iterate through without consuming the messages.
So, first of all you need a Windows Azure account. After you enter Windows Azure portal we need to go to Services tab and select Service Bus. On this page we will need to create a new namespace for our service. The namespace is a unique name that is used to identify a unique service (in our case the Service Bus). After we create this namespace, we already have the Service Bus Queue created automatically. In this moment we don’t need to pay for anything because we didn’t create a queue.
To be able to access the queue we need the “Default Issuer” and “Default Key”. These two strings represent our security credentials that can be used to access the queue from our application. These values can be found if we select the Service Bus namespace that we created already and click on the “View” button from the “Default Key” panel (it is in the right of the page).
Now, that we have this credentials we need to add them to our applications. To be able to access the Service Bus Queue we will security information plus the endpoint (the namespace to our Service Bus).
There are a lot of places where we can store these values. The simplest one is to hardcode this value in our application, but is not the best one. If we create a website we can use configuration file of our web application to store this information.
<configuration>
<appSettings>
<add
key="ServiceBusConnectionString"
value="Endpoint=sb://myFooNamespace.servicebus.windows.net/;SharedSecretIssuer=myFooIssuerName;SharedSecretValue=myFooDefaultKey" />
</appSettings>
</configuration>
In the case you are working with a web-role or with a worker role, a good idea is to save this information in the service definition file (*.csdef and *.cscfg).
<WebRole name="FooWebRole" vmsize="Medium">
<ConfigurationSettings>
<Setting name="ServiceBusConnectionString" />
</ConfigurationSettings>
</WebRole>
<Role name="FooWebRole"
<ConfigurationSettings>
<Setting name="ServiceBusConnectionString"
value=="Endpoint=sb://myFooNamespace.servicebus.windows.net/;SharedSecretIssuer=myFooIssuerName;SharedSecretValue=myFooDefaultKey" />
</ConfigurationSettings>
</Role>
Another possible solution is to store this information in a separately file, that can be consume by our application. In this case when we will create the instance of Service Bus Queue we will need to be able to specify this data.So now, we can look in the code to see what we need to do (write). After we add the namespaces that are required (Microsoft.ServiceBus and Microsoft.ServiceBus.Messaging) we can create a new instance to our namespace manager. Using this manager we will be able to create new Service Bus Queues and access them.
NamespaceManager nm = NamespaceManager.CreateFromConnectionString(
CloudConfigurationManager.GetSetting(“ServiceBusConnectionString”));
You will see that we have a lot of ways to create a queue. It is important at this step to know how we can specify custom properties to a queue. Before creating the queue we can have a QueueDescription object where we can define the name of the queue, the maximum size, the default time to live and so on. Using this object we can create a queue with custom setup. If we want the default queue we can specify only the name of the queue.QueueDescription qd = new QueueDescription("FooQueue");
qd.MaxSizeInMegabytes = 5120;
qd.DefaultMessageTimeToLive = new TimeSpan(0, 10, 30);
if (!namespaceManager.QueueExists("FooQueue"))
{
namespaceManager.CreateQueue(qd);
// or namespaceManager.CreateQueue(“FooQueue”);
}
Don’t forget to check if the queue exists before creating the queue. After we create a Service Bus Queue we don’t need any more the NamespaceManager instance. Using the connection string and the name of the queue we will be able to create a new instance of the QueueClient object that is able to communicate with our queue.QueueClient qc =
QueueClient.CreateFromConnectionString(
myFooConnectionString, "FooQueue");
qc.Send(message);
BrokeredMessage message = qc.Receive();
You will ask me what is the BrokeredMessage? This is the default message that is send and returned by a queue. This kind of message contains two parts:- Heather – where all the configuration properties are stored (like label and time-to-live) + custom properties that we want to set. The maxim size of the heather is 64kb.
- Body – the message body that can be any type of item that is serialized using DataContractSerializer or an IO stream.
When we try to consume a message from the queue we have to options. We can get a message to the queue in a transactional manner. Basically we get the message from the queue and after that we need to confirm that we processed the message (the default timeout is 60 seconds). In this period of time the message will be locked in the queue and no other receiver will be able to process it. Note: the message is not removed from the queue, is only locked. If the consumers don’t notify the queue that he processed the message, it will be unlocked and other consumers will be able to consume the message.
BrokeredMessage message = qc.Reiceve();
// Do some stuff with the message.
message.Complete(); // Notify the queue that the message was processed.
The default period of time that we have to notify the queue that we processed the message is 60 seconds. If we want to notify the queue that we abandoned the processing of that message we can call message.Abadon(). This kind of messaging processing is named PeerLock mode and it requests two calls from the consumer to the queue. Because of this we can increase a little the numbers of transactions.Another approach that can be used is to extract the message from the queue and automatically delete the message from the queue. In this way any message that is extracted from the queue is deleted automatically.
var message = qc.ReceiveAndDelete();
Be aware that when you call ReceiveAndDelete or Receive method, the code will freeze at that line of code until a message is available. We can specify a timeout if we want.In this blog we saw how we can use Service Bus Queues in our applications. In the next step I will describe a mechanism on how we can iterate through without consuming the messages.
Comments
Post a Comment