Azure Service Bus Queues

Introduction

The Azure Service Bus Queue is different than the Azure Storage Queues. In my lastarticle, 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.

static async Task MainAsync()  
       {  
  
          const string connectionString = ""; // get it from azure portal from service bus namespace shared                                                                           //access policy 
           const string queueName = "msgqueue";  
           var _client = QueueClient.CreateFromConnectionString(connectionString, queueName);  
           string Message = "I'm in Azure Service Bus Queue";  
           BrokeredMessage message = new BrokeredMessage(Message);  
           await _client.SendAsync(message);  
  
       }  

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

static void GetMessage()  
        {  
            const string connectionString = "";  
            const string queueName = "msgqueue";  
            var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);  
            BrokeredMessage message = queueClient.Receive();  
            string body = message.GetBody();  
            message.Complete();  
            message.Abandon();  
            Console.WriteLine(body);  
            Console.ReadLine();  
  
        }  

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

using System;  
using System.Threading.Tasks;  
using Microsoft.ServiceBus.Messaging;  
  
namespace ServiceFabricDemo  
{  
    class Program  
    {  
        static void Main(string[] args)  
        {  
            MainAsync().GetAwaiter().GetResult();  
            GetMessage();  
        }  
  
        static async Task MainAsync()  
        {  
  
            const string connectionString = "Endpoint=sb://msgdemobus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=rJ0AZa4UFgpbUN8fGL7eUJYSLfiwtlvP4mnPAXPSu68=";  
            const string queueName = "msgqueue";  
            var _client = QueueClient.CreateFromConnectionString(connectionString, queueName);  
            string Message = "I'm in Azure Service Bus Queue";  
            BrokeredMessage message = new BrokeredMessage(Message);  
            await _client.SendAsync(message);  
  
        }  
        static void GetMessage()  
        {  
            const string connectionString = "Endpoint=sb://msgdemobus.servicebus.windows.net/;SharedAccessKeyName=RootManageSharedAccessKey;SharedAccessKey=rJ0AZa4UFgpbUN8fGL7eUJYSLfiwtlvP4mnPAXPSu68=";  
            const string queueName = "msgqueue";  
            var queueClient = QueueClient.CreateFromConnectionString(connectionString, queueName);  
            BrokeredMessage message = queueClient.Receive();  
            string body = message.GetBody();  
            message.Complete();  
            message.Abandon();  
            Console.WriteLine(body);  
            Console.ReadLine();  
  
        }  
  
    }  
}  

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.

C-Sharpcorner责编内容来自:C-Sharpcorner (源链) | 更多关于

阅读提示:酷辣虫无法对本内容的真实性提供任何保证,请自行验证并承担相关的风险与后果!
本站遵循[CC BY-NC-SA 4.0]。如您有版权、意见投诉等问题,请通过eMail联系我们处理。
酷辣虫 » 后端存储 » Azure Service Bus Queues

喜欢 (0)or分享给?

专业 x 专注 x 聚合 x 分享 CC BY-NC-SA 4.0

使用声明 | 英豪名录