Try for free Book a demo

Auto Filter Messages into Subscriptions in Azure Service Bus Topic

Azure Monitoring

5 Mins Read

Azure Service Bus Topic Filter

Understanding Azure Service Bus Topic Filters, Subscriptions & Actions

Topics

Topic is a logical channel to which publishers send messages. Topics can be employed when several subscribers wish to subscribe to a specific set of messages. Messages sent to a topic are then forwarded to its associated Subscriptions.

Azure Service Bus Topic

Subscriptions

Subscription is a named view of a topic’s messages, allowing subscribers to receive and process messages based on specific criteria. Subscriptions can have unique filtering rules that determine the subset of messages they are interested in. Subscription rules include two parts: “filter” and “action.” Every message sent to the Topic gets evaluated in every Subscription against the rule defined and gets into the Subscription if the condition is satisfied.

Read more: Manage Azure Topic Subscription Rules

Azure Service Bus Subscriptions

Filters

Filter is a set of rules or conditions defined on a Subscription to filter the incoming messages from a topic. It allows you to specify criteria based on message properties or content, and messages matching these criteria are routed to the associated Subscription. Filters are crucial in controlling and optimizing message distribution within your messaging system.

Actions

Action is a component of a Subscription rule. The action defines what should happen to a message that matches the filter criteria. Typically, it involves annotating or modifying the message’s user-defined properties or content to customize its handling within the Subscription. Creating actions in Azure Service Bus is not directly possible through the Azure portal. Instead, you can use .NET code, with support from the “Azure.Messaging.ServiceBus” NuGet package to craft rules and actions for message processing.

Here’s a sample .NET code for defining Filter and Action:

var serviceBusClient = new ServiceBusAdministrationClient(serviceBusConnectionString);     

await serviceBusClient.CreateRuleAsync(TopicName, SubscriptionName, new CreateRuleOptions 

{ 

    Name = RuleName, 

    Filter = new SqlRuleFilter("user.age > 60"), 

    Action = new SqlRuleAction("SET SeniorCitizien = ‘YES’;") 

}); 

Azure Service Bus Topic message filtering

Consider an Order Processing scenario where a message must be sent to different subscribers like Invoice generation, Dispatching unit, Delivery management, etc. when an order placement flow is initiated. The message content can be similar between a few departments where it will be appended with sensitive information for departments like Payment processing.

Service Bus Topics are the preferred solution as this integration scenario requires a queueing mechanism with multiple subscribers. Multiple Subscriptions can be created under a Topic representing each department. Filters can be created in the Subscription to receive only the relevant messages sent to the Topic by ensuring that the respective departments will process only the appropriate messages.

Azure Service Bus Topic message filtering

Types of Azure Service Bus Topic Subscription Filters

  • SQL Filter
  • Boolean Filter
  • Correlation Filter

SQL Filter

A SQL Filter contains a SQL-like condition evaluated against message properties. It supports system properties with the sys. prefix. The SQL subset covers property existence, null values, logical operations, relational operators, numeric arithmetic, and text pattern matching using LIKE.

Here’s a sample .NET code for defining a SQL filter:

serviceBusClient = new ServiceBusAdministrationClient(connectionString);     

// Create a an SQL filter with seniorcitizien set to true and seatCount to 2 

var serviceBusClient = new ServiceBusAdministrationClient(serviceBusConnectionString);     

await serviceBusClient.CreateSubscriptionAsync( 

new CreateSubscriptionOptions(TopicName, SubscriptionName),  

new CreateRuleOptions(RuleName, new SqlRuleFilter("seniorcitizien = ’true’ AND seatCount = 2")) 

); 

Boolean Filter

The TrueFilter and FalseFilter are derived from the SQL filter and have distinct effects. The TrueFilter selects all incoming messages (true), while the FalseFilter selects none of the arriving messages (false) for the Subscription.

Here’s a sample .NET code for defining a Boolean filter:

// Create a False Rule filter with an expression that always evaluates to False. 

Note: It's equivalent to using an SQL rule filter with 1 != 1 as the expression. 

var serviceBusClient = new ServiceBusAdministrationClient(serviceBusConnectionString);     

await serviceBusClient.CreateSubscriptionAsync( 

new CreateSubscriptionOptions(TopicName, SubscriptionName),  

new CreateRuleOptions("Bookings", new FalseRuleFilter()) 

); 

Correlation Filter

A Correlation Filter evaluates conditions against message properties like correlationId, contentType, and others (includes user-defined properties.). A match occurs when the incoming message’s property equals the filter’s specified value.

Here’s a sample .NET code for defining a correlation filter:

// Create a correlation filter with priority set to High 

var serviceBusClient = new ServiceBusAdministrationClient(serviceBusConnectionString);     

var filter = new CorrelationRuleFilter(); 

filter.Subject = “TicketBooking"; 

filter.ReplyTo = "etickets@irtct.co.in"; 

filter.ApplicationProperties["Name"] = "Raju"; 

filter.ApplicationProperties["age"] =  55; 

await serviceBusClient.CreateSubscriptionAsync( 

new CreateSubscriptionOptions(TopicName, SubscriptionName),  

new CreateRuleOptions(RuleName, filter ) 

); 

Note: String comparisons are case-sensitive. Multiple conditions are combined using a logical AND operator. A match occurs when the incoming message’s property exactly matches all the specified values in the filter.

Third-Party Applications

Topic Subscription Rules can be managed through different third-party tools like Azure Service Bus Monitoring Tool by Turbo360.

Conclusion

In modern integrations, it is an essential requirement to have a component that supports subscriber patterns. Azure Service Bus Topic Subscriptions are simple and come with excellent filtering capabilities. Defining Actions to Azure Service Bus Topic filters to modify the messages will reduce numerous manual efforts. Topic Subscriptions also support other crucial features like dead lettering to handle the failures in the messages. Service Bus Topics are the best enterprise-grade solution for users with complex integrations.

Related Reading

This article was published on Sep 14, 2023.

Azure Service Bus - Request Demo CTA

Related Articles