Skip to content

Commit dc07558

Browse files
committed
Service Bus Filters - Review & update
1 parent f4810a4 commit dc07558

File tree

2 files changed

+87
-66
lines changed

2 files changed

+87
-66
lines changed

articles/service-bus-messaging/service-bus-filter-examples.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Set subscriptions filters in Azure Service Bus | Microsoft Docs
33
description: This article provides examples for defining filters and actions on Azure Service Bus topic subscriptions.
44
ms.topic: how-to
5-
ms.date: 03/25/2022
5+
ms.date: 02/28/2023
66
ms.devlang: csharp
77
---
88

articles/service-bus-messaging/topic-filters.md

Lines changed: 86 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: Azure Service Bus topic filters | Microsoft Docs
33
description: This article explains how subscribers can define which messages they want to receive from a topic by specifying filters.
44
ms.topic: conceptual
5-
ms.date: 10/05/2022
5+
ms.date: 02/28/2023
66
---
77

88
# Topic filters and actions
@@ -24,69 +24,92 @@ In this example, if you send one message that matches all five rules, you get th
2424
Each newly created topic subscription has an initial default subscription rule. If you don't explicitly specify a filter condition for the rule, the applied filter is the **true** filter that enables all messages to be selected into the subscription. The default rule has no associated annotation action.
2525

2626
## Filters
27-
Service Bus supports three filter conditions:
28-
29-
- *SQL Filters* - A **SqlFilter** holds a SQL-like conditional expression that is evaluated in the broker against the arriving messages' user-defined properties and system properties. All system properties must be prefixed with `sys.` in the conditional expression. The [SQL-language subset for filter conditions](service-bus-messaging-sql-filter.md) tests for the existence of properties (`EXISTS`), null-values (`IS NULL`), logical NOT/AND/OR, relational operators, simple numeric arithmetic, and simple text pattern matching with `LIKE`.
30-
31-
**.NET example for defining a SQL filter:**
32-
```csharp
33-
adminClient = new ServiceBusAdministrationClient(connectionString);
34-
35-
// Create a SQL filter with color set to blue and quantity to 10
36-
await adminClient.CreateSubscriptionAsync(
37-
new CreateSubscriptionOptions(topicName, "ColorBlueSize10Orders"),
38-
new CreateRuleOptions("BlueSize10Orders", new SqlRuleFilter("color='blue' AND quantity=10")));
39-
40-
// Create a SQL filter with color set to red
41-
// Action is defined to set the quantity to half if the color is red
42-
await adminClient.CreateRuleAsync(topicName, "ColorRed", new CreateRuleOptions
43-
{
44-
Name = "RedOrdersWithAction",
45-
Filter = new SqlRuleFilter("user.color='red'"),
46-
Action = new SqlRuleAction("SET quantity = quantity / 2;")
47-
}
48-
```
49-
- *Boolean filters* - The **TrueFilter** and **FalseFilter** either cause all arriving messages (**true**) or none of the arriving messages (**false**) to be selected for the subscription. These two filters derive from the SQL filter.
50-
51-
**.NET example for defining a boolean filter:**
52-
```csharp
53-
// Create a True Rule filter with an expression that always evaluates to true
54-
// It's equivalent to using SQL rule filter with 1=1 as the expression
55-
await adminClient.CreateSubscriptionAsync(
56-
new CreateSubscriptionOptions(topicName, subscriptionAllOrders),
57-
new CreateRuleOptions("AllOrders", new TrueRuleFilter()));
58-
```
59-
- *Correlation Filters* - A **CorrelationFilter** holds a set of conditions that are matched against one or more of an arriving message's user and system properties. A common use is to match against the **CorrelationId** property, but the application can also choose to match against the following properties:
60-
61-
- **ContentType**
62-
- **Label**
63-
- **MessageId**
64-
- **ReplyTo**
65-
- **ReplyToSessionId**
66-
- **SessionId**
67-
- **To**
68-
- any user-defined properties.
27+
Service Bus supports three types of filters:
28+
29+
- SQL filters
30+
- Boolean filters
31+
- Correlation filters
32+
33+
The following sections provide details about these filters.
34+
35+
### SQL filters
36+
A **SqlFilter** holds a SQL-like conditional expression that's evaluated in the broker against the arriving messages' user-defined properties and system properties. All system properties must be prefixed with `sys.` in the conditional expression. The [SQL-language subset for filter conditions](service-bus-messaging-sql-filter.md) tests for the existence of properties (`EXISTS`), null-values (`IS NULL`), logical `NOT`/`AND`/`OR`, relational operators, simple numeric arithmetic, and simple text pattern matching with `LIKE`.
37+
38+
Here's a .NET example for defining a SQL filter:
39+
40+
```csharp
41+
adminClient = new ServiceBusAdministrationClient(connectionString);
42+
43+
// Create a SQL filter with color set to blue and quantity to 10
44+
await adminClient.CreateSubscriptionAsync(
45+
new CreateSubscriptionOptions(topicName, "ColorBlueSize10Orders"),
46+
new CreateRuleOptions("BlueSize10Orders", new SqlRuleFilter("color='blue' AND quantity=10")));
47+
48+
// Create a SQL filter with color set to red
49+
// Action is defined to set the quantity to half if the color is red
50+
await adminClient.CreateRuleAsync(topicName, "ColorRed", new CreateRuleOptions
51+
{
52+
Name = "RedOrdersWithAction",
53+
Filter = new SqlRuleFilter("user.color='red'"),
54+
Action = new SqlRuleAction("SET quantity = quantity / 2;")
55+
}
56+
```
57+
### Boolean filters
58+
The **TrueFilter** and **FalseFilter** either cause all arriving messages (**true**) or none of the arriving messages (**false**) to be selected for the subscription. These two filters derive from the SQL filter.
59+
60+
Here's a .NET example for defining a boolean filter:
61+
62+
```csharp
63+
// Create a True Rule filter with an expression that always evaluates to true
64+
// It's equivalent to using SQL rule filter with 1=1 as the expression
65+
await adminClient.CreateSubscriptionAsync(
66+
new CreateSubscriptionOptions(topicName, subscriptionAllOrders),
67+
new CreateRuleOptions("AllOrders", new TrueRuleFilter()));
68+
```
69+
### Correlation filters
70+
A **CorrelationFilter** holds a set of conditions that are matched against one or more of an arriving message's user and system properties. A common use is to match against the **CorrelationId** property, but the application can also choose to match against the following properties:
71+
72+
- `ContentType`
73+
- `Label`
74+
- `MessageId`
75+
- `ReplyTo`
76+
- `ReplyToSessionId`
77+
- `SessionId`
78+
- `To`
79+
- any user-defined properties.
6980

