On my previous blog post, I shared my live tech talk experienced. I talked about an overview of Azure Service Bus. A cloud messaging service that can be used to connect or integrate applications, devices and services to another.
Azure Service Bus solves complex messaging problems that many developers have to solve. We aim to develop quality and reliable cloud messaging solutions in our applications. The service Service Bus guarantees reliability, scalability, and security in messaging delivery. Send and receive reliable asynchronous messages or data that you can scale and secure.
Azure Service Bus Key Features
- Message Sessions through FIFO (First In, First Out) ordered unbounded sequence of messages
- Auto-Forwarding chains a subscription or queue to another, can automatically remove messages from queue/topics and puts them to another
- Dead Letter Queue (DLQ) holds undelivered messages, lets you inspect and handle them
- Scheduled Delivery delivers your messages in a specific time
- Message Deferral defers the message retrieval for a later time
- Client-side Batching allows batching of messages in a time period
- Auto-Delete On Idle feature lets you manage and set the idle interval time after which a queue is deleted
- Duplicate Detection gives the ability to resend the same message for recurring tasks or discard any duplicate copies
Azure Service Bus Integrations and Libraries
Solving complex messaging problems is one of the main uses of Service Bus. How you build your architecture, do the implementation and choose the integration depends on the consumer needs. Each use-case scenario is unique. Maybe your application does not require the use of Service Bus. Therefore, it is important to clearly identify what your customers or users needs.
Service Bus in Microsoft Azure cloud platform supports different programming languages. You can develop apps with it using C# .NET/.NET Core, Java, JMS (Java Messaging Service) and other supported-languages. It can be integrated with other cloud services too. For example, integration with Event Grids, Logic Apps, Azure Functions, Dynamics 365, or Azure Stream Analytics in your applications.
Below shows an example of an integration with other cloud services using Event Hubs.
Creating a Namespace is vital in the development and integration of your applications with Azure Service Bus. Queues and Topics/Subscription should be in a Namespace. It serves as a container for different resources for the Service Bus within your app.
Azure Service Bus Queue
So how does Azure Service Bus Queues work?
If you have worked with queues in programming, you are probably familiar of concept of FIFO (First In, First Out) message delivery.
So what is special with Service Bus Queues? How does it differ from a normal queue?
Service Bus Queue is an asynchronous message broker. It is an entity where all your messages are stored and then distributed to different receivers. These receivers can be a service or an application.
A common scenario is communication between worker roles and web in an Azure app that have multiple tiers. Even solving communication problems or creating features in a hybrid cloud solution between on-premises apps and Azure-hosted apps.
Code Example for an Azure Service Bus Queue (The Sender)
Above code is part of a simple console app example. It has an asynchronous Main-method with a new instance of a QueueClient. The QueueClient then takes two parameters : the connection string of the Service Bus and the name of your Queue. These information can be retrieved in Azure Portal. Right after the successful creation of Service Bus Namespace and when the Queue is created in it.
There are several ways to create a Service Bus Queue. The easiest one (without too much coding) is to create it via the Azure Portal. However, if you want to learn more and develop your skills in cloud command lines, you can use Azure Cloud Shell or Azure Powershell. If you have an existing ARM template ready, you can also use it to easily create a queue.
It is important to keep your Service Bus credentials secured. Using Azure Key Vault or saving your connection strings, name and keys in a secured configuration file are highly recommended.
Code Example for an Azure Service Bus Queue (The Receiver)
The above code shows an example console app that receives waiting messages sent by the Sender app through Azure Service Bus Queue.
You set custom parameters using MessageHandlerOptions. You can set how many messages you want to de-queue at a time and AutoComplete. For more detailed guide on parameters and this class, please check out the class library for MessageHandlerOptions.
So, how do you verify if the Receiver received all the messages?
You can verify it by logging it as output to your console. Save the logs to the database if necessary. Another way is to administrate and manage diagnostic logs of Azure Service Bus Queues in the Azure Portal. For code samples of Service Bus queues, check out Service Bus messaging samples.
Topics and Subscriptions
If you want to deliver messages in a subscription way (publish-subscribe), then use the Topics and Subscriptions. Topic serves as a message broker with filter options such as SQL Filters, Correlation Filters, and Boolean Filters.
A distributed application’s components exchange messages through Topic that acts as message broker. The topic then asynchronously distributes the messages to all receiver subscribed on every subscription.
Topic Sender Code Example – Sends Messages to TopicClient()
Topic Receiver Code Example – Receives messages from Sender through SubscriptionClient()
Learn More By Coding + Studying!
The overview and code examples above are just basic samples. If you want to dig deeper into using Service Bus in your Azure applications, I recommend the following documentation and online courses.
- Microsoft Service Bus Documentation
- Recommended Service Bus Online Courses – Azure Service Bus by Salik Malik (LinkedInLearning) , Microsoft Azure Service Bus In-depth by Alan Smith (Pluralsight) and Azure Essential Training for Developers by Adrienne Tacke (LinkedInLearning)
I am preparing for an Azure Certification exam AZ-204. Service Bus is just a tiny bit one the many Azure services that will be covered in it. If you are preparing for it too or just want to learn develop with Service Bus, I recommend checking out the links above.
Jonah C. Andersson is a Software Developer/Engineer who codes C#.NET/NET Core full-stack and develops applications with cloud services. She is passionate about continuous learning and developing tech solutions with modern technologies such as Microsoft Azure. She makes a difference by sharing her tech knowledge through mentorship and tech blogging. For questions about this article, contact Jonah or connect via LinkedIn, follow her on Twitter and GitHub