Use Case: Organization X has a large-scale distributed serverless application environment. There are some azure components that sends a private messages which need to be accessed only once by other components using RoleBasedAccessControls. There are some messages that need to Fan-Out(one-to-many) to large number of systems, where sender doesnt need to know the receiver's details. The message should stay until time out, for receiving components to consume when they are avilable.
In the earlier article, we have seen how we can use Azure Service Bus Queues for integration of serverless application environment. Now we will explore Service Bus Topic and how it will be used.
We have already created Azure service Bus in earlier article, you can reffer the link provoided at the top for details.
Step 1: Creating Azure Service Bus Topic
Go to azure portal, and open the Service Bus we created "DemoAZSvcBus". In left pannel under "Entities", click "Topics".
az login
az servicebus namespace authorization-rule keys list --resource-group DefaultRG --name RootManageSharedAccessKey --query primaryConnectionString --output tsv --namespace-name DemoAZSvcBus
dotnet new console -n CorpAnnouncementSender
CD .\CorpAnnouncementSender\
dotnet add package Microsoft.Azure.ServiceBus
Code .
using System;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.Azure.ServiceBus;
namespace CorpAnnouncementSender
{
class Program
{
const string ServiceBusConnectionString = "Service Bus Connecttion string goes here";
const string TopicName = "corpannouncements";
static ITopicClient topicClient;
static void Main(string[] args)
{
Console.WriteLine("Sending a message to the corporate annpuncements topic...");
SendCorpAnnouncementAsync().GetAwaiter().GetResult();
Console.WriteLine("Message was sent successfully.");
}
static async Task SendCorpAnnouncementAsync()
{
topicClient = new TopicClient(ServiceBusConnectionString, TopicName);
try
{
string messageBody = $"Organization X going public IPO in Jan 2022";
var message = new Message(Encoding.UTF8.GetBytes(messageBody));
Console.WriteLine($"Sending message: {messageBody}");
await topicClient.SendAsync(message);
}
catch (Exception exception)
{
Console.WriteLine($"{DateTime.Now} :: Exception: {exception.Message}");
}
await topicClient.CloseAsync();
}
}
}
dotnet build
dotnet run -p CorpAnnouncementSender
Unlike Queue, the messages in Topics will be tunnel to subscriptions and they will stay there for the subscribers to consume. They stay there until message timeout.
dotnet new console -n CorpAnnouncementReceiver
CD .\CorpAnnouncementReceiver\
dotnet add package Microsoft.Azure.ServiceBus
Code .
using System; using System.Text; using System.Threading; using System.Threading.Tasks; using Microsoft.Azure.ServiceBus; namespace CorpAnnouncementReceiver { class Program { const string ServiceBusConnectionString = "Service Bus Connection string here";
const string TopicName = "corpannouncements"; const string SubscriptionName = "NZSubscription"; static ISubscriptionClient subscriptionClient; static void Main(string[] args) { MainAsync().GetAwaiter().GetResult(); } static async Task MainAsync() { subscriptionClient = new SubscriptionClient(ServiceBusConnectionString, TopicName, SubscriptionName); Console.WriteLine("======================================================"); Console.WriteLine("Press ENTER key to exit after receiving all the messages."); Console.WriteLine("======================================================"); RegisterMessageHandler(); Console.Read(); await subscriptionClient.CloseAsync(); } static void RegisterMessageHandler() { var messageHandlerOptions = new MessageHandlerOptions(ExceptionReceivedHandler) { MaxConcurrentCalls = 1, AutoComplete = false }; subscriptionClient.RegisterMessageHandler(ProcessMessagesAsync, messageHandlerOptions); } static async Task ProcessMessagesAsync(Message message, CancellationToken token) { Console.WriteLine($"Received corp announcement message: SequenceNumber:{message.SystemProperties.SequenceNumber} Body:{Encoding.UTF8.GetString(message.Body)}"); //Uncomment below line if you want to delete the message from subscription //await subscriptionClient.CompleteAsync(message.SystemProperties.LockToken); } static Task ExceptionReceivedHandler(ExceptionReceivedEventArgs exceptionReceivedEventArgs) { Console.WriteLine($"Message handler encountered an exception {exceptionReceivedEventArgs.Exception}."); var context = exceptionReceivedEventArgs.ExceptionReceivedContext; Console.WriteLine("Exception context for troubleshooting:"); Console.WriteLine($"- Endpoint: {context.Endpoint}"); Console.WriteLine($"- Entity Path: {context.EntityPath}"); Console.WriteLine($"- Executing Action: {context.Action}"); return Task.CompletedTask; } } }
dotnet build
dotnet run -p CorpMessageReceiverr
No comments:
Post a Comment