Azure Service Bus Queues

Introduction

The Azure Service Bus Queue is different than the Azure Storage Queues. In my last article, we have seen how to get started with Azure Service Bus by creating a namespace. This article tells you how to work with Azure Service Bus Queues with a sample console application.

Content

  • Service Bus Queues
  • Create Service Bus Queues in Azure portal
  • Send and receive a message using service bus Queues

Pre-request

  • Visual Studio 2017 update 3 or later
  • Azure Subscription

Service Bus Queues

  • Service Bus Queues provide a queueing mechanism
  • Message will appear only once
  • Message is processed using FIFO(First In First out) pattern
  • Support transactions
The message lock can be renewed, It consists of few major parts,
  1. Body – The body can be serialized object or a stream
  2. Label – Simple text label
  3. Time to Live – How long the message is stored in queue
  4. Properties – Dictionary of properties that can be used by your specific consume

Create a service bus

Step 1

Log in to the Azure portal (http://portal.azure.com ), using your Azure account.

Step 2

Go to the service bus namespace (refer my previous article to create a namespace) which is already created.

Figure 1: Service Bus Namespace

Step 3

Click on Queues in Entity and create a queue as shown in below figure.

Figure 2

Figure 3

Give a name for the queue, in my case I named it as msgqueue.

Message time to live – by default it will be 14 days, it means how long the message will be stored in queue. Keep the rest of the option to default setting and click on create

Send and receive a message using service bus Queues

Create a new console application project in Visual Studio 2017.

Download the service bus queues messaging package from NuGet.

Figure 4: Nuget

Write the below function in Program.cs file.

  1. static async Task MainAsync()  
  2.        {  
  3.   
  4.           const string connectionString = "<Namespace connection String>"; // get it from azure portal from service bus namespace shared                                                                           //access policy 
  5.            const string queueName = "msgqueue";  
  6.            var _client = QueueClient.CreateFromConnectionString(connectionString, queueName);  
  7.            string Message = "I'm in Azure Service Bus Queue";  
  8.            BrokeredMessage message = new BrokeredMessage(Message);  
  9.            await _client.SendAsync(message);  
  10.   
  11.        }  

Queue client is an abstract class, where the function CreateFromConnectionString (string, string) is used to create a new copy of Queue Client from the connection string with specified queue path.

BrokeredMessage is a sealed class; here, it is used to initialize the new instance and to serialize the message which is sent to the Queue with the help of queue client. From the above code, it is obvious that the message with string type is serialized and send to queue using Queue Client.

Check the stats in Azure

 
                                                Figure 5 

Receive a Message

  1. static void GetMessage()  
  2.         {  
  3.             const string connectionString = "<Namespcae connection string>";  
  4.             const string queueName = "msgqueue";  
  5.             var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);  
  6.             BrokeredMessage message = queueClient.Receive();  
  7.             string body = message.GetBody<string>();  
  8.             message.Complete();  
  9.             message.Abandon();  
  10.             Console.WriteLine(body);  
  11.             Console.ReadLine();  
  12.   
  13.         }  

The above function is used to receive the message from the queue using the QueueClient and get the body of the message using the brokeredMessage.

Program.cs
  1. using System;  
  2. using System.Threading.Tasks;  
  3. using Microsoft.ServiceBus.Messaging;  
  4.   
  5. namespace ServiceFabricDemo  
  6. {  
  7.     class Program  
  8.     {  
  9.         static void Main(string[] args)  
  10.         {  
  11.             MainAsync().GetAwaiter().GetResult();  
  12.             GetMessage();  
  13.         }  
  14.   
  15.         static async Task MainAsync()  
  16.         {  
  17.   
  18.             const string connectionString = "Endpoint=sb://msgdemobus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=rJ0AZa4UFgpbUN8fGL7eUJYSLfiwtlvP4mnPAXPSu68=";  
  19.             const string queueName = "msgqueue";  
  20.             var _client = QueueClient.CreateFromConnectionString(connectionString, queueName);  
  21.             string Message = "I'm in Azure Service Bus Queue";  
  22.             BrokeredMessage message = new BrokeredMessage(Message);  
  23.             await _client.SendAsync(message);  
  24.   
  25.         }  
  26.         static void GetMessage()  
  27.         {  
  28.             const string connectionString = "Endpoint=sb://msgdemobus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=rJ0AZa4UFgpbUN8fGL7eUJYSLfiwtlvP4mnPAXPSu68=";  
  29.             const string queueName = "msgqueue";  
  30.             var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);  
  31.             BrokeredMessage message = queueClient.Receive();  
  32.             string body = message.GetBody<string>();  
  33.             message.Complete();  
  34.             message.Abandon();  
  35.             Console.WriteLine(body);  
  36.             Console.ReadLine();  
  37.   
  38.         }  
  39.   
  40.     }  
  41.  
Output
 
Message received from Queue as shown below.
 
 
Figure 6 : Message received from Queue
 
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome. 

Up Next
    Ebook Download
    View all
    Learn
    View all