i want add filter for subscription so that different actions are performed on different types of event.
How do i add filters to it. Should it be added before sending to topic or it can be done in azure portal?
i want add filter for subscription so that different actions are performed on different types of event.
How do i add filters to it. Should it be added before sending to topic or it can be done in azure portal?
How do i add filters to it. Should it be added before sending to topic or it can be done in azure portal?
AFAIK, Azure Portal does not provide the feature for you to create a subscription under the specific Service Bus Topic along with the filter expression.
Per my experience, you may need to use the Service Bus client library for your development language to create your subscription with the filter expression. For C#, you may follow the code snippet below to create the subscription and retrieve the messages:
var namespaceManager = SB.NamespaceManager.CreateFromConnectionString("{connectionString}");
//create a subscription with the filter expression
if (!namespaceManager.SubscriptionExists("{your-topic-name}", "{your-subscription-name}"))
{
namespaceManager.CreateSubscription("{your-topic-name}", "{your-subscription-name}", new SqlFilter("sys.Label='important' or MessageId<0"));
}
//send topic message(s)
var msg= new BrokeredMessage("Hello World");
msg.Properties["From"] = "Bruce Chen";
msg.Label = "important";
msg.Properties["MessageId"] = 1;
var client = TopicClient.CreateFromConnectionString("{connectionString}", "{your-topic-name}");
client.Send(msg);
//subscription receives message(s)
var subClient =SubscriptionClient.CreateFromConnectionString(connectionString, "{your-topic-name}", "{your-subscription-name}");
subClient .OnMessage(m =>
{
Console.WriteLine(m.GetBody<string>() + "," + m.Label + "," + m.Properties["From"] + "," + m.Properties["MessageId"]);
});
Console.ReadLine();
Moreover, the SQLFilter syntax only applies on the public property of the BrokeredMessage class or the key of the BrokeredMessage class dictionary (e.g. BrokeredMessage.Properties).
Filters can be added in the Azure Portal for a particular subscription or it can be done added while sending from the coding side also.
Here I am sharing the method where we can add a filter from Azure Portal
Example: In the below code I am sending a message to a topic named "apptopic"
for(int i=0;i<5;i++)
{
Order obj = new Order();
var _message = new Message(Encoding.UTF8.GetBytes(obj.ToString()));
_message.MessageId = $"{i}";
_message.UserProperties.Add("Category", Exams[i]);
await _client.SendAsync(_message);
Console.WriteLine($"Sending Message : {obj.Id} ");
}
This topic has been subscribed by three subscriptions SubscriptionA, SubscriptionB, SubscriptionC.
So I want to add a filter to SubscriptionC to receive messages when MessageId is 1.
So go to a particular subscription which you want to want to add a filter. Then delete the default filter which is highlighted in the screenshot.
.
Then click on Add Filter, Give the filter name and Filter condition as mentioned in the screenshot and save the changes.
.
In this Process Subscription,
Should it be added before sending to topic or it can be done in azure portal?
You need to have a filter. Whenever you create a topic using .NET (full framework example by Bruce Chen or using the new client, 3.1.0-preview and higher) or streight REST API, a topic will contain a default filter. Default filter is a catch all filter. You will need to replace it with more specific filters if you don't want to handle any message or leave as it.
Azure portal provides the facility to create filters via Azure Resource Manager template. It involves writing custom code.
Navigate in Azure portal to Namespace -> Topic -> Automation Script
Click Deploy and edit the template
"resources": [{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusNamespaceName')]",
"type": "Microsoft.ServiceBus/Namespaces",
"location": "[variables('location')]",
"sku": {
"name": "Standard",
},
"resources": [{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusTopicName')]",
"type": "Topics",
"dependsOn": [
"[concat('Microsoft.ServiceBus/namespaces/', parameters('serviceBusNamespaceName'))]"
],
"properties": {
"path": "[parameters('serviceBusTopicName')]"
},
"resources": [{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusSubscriptionName')]",
"type": "Subscriptions",
"dependsOn": [
"[parameters('serviceBusTopicName')]"
],
"properties": {},
"resources": [{
"apiVersion": "[variables('sbVersion')]",
"name": "[parameters('serviceBusRuleName')]",
"type": "Rules",
"dependsOn": [
"[parameters('serviceBusSubscriptionName')]"
],
"properties": {
"filterType": "SqlFilter",
"sqlFilter": {
"sqlExpression": "StoreName = 'Store1'",
"requiresPreprocessing": "false"
},
"action": {
"sqlExpression": "set FilterTag = 'true'"
}
}
}]
}]
}]
}]
Now deploy the resource.
Using this, Topic Subscription with rules can be created.
Alternatively you can go with tools like ServiceBus Explorer or Serverless360, which has user interface to create topic subscription rules.
I prefer using the CLI to script things; I find it to be extremely easy. So, after I create the subscriptions I'll then add the Rules, which are filters. Note that you can't update the $Default rule. The act of creating your own rule will delete the default. You can then update your rule.
az servicebus topic subscription rule create --resource-group myresourcegroup --namespace-name mynamespace --topic-name mytopic --subscription-name mysubscription --name myrule --filter-sql-expression myproperty=myvalue