Windows Azure Service Bus Brokered Messaging

Download Presentation

There are many different queuing systems available like Kafka, MSMQ, RabbitMQ, Amazon SQS etc.  Azure Service Bus Brokered Messaging is a queuing system that is a scalable and feature-rich messaging service hosted in Windows Azure, or available as part of the Windows Azure Pack in your own data center.

Message Queue

Message queue provides an asynchronous, decoupled message communication between two, or more, applications. The producer can submit the message to a queue with a guaranteed delivery to one or more consumers.  This decoupling enables applications running at different times to communicate across heterogeneous networks and systems that may be temporarily offline.

With the help of message queue, the applications can work asynchronously and need not to interact with the message queue at the same interval of time. Messages in queue remains available until the receiver retrieves and act upon them. In addition, there can be more than one producers of the message.  This enable the system to be able to scale horizontally.

To read more about Azure Service Bus please read my article “Delivering Messages Made Easy With Azure Service Bus“.

 

Creating a Namespace

To create a namespace, log in to the Microsoft Azure online portal.  After logged in a Namespace can be quickly created by clicking on the New button at the top left.

azure_1

From the expanded menu, select the ‘Enterprise Integration’ option and then ‘Service Bus’.

azure_2

As do in .Net, a namespace in Service Bus is a way of scoping your service bus entities so that similar or related services can be grouped together. Several different types of entities such as queues, topics, etc. can be created within a Namespace. The namespace can also be hosted in a data center at specific location such as East US, West US, etc.  The data for that namespace will only be in that location. If a namespace already exists in the selected location, same can be selected as well.  However, The namespace name need to be globally unique because it is used as part of the URI to the service. Although, the queue name only need to be unique within that namespace.

A resource group also need to be filled in which is a collection of resources that share the same lifecycle, permissions and policies. More can be read about resource group here.

After filling the required fields, click ‘Create’’ at the bottom of the screen.  The portal will then instantly create the new namespace.

 azure_3

Creating a Queue

To create a queue, create the Queue button at the top menu as highlighted below.

azure_4

Fill in the queue name and the other details as shown below and click on click ‘Create’’ at the bottom of the screen.

azure_5

The portal will then instantly create the new queue and within a few seconds, the queue is ready to use. The details about the messages can be seen by clicking the “Overview”.

 azure_6

Retrieving the Connection String

Before starting interacting with the queue in code, the connection string of the namespace need to be retrieved from the portal.  The connection string will contain the URI and the credentials that are needed to access the queue.

On the Shared Access Policy screen a primary and secondary key are displayed for that policy.  These keys are both valid for the policy name and can be regenerated by clicking on the regenerate button on the top.  Click regenerate will disable any client that might be using that policy with the specific key.

 azure_7

 

C# Code

Once the Queue is created, Using Visual Studio, create a C# application.   Right-click on the project and select ‘Manage NuGet Packages…‘ from the context menu.

azure_8

Once the Package Manager dialog is loaded, select the ‘Online’ tab and search for ‘Service Bus‘.    Select the Windows Azure Service Bus package from Microsoft and click ‘Install‘.

 azure_9

Sending a Message

Add the following statements to the top of the class file:

using Microsoft.ServiceBus.Messaging;

Use the following code to create connection using the connection string copied out of the portal and sending the test message:

string connectionString = “”;
MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
//Sending a message
MessageSender testSender = factory.CreateMessageSender(“”);
BrokeredMessage message = new BrokeredMessage(“”);
testQueueSender.Send(message);

On executing the above code, the message will be sent and should be visible on the Portal.

The maximum size of message that can be send is 256KB, with a maximum of 64KB for the header.  The client library will break up the message into 64KB blocks to send it over the network. Therefore, the larger messages will incur more transactions.


Retrieving a Message

The following code can be used to receive the test message sent earlier, from the queue:

string connectionString = “”;
MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
//Receiving a message
MessageReceiver testReceiver = factory. CreateMessageReceiver(“”);
BrokeredMessage message = testReceiver.Receive();
String messageText = message.GetBody();
message.Complete();

As a message is retrieved from the queue it is pulled from the queue in one of two modes: either with RecieveAndDelete or PeekLock.  You can control which method you use when you generate the MessageSender.

For Example:

MessageReceivertestReceiver=factory.CreateMessageReceiver(“”,ReceiveMode.ReceiveAndDelete);

Please read this article for Using Service Bus Explorer to manage and test Service Bus Queues, topics, relays, event hubs and notification hubs.

Advertisements

Written by Varun Kumar

Varun works with Microsoft as a Cloud Consultant. He comes with 10+ years of experience into Consultant, Solution Architect, and Delivery Management roles. As a Consultant in Microsoft, his job is to design, develop and deploy enterprise level solutions using Azure, to help organizations to achieve more.

One comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: