Azure Service Bus

02 september 2020 om 10:00 by ParTech Media - Post a comment

Many a time, people transfer sensitive data from cloud applications to other applications without worrying much about how secure the transmission is. They fail to realize that if such sensitive information falls into the wrong hands, it can turn catastrophic for the organization.

Such organizations should embrace the Microsoft managed message broker called Azure Service Bus.

We are in an era where messaging is a very common phenomenon. It is the easiest way to transfer data from one endpoint to another. And Azure Service Bus uses messaging to transfer data. It transfers the messages through queues or topics depending upon the requirement.

These terms might be a bit blurry right now. But once you have consumed this article, you will have a better understanding of them along with answers to questions such as what is Azure service Bus and how to securely transfer data using it.

Table of Content:

  1. What is Azure Service Bus?
  2. What is the most common way of data transfer?
  3. What is a namespace?
  4. Queue v/s Topic: Which one to use and when?
  5. How to send and receive messages from a queue?
  6. Wrapping Up

What is Azure Service Bus?

Azure has a messaging service called Azure Service Bus which facilitates a connection between devices, applications, and other services running on the cloud. In Azure Service Bus, asynchronous transfer of data can be done as it can decouple applications and services which in turn leads to reliable and secure transfer of data. Azure Service Bus provides a unanimous platform for sharing data between various applications and devices.

What is the most common way of data transfer?

Transfer of data using Azure Service Bus is done commonly with the help of messages. Messages can carry text, JSON, or XML files. As services and applications are decoupled, so both the sender and the receiver need not be online simultaneously. This calls for a location where the messages can be stored securely after they are sent by the sender and until it is received by the receiver. Let us dig deeper and understand how these messages are sent from a sender to one or many receivers.

What is a namespace?

An important term that needs to be introduced here is a ‘Namespace’. It is a container where all the elements related to messages and their transfer are stored. Typically messages can be transferred through queues or topics and a namespace can store multiple queues and topics.

Queue v/s Topic: Which one to use and when?

Queue

A queue has one sender and one receiver who need not be active simultaneously. Once the sender has sent the message, it is stored in the queue up to the time the receiver receives and processes it. Queue follows the first in first out (FIFO) principle where the message that is sent first by the sender will be received first by the receiver and so on. Every message on arrival is time-stamped and stored in the queue in the order of their arrival. Messages are delivered to the receiver in pull mode i.e. only when the receiver requests for the message.

Topic

A topic has one sender but many receivers. It is used when a sender needs to send the same message to multiple receivers. The sender is the publisher and the receiver is the subscriber. A sender sends a message to a topic and then a routing rule can be set on which subscribers can request and receive the message. This is done when a message needs to be sent only to a selected number of subscribers among all the subscribers of that topic. Filters are also needed to be added when a subset of all the messages needs to be sent to a subscriber.

There are three types of filters:

  • Boolean (False/true) SQL filter
  • SQL like statement utilizing characteristics of the message
  • Correlation filters which match against a group of conditions

If none of the filters are used then a default filter gets automatically chosen. The default filter is true i.e. all subscribers will receive all the messages sent by the sender.

So it is clear now that when a message needs to be sent to one receiver a queue, is chosen for transferring the message. And when there are multiple subscribers to a single message, then a topic is chosen for transferring the message.

How to send and receive messages from a queue?

Here a console application is written to pass a message to a queue of the Azure Service Bus by utilizing a QueueClient. Using QueueClient is beneficial because it provides a simple API surface for sending a message and a simple MessagePump model for receiving a message. QueueClient is used when the user wants a quick start in receiving messages with as little coding to do as possible. These are the essentials needed for sending and receiving messages:

  • .NET Core
  • An Azure Subscription
  • An Azure Service Bus namespace
  • An Azure Service Bus queue or topic

Following steps need to be performed for sending and receiving messages from a queue:

Step 1: A console application needs to be created first. So a new .NET Core application is created.

Step 2: “Microsoft.Azure.ServiceBus": "1.0.0" needs to be added to our project.json to provide a reference to the Microsoft.Azure.ServiceBus project.

Step 3: Import the packages with the keyword “using” as mentioned in the code snippet.

Step 4: A class named “SendingReceiving” is created and the variables are declared.

Step 5: A task called “MainAsync” is created under the main method. This is the point from which execution begins. The task MainAsync calls the methods “RegisterOnMessageHandler” and “SendMessagesAsync” that are explained in the next steps.

Step 6: Next a new method “RegisterOnMessageHandler” is created that registers “ProcessMessagesAsync” and “ExceptionReceivedHandler” with the required parameters as mentioned.

Step 7: The final method “SendMessagesAsync” is created to send the desired number of messages to the queue.

Wrapping Up

The Azure Service Bus is a cloud-based service and provides a clear channel for data transfer between the sender and receiver. It increases user convenience due to its reliability and availability in market space. In this post, you have understood how to transfer data using the Azure Service Bus via queue or topic for message transfer as per your requirement. So go ahead and use this feature offered by Azure to send and receive your data.