How To Create Sql Filter On Service Bus Topic Subscription Using Azure Portal
Azure Service Bus Topic Filters
In this post, let's have a look at a nice feature that comes with Azure Service Bus Topics/Subscriptions and that is Topic Filters.
Usually, when we publish a message nether a Topic, all its subscribers will receive a copy of the message. Only sometimes, we want to route specific messages only to a specific subscription based on the bulletin. For case, permit'due south say we have a Topic and a Subscription, and the Subscription is session-enabled. And once sessions are enabled in a queue/subscription, it can decrease the throughput. And for some messages, nosotros might not actually need to apply Sessions, because we don't care nearly the society of processing. In that case, information technology makes sense to use a different subscription without sessions and route the messages which don't crave sessions there. And Topic Filters can be quite handy here.
Now permit's see the things in action.
First, let's go alee and create a Topic.
using Azure.Messaging.ServiceBus; using Azure.Messaging.ServiceBus.Administration; var connectionString = "<ServiceBus_ConnectionString>"; var TopicName = "sbt-test-topic"; var SimpleSubscriptionName = "sbs-simple-subscription"; var SessionEnabledSubscriptionName = "sbs-session-enabled-subscription"; var serviceBusClient = new ServiceBusClient(connectionString); var serviceBusAdministrationClient = new ServiceBusAdministrationClient(connectionString); if (!await serviceBusAdministrationClient.TopicExistsAsync(TopicName)) { await serviceBusAdministrationClient.CreateTopicAsync(TopicName); }
And now I am going to create ii subscriptions, one with Session enabled and i without.
if (!expect serviceBusAdministrationClient.SubscriptionExistsAsync(TopicName, SimpleSubscriptionName)) { wait serviceBusAdministrationClient.CreateSubscriptionAsync( new CreateSubscriptionOptions(TopicName, SimpleSubscriptionName), new CreateRuleOptions { Proper noun = "RequiresSessionRule", Filter = new SqlRuleFilter("RequiresSession IS Aught OR RequiresSession = false") }); } if (!await serviceBusAdministrationClient.SubscriptionExistsAsync(TopicName, SessionEnabledSubscriptionName)) { look serviceBusAdministrationClient.CreateSubscriptionAsync( new CreateSubscriptionOptions(TopicName, SessionEnabledSubscriptionName) { RequiresSession = true }, new CreateRuleOptions { Name = "RequiresSessionRule", Filter = new SqlRuleFilter("RequiresSession = truthful") }); }
The important part is the CreateRuleOptions parameter. Hither for the subscriptions, I have added Filters, which is of typeSql Filter. At that place are another two types of filters which areBoolean filters andCorrelation Filters (nosotros aren't going to use them in this mail). Now you lot might wonder where thisRequiresSession property is. Ane of the nearly important things to note with Topic Filters is "Filters can evaluate only bulletin backdrop. Filters can't evaluate the bulletin body". So nosotros need to addRequiresSession property to the ServiceBusMessage that we are publishing (not to the body).
List<ServiceBusMessage> CreateMessages() { render new() { CreateMessage(new("Message one", false)), CreateMessage(new("Message 2", fake)), CreateMessage(new("Message three")), CreateMessage(new("Message 4")), CreateMessage(new("Session Required Message one-ane", truthful, "Session1")), CreateMessage(new("Session Required Bulletin one-2", true, "Session1")), }; } ServiceBusMessage CreateMessage(MyMessage bulletin) { ServiceBusMessage serviceBusMessage = new() { Subject = message.ToString(), SessionId = message.SessionId }; serviceBusMessage.ApplicationProperties.Add("Subject area", message.Subject area); if (bulletin.RequiresSession.HasValue) { serviceBusMessage.ApplicationProperties.Add("RequiresSession", message.RequiresSession); } render serviceBusMessage; } record MyMessage(string Discipline, bool? RequiresSession = zip, string SessionId = "");
So here, I take created some test messages and note I have addedRequiresSessionproperty into ServiceBusMessages' ApplicationProperties. And at present, let's publish these messages out.
List<ServiceBusMessage> messages = CreateMessages(); ServiceBusSender serviceBusSender = serviceBusClient.CreateSender(TopicName); await serviceBusSender.SendMessagesAsync(messages);
And I can see my letters got routed to the correct subscription.
Nosotros tin can create/edit filters for subscriptions using Azure Portal also.
And if you are using a tool similar Service Autobus Explorer, yous can fifty-fifty query the messages using a Filter.
Hope this helps.
Happy Coding.
Regards,
Jaliya
For more great content click here.
About the Author:
If y'all are interested in getting to know me more,
- GitHub Profile
- Microsoft MVP Contour
- MSDN Contour
Reference:
Udagedara, J. (2021). Azure Service Jitney Topic Filters. Available at: https://jaliyaudagedara.blogspot.com/2021/11/azure-service-double-decker-topic-filters.html [Accessed: 5th Jan 2021].
Yous might besides like ...
By using our website yous concord to our employ of cookies in accordance with our cookie policy.Have
How To Create Sql Filter On Service Bus Topic Subscription Using Azure Portal,
Source: https://www.sharepointeurope.com/azure-service-bus-topic-filters/
Posted by: merrillfrenjudipt.blogspot.com
0 Response to "How To Create Sql Filter On Service Bus Topic Subscription Using Azure Portal"
Post a Comment