In this post, I want to show how to create an integration pattern Publish/Subscribe with Logic Apps and Service Bus similar to what we have in the messaging with BizTalk.
The scenario (the architecture is from a real project I did while working for one of Theta’s clients, the application is made up for this post) to build these integrations is that I want to have the ability to receive order requests for particular meals and based on the content of the request I want to route to specific integrations.
Covering the basics
The picture below shows the structure I’m going to need to have this working. Here are the links for Logic Apps and Service Bus Topics creation in case you need some help in these areas.
Gastronomical advice
As you can see part of the names of the Logic Apps are not in English because I’m referring to some good restaurants we have in Sao Paulo, Brazil that I like and miss a lot by living overseas. If anyone that reads this is ever going to Sao Paulo, you need to try them.
Publisher
In the picture above I have 4 Logic Apps and one Service Bus. The first integration to be triggered is the WebAppToServiceBus. It will be receiving Http calls from an application (web or mobile) and will send a message to a Service Bus Topic by promoting some content of the request as properties on the messagebox topic. The other three Logic Apps will be triggered based on filtering the properties to receive only messages related to the meals they can make.
Subscribers
The Logic App ServiceBusToBolinha is going to make API Calls to the Bolinha restaurant API by using an HTTP connector whenever the order is for the Feijoada meal.
The Logic App ServiceBusToHamburguinho is going to send an email message to the Hamburguinho restaurant by using the Email connector whenever the order is for the X-Salada meal.
The last Logic App is ServiceBusToPastelDoKyoto that is going to send SMS messages to the Pastel do Kyoto restaurant by using the Twilio connector whenever the order is for the Pastel meal.
Service Bus Structure
The Service Bus will contain only one topic called messagebox (any similarity with names used in other integration tools are a mere coincidence – or not 😉 ). In this topic I’ll have 3 subscriptions with specific filters to match the conditions I need:
- Subscription: ToBolinha
- Filter: Meal=’Feijoada‘
- Subscription: ToHamburguinho
- Filter: Meal=’X-Salada‘
- Subscription: ToPastelDoKyoto
- Filter 1: Meal=’Pastel de Frango com Catupiry‘
- Filter 2: Meal=’Pastel de Calabresa com Queijo‘
You can use the Azure portal to create the topic, but even though you can create the subscriptions, you can’t change the filter there. You will need to use others tools for that. I’m using the Service Bus Explorer, but there’s also the ServiceBus 360 for Service Bus monitoring, or you can code one application for yourself.
Logic Apps Flows
For the Logic let me show you how I developed them.
In short, I’d like to talk about Azure Logic Apps monitoring‘s key performance metrics and gaining actionable insights to resolve problems in real-time in the other posts.
WebAppToServiceBus
The only responsibility of this Logic App is to receive an HTTP call and send a message to the messagebox topic. See that I’m sending to Service Bus the body of the message I’m receiving on the HTTP call both on the Content property as well on the Properties property. The reason for that is that we can only apply a filter on the properties of the message and not on the content.
Now that we have this Logic App, let’s make some calls and check if the messages are being published on the service bus. For this, I’ll use Postman and this is the expected JSON on the Logic App. Remember that these properties names will be used in the subscriptions filters in the Service Bus.
If the response you have is the same as above, open the Service Bus Explorer or any other tool you have and check the message.
Since my message was specifying the meal as Feijoada, I checked the subscription ToBolinha that is filtering this value. Also, note that we have in the picture the Message Text (that is the message content sent) and Message Custom Properties (that is the properties I specified in the Logic App). Again, the filter is happening on the properties, not on the content.
Try a few different values and check if the routing on the subscriptions is working ok. Once you confirm that, let’s do the other Logic Apps flows.
ServiceBusToBolinha
As you can see below, I’m using the Service Bus connector and I’m using the trigger option to receive a message in a topic subscription in peek-lock mode. I want that in case my communication with the API fails, I have the ability to leave the message in the topic to try again later. I’m using a request bin for the sake of this post.
ServiceBusToHamburguinho
For this Logic App, I’m using the Outlook.com connector, but be aware that you also have the Outlook 365 connector. The first one for Hotmail and outlook.com accounts and the second one for Outlook 365 accounts.
This time I’m using the auto-complete when picking the messages and working around email approvals to send a notification back to the client when their order is ready. Note that I’m using a few expressions to extract data from the message content or properties to use when sending emails. Check Logic App Expressions documentation.
- @{json(base64ToString(triggerBody()?[‘ContentData’]))?[‘Meal’]} -> to get the Meal name
-
@{triggerBody()?[‘Properties’]?[‘Email’]} -> to get the email of the client
ServiceBusToPastelDoKyoto
In the last Logic App I will use the Twilio connector to send SMS messages. The idea is that this restaurant don’t have much more than a phone to receive SMS messages since they have only a tent in a street farmers market.
When I make calls on the first Logic App passing the parameters that match with the filters for this Logic App, I’ll receive the SMS on the phone number that was specified on the request as you can see below.
Summary
So to finalize, you see how easy is to implement the same architecture that we have in integration products like BizTalk easily. I just used Logic Apps flows, but it’s possible to implement also with Azure Functions.
Hope you guys like, and please give me some comments and advice on this post and ideas for next posts.
1 thought on “Publish/Subscribe with Logic Apps and Service Bus”