70-
A match exists when an arriving message's value for a property is equal to the value specified in the correlation filter. For string expressions, the comparison is case-sensitive. If you specify multiple match properties, the filter combines them as a logical AND condition, meaning for the filter to match, all conditions must match.
81+
A match exists when an arriving message's value for a property is equal to the value specified in the correlation filter. For string expressions, the comparison is case-sensitive. If you specify multiple match properties, the filter combines them as a logical AND condition, meaning for the filter to match, all conditions must match.
7182

72-
**.NET example for defining a correlation filter:**
83+
Here's a .NET example for defining a correlation filter:
7384

74-
```csharp
75-
// Create a correlation filter with color set to Red and priority set to High
76-
await adminClient.CreateSubscriptionAsync(
77-
new CreateSubscriptionOptions(topicName, "HighPriorityRedOrders"),
78-
new CreateRuleOptions("HighPriorityRedOrdersRule", new CorrelationRuleFilter() {Subject = "red", CorrelationId = "high"} ));
79-
```
85+
```csharp
86+
// Create a correlation filter with color set to Red and priority set to High
87+
await adminClient.CreateSubscriptionAsync(
88+
new CreateSubscriptionOptions(topicName, "HighPriorityRedOrders"),
89+
new CreateRuleOptions("HighPriorityRedOrdersRule", new CorrelationRuleFilter() {Subject = "red", CorrelationId = "high"} ));
90+
```
8091

