In my previous article we have seen on how to create a new Namespace, how to create a queue and use it for point to point messaging. We created a sample consumer application and sample publisher application to demonstrate the Azure service bus messaging with queues.
If you are new to Azure service bus, I would strongly recommend to go through my previous articles before proceeding further. Below are the links of those articles:
In this article we will see how to use Topics for messaging. You will need access to Azure Portal. If you don’t have an Azure subscription, create a free account before you begin.
For this demo, I have created a sample and it is available in my GitHub repository (name TopicSample). One console application is publisher which sends messages to Azure topic, while the other console application is the consumer which consumes those messages through subscription.
Create namespace
You can create new namespace using the steps mentioned in the previous article.
Get connection string
You can get the primary connection string by following steps mentioned in previous article.
Create Topic
When you open the namespace, you will find Add Topic button on overview blade. This will open Create topic panel on the right side.
You can specify the topic name, time to live and topic size. Please keep rest of the details as they are. Only change the topic name to demo-topic. Then click on Create button.
What is Subscription ?
We have seen in my previous article that both message publisher and consumer were reading from the queues. In case of topics, the consumer reads the messages from the subscriptions.
There can be multiple subscriptions associated with the topic. You can also apply filtering to send subset of messages to specific subscription.
e.g. you may want to create one subscription for INSERT requests, while other for UPDATE requests. Lets assume that you have operationType field in your message which tells if the request is for Insert or it is for update. Then you can apply filter to send messages based on operationType field and its value.
Filters and actions are not covered in this article. By default, all messages from the topic are sent to all subscriptions
Create Subscription
For creating subscription, you can navigate to Topics blade and then click on the topic for which you want to create subscription. It will open a Subscriptions panel.
On subscriptions panel, you can check all existing subscriptions for that topic. On this panel, you can click on Add Subscription button. It will open create new subscription panel.
On Create new subscription, you can specify name of the subscription and keep rest of the fields as they are. Then click on create.
For your information below are the meanings of input fields:
- Name of the topic subscription
- Time to live, time for which message will be held in Topic
- Lock duration, time for which the message will be locked once a consumer reads it (so that it is not visible to other consumers)
- Max Delivery Count, this is the maximum number of times for which message delivery would be attempted. After that it may go to Dead letter queue subjected to your configurations.
Once subscription is deployed, you are ready for running the demo application.
Send Messages to Topic
In publisher application, you can see the code for sending the messages.
We are using TopicClient instance and calling SendAsync method on it to send message data to topic.
Below is the sample code:
// Creating topic client
var topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
// Create a new message to send to the topic
string messageBody = $"Message {i}";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
// Send the message to the topic
await topicClient.SendAsync(message);
Receive Messages from Subscription
In consumer application, you can see the code for receiving the message from subscription. It uses an instance of SubscriptionClient for receiving the messages.
The message handler instance is used to specify methods to be used during processing.
var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler)
{
MaxConcurrentCalls = 1,
AutoComplete = false
};
// Register the function that processes messages.
subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions);
The message will be available as parameter in ProcessMessageAsync method. Once processing is done, the message should be marked as complete so that it is not visible to other consumers.
static async Task ProcessMessagesAsync(Message message, CancellationToken token)
{
// Process the message.
Console.WriteLine($"Received message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}");
await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken);
}
Running the sample
You can download sample code from my GitHub repository. Then you can open the solution in Visual Studio.
You will have to specify service bus connection string and topic name in both samples. In consumer application, you will also need subscription name from which the messages should be consumed.
Once this is done, you can run those console application and that’s it. Your sample messaging solution is ready !
I hope this information is helpful for you to get started on Azure service bus topics. Please do comment and let me know your thoughts and experiences.