Azure Service Bus Queue Sample Implementation

facebooktwittergoogle_plusredditpinterestlinkedinmail

Azure Service Bus Queue Feature

In the previous post I have explained the decoupled architecture and Azure queues. As I mentioned in the previous post, we have two types of queues available in the Azure cloud. One is an Azure storage queue and the second is Service Bus. In this blog we will see the overview and implementation of the Azure Service Bus Queue.

Introduction to Azure Service Bus Queue


Azure Service Bus offers the message queue capability. It is not just a queue, It’s a part of a larger Azure messaging infrastructure. Azure Service Bus allows for high-level brokered messaging between the decoupled application components in the form of queue, topics and subscriptions, rules and actions, and event hubs. In this blog we will only consider the queuing infrastructure component of the Azure messaging infrastructure. Azure Service Bus can be up to 256 KB in message size. Only 64 KB in case of Azure storage queue. Service Bus grantees ordering (FIFO) and supports local translations.

Implement Azure Service Bus Queue

To work with Azure Service Bus We must create a queue in the Azure management portal. To create a queue, navigate to App service and click on the Service Bus. Enter queue, region, namespace and click on create a new queue. This will create a brand new queue for you.

Azure Service Bus - Create

Click on connection information button, then you can see the connection string there.

Azure Service Bus Queue - Connection String

Create one client application to consume our newly created queue. Add Azure Service Bus Nuget package.

Azure Service Bus Queue  - Nuget install

Here I am using console application to demonstrate. In the following code, I have one method for sending data to Azure Service Bus Queue. Using NamespaceManager class we can create a queue. Create a queue using connection string and Brokered Messages. Send the Brokered Message to Azure Service Bus using send method in the QueueClient class. In the following code you can see I have created 5 Brokered Messages and sent to queue.

 static void Main(string[] args)
        {
 
            string connectionString = "Endpoint=sb://azurebus-ns.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=3uFFsn8R68u8V9vo889hdov/QF3sWV7xfPxFz723J73234fdIZs4=";
            SendMessage(connectionString);
            ReceiveMessage(connectionString);
        }
  private static void SendMessage(string connectionString)
        {
            NamespaceManager namespaceManager = NamespaceManager.CreateFromConnectionString(connectionString);
 
            if (!namespaceManager.QueueExists("First_Queue"))
            {
                namespaceManager.CreateQueue("First_Queue");
            }
            QueueClient Client =
           QueueClient.CreateFromConnectionString(connectionString, "First_Queue");
 
            for (int i = 0; i < 5; i++)
            {
                // Create message, passing a string message for the body.
                BrokeredMessage message = new BrokeredMessage("Test message " + i);
                message.Properties["Message"] = "Message";
                message.Properties["Message Value"] = i;
 
                // Send message to the queue.
                Client.Send(message);
            }
            Console.WriteLine("Messages have been sent");
            Console.ReadKey();
        }

In the following code you can see the code for receiving messages from the queue. Create a MessagingFactory class instance using connection string and get the messages.

        private static void ReceiveMessage(string connectionString)
        {
            MessagingFactory factory = MessagingFactory.CreateFromConnectionString(connectionString);
 
            //Receiving a message
            MessageReceiver receiver = factory.CreateMessageReceiver("First_Queue");
            Console.WriteLine("Start reading");
            while (true)
            {
                using (BrokeredMessage retrievedMessage = receiver.Receive())
                {
                    try
                    {
                        Console.WriteLine("Message(s) Retrieved: " + retrievedMessage.GetBody<string>());
                        retrievedMessage.Complete();
                    }
                    catch (Exception ex)
                    {
                        retrievedMessage.Abandon();
                    }
                }
            }
        }

Run the application, you can see the following console.

Azure Service Bus Queue - Sending Receiving messages.

In the preceding screen you can see the ordered message. Service Bus guarantees Fist In First Out (FIFO) pattern.

See Also

  • Decoupled Architecture And Azure Queues
  • The following two tabs change content below.

    Tom Mohan

    Tom Mohan is a technologist who loves to code and build. He enjoys working on Microsoft Technologies. Tom specializes in ASP.NET MVC, Web API , Azure, C# ,WPF, SQL etc and holds a Bachelor engineering degree in Computer Science. Certification : MCSD , MCTS