Azure Service Bus - Peek-Lock A Message From Queue Using Azure Logic Apps

In this article, we will understand the basics of Azure Service Bus peek-lock concept and see how you can retrieve a message in the peek-lock mode using Azure Logic Apps.

Prerequisites and assumptions for this exercise 
  • Microsoft Azure account - You can get started with a free Azure account from here.
  • You need to know the basics of creating an Azure Logic App. Please read the first half of my earlier article where I have defined the process of creating a new Logic App.
Azure Service Bus

Azure Service Bus is a reliable cloud-based Messaging as a Service (MaaS) used mainly for simplifying the enterprise messaging capabilities. Azure Service Bus is a multi-tenant service that supports hybrid integration in a very simple way using the two different messaging patterns.
  • Azure Service Bus (Brokered) Messaging
  • Azure Relay
Azure Service Bus (Brokered) Messaging includes Queues and Topics as the main communication services while Azure Relay offers support to different transport protocols and web services such as REST, SOAP, and WCF. 

Queue

Queues allow a unidirectional way of communication between devices based on a First In First Out (FIFO) message delivery mechanism. The communication is only one way and the sender needs not have to wait to receive a response from the receiver end in order to process the messages.

Topic (and Subscriptions)

On the other hand, Topics offer a one-to-many messaging mechanism where a single published message is capable of being retrieved by multiple subscribers depending on predefined filter rules applied for every subscriber. For example, let's say Purchase Orders (POs) are being sent through the topic. Say, one subscriber can have filter rules to receive the POs with the value greater than 10,000. Similarly, another subscriber can only receive POs whose purchase request is from a particular party. Messages sent to the topic are not directly received by the receivers; instead, they are received from the subscriptions.

What is Peek-Lock a Message?

Peek-lock(ing) a message means retrieving the message from the queue but locking the message for further processing. This is also commonly referred to as Non-Destructive Read. When a message is peek-locked, it will not be available for any other receivers for the valid lock duration. Post the lock duration time period, the message will be available for the rest of the receivers. It's as simple as reading a message that comes to the queue and then, removing the message from the queue by setting a status as "Complete".
 
In this article, I will show you how you can receive a message to the Azure Service Bus Queue, Peek-lock the message to read the content, and then remove the message from the queue. Let's get started!

Create a Service Bus Queue in Microsoft Azure

The first step is to create a queue in the Azure Portal. Follow the steps shown below to create a service bus namespace and the queue.
  1. Log in to Azure Portal.
  2. Click "+ New" on the left dashboard menu and select "Internet of Things" under Azure Marketplace. In the search bar, enter "Service Bus". Click Service Bus from the list of results.



  3. Click "Create" on the Service Bus introduction screen.



  4. The first step in creating a Service Bus Queue or Topic is to create a namespace. The namespace acts as a container for the Service Bus resources within your application. Enter the following details to create the Azure Service Bus Queue.

    1. Friendly name for the Azure Service Bus namespace
    2. Choose your appropriate pricing tier (Basic/Standard/Premium)
    3. Choose your Azure Subscription
    4. Choose to create a new user group or choose an existing one. Select a location from the drop-down.
    5. Select the "Pin to Dashboard" checkbox if you prefer to have the Service Bus listed as a widget on your Azure Portal dashboard.
    6. Click "Create" to create the Azure Service Bus namespace.



  5. Once the Azure Service Bus namespace is created, you can create the Queue/Topic within the namespace. Let's start creating a queue. Follow the steps as shown below to create a queue.

    1. In the namespace screen, click Queue.
    2. In the "Create Queue" balde, enter the friendly name for the Azure Service Bus Queue.
    3. Choose the size of the queue from the drop-down.
    4. Set the Message Time to Live (TTL) and Lock Duration from the drop-down.
    5. Select the checkbox if you wish to move the expired messages to deadletter queue, enable duplicate detection, enable sessions, enable partitioning.
    6. Click "Create" to create the queue.




  6. Now, that the Queue is created, let's create the Azure Logic App. Follow the steps shown in this article to create the Logic App.


  7. Let's build the Logic App. Open the Logic App. In the Logic Apps Designer screen, select the "Peek-lock receive a Service Bus message and complete it" template and click "Use this template".



  8. Enter your Service Bus account details (that we created in step 4). Provide a name and authorization for the Logic App to connect with Service Bus. Once the connection is established, the Logic App will be displayed with the trigger and action steps.





  9. For this scenario, once a message is received in the queue, we will peek-lock on the message and view the message details. By default, the lock duration is set to 1 minute until the lock expires and the message will be visible to other subscribers. To add some delay into the processing, we will introduce a "Delay" step in the Logic App to complete the message. Once the message is completed, we will pass the message lock token as an email notification. The Logic App will look as shown below.


To mimic a real business scenario, we have different tools and offerings in the market using which we can send messages to the Azure Service Bus Queue. In my experience, the most prominent tools used by customers are,
  • Service Bus Explorer
    an open source community tool developed by Paolo Salvatori (a Microsoft employee in the globally distributed Windows Azure Customer Advisory Team)

  • ServiceBus360
    ServiceBus360 is a SaaS based advanced Operations, Monitoring, and Analytics tool that enhances the day to day working experience with Microsoft Azure Service Bus. ServiceBus360 makes it easy for anyone who is using Azure Service Bus entities (Queues, Topics, Relays and Event Hubs)
I will show you how you can use both these products to send messages to the Azure Service Bus Queue.

Sending Messages into Queue using Service Bus Explorer

You need to download the Service Bus Explorer from the above link. This is a desktop tool that you will use to send messages to your Azure Service Bus queues (and topics). Once you have set up Service Bus Explorer on your machine, open the application and connect your Service Bus namespace. Once the connection is established, you will see the list of queues and topics under the namespace listed in the namespace pane (on the left).

 


To send a message from Service Bus Explorer into the queue, right-click the queue name from the namespace pane, select "Send Messages". In the Send Messages pane, enter the XML message to be sent and click Start. This will send messages into the queue.

 

 

Sending Messages into Queue using ServiceBus360

In order to send messages to Queue or Topic, you need to first create an account with ServiceBus360. Then, you need to associate the namespace (using the namespace connection string) that we created earlier in order to view the queues and topics within the ServiceBus360 portal.

 
 

Once the queue(s) and topics(s) are added into ServiceBus360, simply select the queue (ellipses icon) and click Send Messages. Simply add the XML message, set the count of messages to be sent to the queue, and optionally save the configuration (if you wish to send more messages in future). Click Send to send the messages into the queue.

 
 

When the Logic App trigger executes during the next cycle (3 minutes), the messages will be peek-locked, and then further completed. Once the messages are completed, the Logic App will trigger an email notification with the message content, message ID, and Sequence number.

 

Therefore, with this method, you get to peek-lock into a message in an Azure Service Bus Queue before actually processing the message using Azure Logic Apps.