81-
All filters evaluate message properties. Filters can't evaluate the message body.
92+
Use the [`CorrelationRuleFilter` constructor that takes a `String` argument](/dotnet/api/azure.messaging.servicebus.administration.correlationrulefilter.-ctor#azure-messaging-servicebus-administration-correlationrulefilter-ctor(system-string)) to create a correlation filter with a correlation ID.
8293

83-
Complex filter rules require processing capacity. In particular, the use of SQL filter rules cause lower overall message throughput at the subscription, topic, and namespace level. Whenever possible, applications should choose correlation filters over SQL-like filters because they're much more efficient in processing and have less impact on throughput.
94+
When you use the [`CorrelationRuleFilter` default constructor](/dotnet/api/azure.messaging.servicebus.administration.correlationrulefilter.-ctor#azure-messaging-servicebus-administration-correlationrulefilter-ctor), you can assign system properties (`ContentType`, `Label`, `MessageId`, `ReplyTo`, `ReplyToSessionId`, `SessionId`, `To`), and user-defined properties for filtering. To specify user-defined properties for correlation filter, use the property `Properties` of type `IDictionary <string, object>`. Keys for this dictionary are the user-defined properties to look up on messages. Values associated with keys are the values to correlate on. Heres an example.
8495

85-
## Actions
96+
```csharp
97+
var filter = new CorrelationFilter();
98+
filter.Label = "abc";
99+
filter.ReplyTo = "[email protected]";
100+
filter.Properties["prop1"] = "abc";
101+
filter.Properties["prop2"] = "xyz";
102+
```
86103

87-
With SQL filter conditions, you can define an action that can annotate the message by adding, removing, or replacing properties and their values. The action [uses a SQL-like expression](service-bus-messaging-sql-rule-action.md) that loosely leans on the SQL UPDATE statement syntax. The action is done on the message after it has been matched and before the message is selected into the subscription. The changes to the message properties are private to the message copied into the subscription.
88104

89-
**.NET example:**
105+
> [!NOTE]
106+
> All filters evaluate message properties. Filters can't evaluate the message body.
107+
> Complex filter rules require processing capacity. In particular, the use of SQL filter rules cause lower overall message throughput at the subscription, topic, and namespace level. Whenever possible, applications should choose correlation filters over SQL-like filters because they're much more efficient in processing and have less impact on throughput.
108+
109+
## Actions
110+
With SQL filter conditions, you can define an action that can annotate the message by adding, removing, or replacing properties and their values. The action [uses a SQL-like expression](service-bus-messaging-sql-rule-action.md) that loosely leans on the `SQL UPDATE` statement syntax. The action is done on the message after it has been matched and before the message is selected into the subscription. The changes to the message properties are private to the message copied into the subscription.
111+
112+
Here's a .NET example that creates a SQL rule with an action to update the quantity when the color is Red.
90113

91114
```csharp
92115
adminClient = new ServiceBusAdministrationClient(connectionString);
@@ -103,18 +126,16 @@ await adminClient.CreateRuleAsync(topicName, "ColorRed", new CreateRuleOptions
103126

104127
## Usage patterns
105128

106-
The simplest usage scenario for a topic is that every subscription gets a copy of each message sent to a topic, which enables a broadcast pattern.
129+
- **Broadcast** pattern: The simplest usage scenario for a topic is that every subscription gets a copy of each message sent to a topic, which enables a broadcast pattern.
130+
- **Partitioning** pattern
107131

108-
Filters and actions enable two further groups of patterns: partitioning and routing.
109-
110-
Partitioning uses filters to distribute messages across several existing topic subscriptions in a predictable and mutually exclusive manner. The partitioning pattern is used when a system is scaled out to handle many different contexts in functionally identical compartments that each hold a subset of the overall data; for example, customer profile information. With partitioning, a publisher submits the message into a topic without requiring any knowledge of the partitioning model. The message then is moved to the correct subscription from which it can then be retrieved by the partition's message handler.
111-
112-
Routing uses filters to distribute messages across topic subscriptions in a predictable fashion, but not necessarily exclusive. In conjunction with the [auto forwarding](service-bus-auto-forwarding.md) feature, topic filters can be used to create complex routing graphs within a Service Bus namespace for message distribution within an Azure region. With Azure Functions or Azure Logic Apps acting as a bridge between Azure Service Bus namespaces, you can create complex global topologies with direct integration into line-of-business applications.
132+
Partitioning uses filters to distribute messages across several existing topic subscriptions in a predictable and **mutually exclusive** manner. The partitioning pattern is used when a system is scaled out to handle many different contexts in functionally identical compartments that each hold a subset of the overall data; for example, customer profile information. With partitioning, a publisher submits the message into a topic without requiring any knowledge of the partitioning model. The message then is moved to the correct subscription from which it can then be retrieved by the partition's message handler.
133+
- **Routing** pattern
113134

135+
Routing uses filters to distribute messages across topic subscriptions in a predictable fashion, but **not necessarily exclusive**. In conjunction with the [auto forwarding](service-bus-auto-forwarding.md) feature, topic filters can be used to create complex routing graphs within a Service Bus namespace for message distribution within an Azure region. With Azure Functions or Azure Logic Apps acting as a bridge between Azure Service Bus namespaces, you can create complex global topologies with direct integration into line-of-business applications.
136+
114137
## Examples
115-
For examples, see [Service Bus filter examples](service-bus-filter-examples.md).
116-
117-
138+
For more examples, see [Service Bus filter examples](service-bus-filter-examples.md).
118139

119140
> [!NOTE]
120141
> Because the Azure portal now supports Service Bus Explorer functionality, subscription filters can be created or edited from the portal.

0 commit comments

Comments
 (0)