Azure Service Bus Topics

Introduction

In my last article, I discussed about Azure Service Bus Queue, which is basically a one to one communication. This article tells you how to work with Azure Service Bus topics with a sample console application.

Content

  • Service Bus Topics
  • Create Service Bus Topics and subscriptions on the Azure portal
  • Send and receive a message using service bus topics

Pre-request

  • Visual Studio 2017 update 3 or later
  • Azure Subscription
  • Basic Knowledge of the Azure portal
Azure Service Bus Topics

Topics is similar to Queue but Inside a topic, we have subscriptions and each subscription will have multiple subscribers. The process is simple - just put data to the subscription and one or more subscriber will pull the data out.

Creating a Service Bus Topic in Azure Portal

Step 1

Login into Azure Portal https://portal.azure.com/

Step 2

I’m going to use an existing Azure Service Bus namespace which is used for creating a Queue, please refer to the last article where I have explained how to create an Azure Service Bus and it’ namespace.

Step 3

Once the Azure Service Bus has been created successfully, now, we can start to create a topic for the service bus. Working with topics is similar to queue in Azure Service Bus.

Step 4

Go to Azure service bus namespace. In my case I’m using my existing namespace msgdemobus .

Step 5

In overview, you can find a topic, click on it to add a new topic as shown in the below figure.

 

Step 6 - Add Topic

  • Give the name for the topic, in my case I named it as mytopics
  • Set a max topic size based on the requirement
  • Time for message to live --  by default it will be 14 days
  • By enabling duplicate detection, topic will not store any duplicate messages
  • By enabling the partioning, the topics itself spreads into multiple storage systems, that means more than one message broker will    be backing up the message.
 

Finally click on create.

Step 7

Once the topic is created successfully, we are able to create a subscription for the topic. In topic overview, click on subscription as shown below.

Step 8

Add the subscription, name the subscription, and I'm going with default values for other fields, as shown below,

 
Send and receive a message using Service Bus Topics

I’m going to create a new console application using C# to send and receive a message with Azure Service Bus Topics using Topic Client.

MyTopicApp.cs

  1. using Microsoft.ServiceBus.Messaging;  
  2. using System;  
  3.   
  4.   
  5. namespace MyTopicApp  
  6. {  
  7.     class Program  
  8.     {  
  9.         private static string _serviceBusConn = "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<policy name>;SharedAccessKey=<key>";  
  10.         private static string _serviceBustopic = "<Topic Name>";  
  11.   
  12.         static void Main(string[] args)  
  13.         {  
  14.             SendMessage("Hello I'm Azure Service Bus Topic ");  
  15.         }  
  16.   
  17.         static void SendMessage(string message)  
  18.         {  
  19.             var topicClient = TopicClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic);  
  20.             var msg = new BrokeredMessage(message);  
  21.             topicClient.Send(msg);  
  22.         }  
  23.   
  24.         static void ReadMessage()  
  25.         {  
  26.             var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "<subscription name>");  
  27.             subClient.OnMessage(m =>  
  28.             {  
  29.                 Console.WriteLine(m.GetBody<string>());  
  30.             });  
  31.         }  
  32.     }  
  33. }  

_serviceBustopic will holds a topic Name.  

_serviceBusConn will hold the Azure service bus connection string, we can get a shared access Name and key from Azure service bus shared access policy as showed in the below figure.
 
 
 Policy Name  
 
Pick a key.

Send Message

SendMessage function is used to send a message to Azure service bus topic using topic client and the BrokeredMessage is used to hold the message.

  1. static void Main(string[] args)  
  2.        {  
  3.            SendMessage("Hello I'm Azure Service Bus Topic ");  
  4.        }  
  5.   
  6.        static void SendMessage(string message)  
  7.        {  
  8.            var topicClient = TopicClient.CreateFromConnectionString(_conn, _topic);  
  9.            var msg = new BrokeredMessage(message);  
  10.            topicClient.Send(msg);  
  11.        }  
Run the program

Switch to Azure portal, you can notice the message count will be updated from 0 to 1. 

 
Read Message

ReadMessage function is used to received a message based on the subscription using subscription client.

  1. static void Main(string[] args)  
  2.        {  
  3.            ReadMessage();  
  4.            Console.ReadKey();  
  5.        }  
  6.  static void ReadMessage()  
  7.        {  
  8.            var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "MyAppSubscription");  
  9.            subClient.OnMessage(m =>  
  10.            {  
  11.                Console.WriteLine(m.GetBody<string>());  
  12.                 
  13.            });  
  14.        }  
Run a program

Result
 
 
Message is received from topic through subscription.
 
Switch to the Azure portal and you can notice the message count will be updated from 1 to 0. 
 
I hope you have enjoyed this article. Your valuable feedback, questions, or comments about this article are always welcome.