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
- using Microsoft.ServiceBus.Messaging;
- using System;
-
-
- namespace MyTopicApp
- {
- class Program
- {
- private static string _serviceBusConn = "Endpoint=sb://<namespace>.servicebus.windows.net/;SharedAccessKeyName=<policy name>;SharedAccessKey=<key>";
- private static string _serviceBustopic = "<Topic Name>";
-
- static void Main(string[] args)
- {
- SendMessage("Hello I'm Azure Service Bus Topic ");
- }
-
- static void SendMessage(string message)
- {
- var topicClient = TopicClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic);
- var msg = new BrokeredMessage(message);
- topicClient.Send(msg);
- }
-
- static void ReadMessage()
- {
- var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "<subscription name>");
- subClient.OnMessage(m =>
- {
- Console.WriteLine(m.GetBody<string>());
- });
- }
- }
- }
_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.
- static void Main(string[] args)
- {
- SendMessage("Hello I'm Azure Service Bus Topic ");
- }
-
- static void SendMessage(string message)
- {
- var topicClient = TopicClient.CreateFromConnectionString(_conn, _topic);
- var msg = new BrokeredMessage(message);
- topicClient.Send(msg);
- }
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.
- static void Main(string[] args)
- {
- ReadMessage();
- Console.ReadKey();
- }
- static void ReadMessage()
- {
- var subClient = SubscriptionClient.CreateFromConnectionString(_serviceBusConn, _serviceBustopic, "MyAppSubscription");
- subClient.OnMessage(m =>
- {
- Console.WriteLine(m.GetBody<string>());
-
- });
- }
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.