Skip to content

Commit 098abbd

Browse files
authored
[Service Bus] topic filter sample in .ts (Azure#21812)
* set up sample for filters * modeling topicSubscription sample similar to .cs versions * creating topics, subscriptions, and deleting rules * created subscriptions with name, filter, and action (optional) * should have sample done, but cannot run on basic tier * added comments * format * revision * formatting * revised expected output * revised naming and comments * added clarifying comments * delete samples * update samples/ * restore samples/v7 * checkout
1 parent 1010e5f commit 098abbd

File tree

5 files changed

+371
-34
lines changed

5 files changed

+371
-34
lines changed
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample demonstrates the topic filter functionality in Service Bus.
6+
* It follows similar functionality to TopicSubscriptionWithRuleOperationsSample/program.cs,
7+
* found here: https://github.com/Azure/azure-service-bus/blob/master/samples/DotNet/GettingStarted/Microsoft.Azure.ServiceBus/TopicSubscriptionWithRuleOperationsSample/Program.cs
8+
*
9+
* @summary Demonstrates how to filter messages in Service Bus
10+
* @azsdk-weight 100
11+
*/
12+
13+
import {
14+
ServiceBusClient,
15+
ServiceBusAdministrationClient,
16+
ServiceBusMessage,
17+
} from "@azure/service-bus";
18+
19+
// Load the .env file if it exists
20+
import * as dotenv from "dotenv";
21+
dotenv.config();
22+
23+
// Define connection string and related Service Bus entity names here
24+
const connectionString = process.env.SERVICEBUS_CONNECTION_STRING || "<connection string>";
25+
const topicName = "TopicSubscriptionWithRuleOperationsSample" + new Date().getTime();
26+
27+
// Default rule name
28+
const DEFAULT_RULE_NAME = "$Default";
29+
30+
// The messages we want to send
31+
const firstSetOfMessages: ServiceBusMessage[] = [
32+
{ subject: "Red", body: "test-red1" },
33+
{ subject: "Red", body: "test-red2", correlationId: "notimportant" },
34+
{ subject: "Red", body: "test-red3", correlationId: "important" },
35+
{ subject: "Blue", body: "test-blue1" },
36+
{ subject: "Blue", body: "test-blue2", correlationId: "notimportant" },
37+
{ subject: "Blue", body: "test-blue3", correlationId: "important" },
38+
{ subject: "Green", body: "test-green1" },
39+
{ subject: "Green", body: "test-green2", correlationId: "notimportant" },
40+
{ subject: "Green", body: "test-green3", correlationId: "important" },
41+
];
42+
43+
// The subscription names for our topics
44+
const NoFilterSubscriptionName = "NoFilterSubscription";
45+
const SqlFilterOnlySubscriptionName = "RedSqlFilterSubscription";
46+
const SqlFilterWithActionSubscriptionName = "BlueSqlFilterWithActionSubscription";
47+
const CorrelationFilterSubscriptionName = "ImportantCorrelationFilterSubscription";
48+
49+
export async function main() {
50+
const sbClient = new ServiceBusClient(connectionString);
51+
const sbAdminClient = new ServiceBusAdministrationClient(connectionString);
52+
53+
// Create the topic
54+
await sbAdminClient.createTopic(topicName);
55+
56+
// Create the first subscription, which will not filter anything
57+
await sbAdminClient.createSubscription(topicName, NoFilterSubscriptionName);
58+
await sbAdminClient.deleteRule(topicName, NoFilterSubscriptionName, DEFAULT_RULE_NAME);
59+
await sbAdminClient.createRule(topicName, NoFilterSubscriptionName, DEFAULT_RULE_NAME, {
60+
sqlExpression: "1=1",
61+
});
62+
63+
// Create the next subscription, which will filter out non-red colors
64+
await sbAdminClient.createSubscription(topicName, SqlFilterOnlySubscriptionName, {
65+
defaultRuleOptions: {
66+
name: "RedSqlRule",
67+
filter: { sqlExpression: "Color = 'Red'" },
68+
},
69+
});
70+
71+
// Create another subscription, which will filter out non-blue colors and set the blue
72+
// color to BlueProcessed
73+
await sbAdminClient.createSubscription(topicName, SqlFilterWithActionSubscriptionName, {
74+
defaultRuleOptions: {
75+
name: "BlueSqlRule",
76+
filter: { sqlExpression: "Color = 'Blue'" },
77+
action: { sqlExpression: "SET Color = 'BlueProcessed'" },
78+
},
79+
});
80+
81+
// Create the last subscription, which will filter out all messages that are not
82+
// important and not red
83+
await sbAdminClient.createSubscription(topicName, CorrelationFilterSubscriptionName, {
84+
defaultRuleOptions: {
85+
name: "ImportantCorrelationRule",
86+
filter: { subject: "Red", correlationId: "important" },
87+
},
88+
});
89+
90+
// Send and receive the messages using the CorrelationFilterSubscriptionName filter
91+
await sbClient.createSender(topicName).sendMessages(firstSetOfMessages);
92+
const receivedMessages = await sbClient
93+
.createReceiver(topicName, CorrelationFilterSubscriptionName)
94+
.receiveMessages(10);
95+
96+
// Print the received messages
97+
// should be test-red3 only
98+
for (const msg of receivedMessages) {
99+
console.log(`Received message: ${msg.body}`);
100+
}
101+
102+
// Clean up the resources
103+
await sbAdminClient.deleteSubscription(topicName, NoFilterSubscriptionName);
104+
await sbAdminClient.deleteSubscription(topicName, SqlFilterOnlySubscriptionName);
105+
await sbAdminClient.deleteSubscription(topicName, SqlFilterWithActionSubscriptionName);
106+
await sbAdminClient.deleteSubscription(topicName, CorrelationFilterSubscriptionName);
107+
await sbClient.close();
108+
}
109+
110+
main().catch((err) => {
111+
console.log("sendMessages Sample: Error occurred: ", err);
112+
process.exit(1);
113+
});

sdk/servicebus/service-bus/samples/v7-beta/javascript/README.md

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,24 @@ urlFragment: service-bus-javascript-beta
1212

1313
These sample programs show how to use the JavaScript client libraries for Azure Service Bus in some common scenarios.
1414

15-
| **File Name** | **Description** |
16-
| ------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
17-
| [sendMessages.js][sendmessages] | Demonstrates how to send messages to Service Bus Queue/Topic |
18-
| [receiveMessagesLoop.js][receivemessagesloop] | Demonstrates how to receive Service Bus messages in a loop |
19-
| [receiveMessagesStreaming.js][receivemessagesstreaming] | Demonstrates how to receive Service Bus messages in a stream |
20-
| [usingAadAuth.js][usingaadauth] | This sample how to create a namespace using AAD token credentials |
21-
| [browseMessages.js][browsemessages] | Demonstrates how to browse a Service Bus message |
22-
| [session.js][session] | Demonstrates how to send/receive messages to/from session enabled queues/subscriptions in Service Bus |
23-
| [scheduledMessages.js][scheduledmessages] | Demonstrates how to schedule messages to appear on a Service Bus Queue/Subscription at a later time |
24-
| [useProxy.js][useproxy] | This sample demonstrates how to create a ServiceBusClient meant to be used in an environment where outgoing network requests have to go through a proxy server |
25-
| [advanced/administrationClient.js][advanced_administrationclient] | Demonstrates how to manage the resources of a service bus namespace. |
26-
| [advanced/sessionRoundRobin.js][advanced_sessionroundrobin] | Demonstrates how to continually read through all the available sessions |
27-
| [advanced/deferral.js][advanced_deferral] | Demonstrates how to defer a message for later processing. |
28-
| [advanced/listingEntities.js][advanced_listingentities] | Demonstrates how the ServiceBusAdministrationClient can be used to list the entities of a service bus namespace |
29-
| [advanced/sessionState.js][advanced_sessionstate] | Demonstrates usage of SessionState. |
30-
| [advanced/movingMessagesToDLQ.js][advanced_movingmessagestodlq] | Demonstrates scenarios as to how a Service Bus message can be explicitly moved to the DLQ |
31-
| [advanced/processMessageFromDLQ.js][advanced_processmessagefromdlq] | Demonstrates retrieving a message from a dead letter queue, editing it and sending it back to the main queue |
15+
| **File Name** | **Description** |
16+
| ----------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------- |
17+
| [sendMessages.js][sendmessages] | Demonstrates how to send messages to Service Bus Queue/Topic |
18+
| [topicSubscriptionWithRuleOperationsSample.js][topicsubscriptionwithruleoperationssample] | Demonstrates how to filter messages in Service Bus |
19+
| [receiveMessagesLoop.js][receivemessagesloop] | Demonstrates how to receive Service Bus messages in a loop |
20+
| [receiveMessagesStreaming.js][receivemessagesstreaming] | Demonstrates how to receive Service Bus messages in a stream |
21+
| [usingAadAuth.js][usingaadauth] | This sample how to create a namespace using AAD token credentials |
22+
| [browseMessages.js][browsemessages] | Demonstrates how to browse a Service Bus message |
23+
| [session.js][session] | Demonstrates how to send/receive messages to/from session enabled queues/subscriptions in Service Bus |
24+
| [scheduledMessages.js][scheduledmessages] | Demonstrates how to schedule messages to appear on a Service Bus Queue/Subscription at a later time |
25+
| [useProxy.js][useproxy] | This sample demonstrates how to create a ServiceBusClient meant to be used in an environment where outgoing network requests have to go through a proxy server |
26+
| [advanced/administrationClient.js][advanced_administrationclient] | Demonstrates how to manage the resources of a service bus namespace. |
27+
| [advanced/sessionRoundRobin.js][advanced_sessionroundrobin] | Demonstrates how to continually read through all the available sessions |
28+
| [advanced/deferral.js][advanced_deferral] | Demonstrates how to defer a message for later processing. |
29+
| [advanced/listingEntities.js][advanced_listingentities] | Demonstrates how the ServiceBusAdministrationClient can be used to list the entities of a service bus namespace |
30+
| [advanced/sessionState.js][advanced_sessionstate] | Demonstrates usage of SessionState. |
31+
| [advanced/movingMessagesToDLQ.js][advanced_movingmessagestodlq] | Demonstrates scenarios as to how a Service Bus message can be explicitly moved to the DLQ |
32+
| [advanced/processMessageFromDLQ.js][advanced_processmessagefromdlq] | Demonstrates retrieving a message from a dead letter queue, editing it and sending it back to the main queue |
3233
| [exceedMaxDeliveryCount.js][exceedmaxdeliverycount] | Demonstrates exceeding the max delivery count, then processing the messages sent to the dead letter queue |
3334

3435
## Prerequisites
@@ -72,6 +73,7 @@ npx cross-env SERVICEBUS_CONNECTION_STRING="<servicebus connection string>" QUEU
7273
Take a look at our [API Documentation][apiref] for more information about the APIs that are available in the clients.
7374

7475
[sendmessages]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/servicebus/service-bus/samples/v7-beta/javascript/sendMessages.js
76+
[topicsubscriptionwithruleoperationssample]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/servicebus/service-bus/samples/v7-beta/javascript/topicSubscriptionWithRuleOperationsSample.js
7577
[receivemessagesloop]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/servicebus/service-bus/samples/v7-beta/javascript/receiveMessagesLoop.js
7678
[receivemessagesstreaming]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/servicebus/service-bus/samples/v7-beta/javascript/receiveMessagesStreaming.js
7779
[usingaadauth]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/servicebus/service-bus/samples/v7-beta/javascript/usingAadAuth.js
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
// Copyright (c) Microsoft Corporation. All rights reserved.
2+
// Licensed under the MIT License.
3+
4+
/**
5+
* This sample demonstrates the topic filter functionality in Service Bus.
6+
* It follows similar functionality to TopicSubscriptionWithRuleOperationsSample/program.cs,
7+
* found here: https://github.com/Azure/azure-service-bus/blob/master/samples/DotNet/GettingStarted/Microsoft.Azure.ServiceBus/TopicSubscriptionWithRuleOperationsSample/Program.cs
8+
*
9+
* @summary Demonstrates how to filter messages in Service Bus
10+
*/
11+
12+
const { ServiceBusClient, ServiceBusAdministrationClient } = require("@azure/service-bus");
13+
14+
// Load the .env file if it exists
15+
require("dotenv").config();
16+
17+
// Define connection string and related Service Bus entity names here
18+
const connectionString = process.env.SERVICEBUS_CONNECTION_STRING || "<connection string>";
19+
const topicName = "TopicSubscriptionWithRuleOperationsSample" + new Date().getTime();
20+
21+
// Default rule name
22+
const DEFAULT_RULE_NAME = "$Default";
23+
24+
// The messages we want to send
25+
const firstSetOfMessages = [
26+
{ subject: "Red", body: "test-red1" },
27+
{ subject: "Red", body: "test-red2", correlationId: "notimportant" },
28+
{ subject: "Red", body: "test-red3", correlationId: "important" },
29+
{ subject: "Blue", body: "test-blue1" },
30+
{ subject: "Blue", body: "test-blue2", correlationId: "notimportant" },
31+
{ subject: "Blue", body: "test-blue3", correlationId: "important" },
32+
{ subject: "Green", body: "test-green1" },
33+
{ subject: "Green", body: "test-green2", correlationId: "notimportant" },
34+
{ subject: "Green", body: "test-green3", correlationId: "important" },
35+
];
36+
37+
// The subscription names for our topics
38+
const NoFilterSubscriptionName = "NoFilterSubscription";
39+
const SqlFilterOnlySubscriptionName = "RedSqlFilterSubscription";
40+
const SqlFilterWithActionSubscriptionName = "BlueSqlFilterWithActionSubscription";
41+
const CorrelationFilterSubscriptionName = "ImportantCorrelationFilterSubscription";
42+
43+
async function main() {
44+
const sbClient = new ServiceBusClient(connectionString);
45+
const sbAdminClient = new ServiceBusAdministrationClient(connectionString);
46+
47+
// Create the topic
48+
await sbAdminClient.createTopic(topicName);
49+
50+
// Create the first subscription, which will not filter anything
51+
await sbAdminClient.createSubscription(topicName, NoFilterSubscriptionName);
52+
await sbAdminClient.deleteRule(topicName, NoFilterSubscriptionName, DEFAULT_RULE_NAME);
53+
await sbAdminClient.createRule(topicName, NoFilterSubscriptionName, DEFAULT_RULE_NAME, {
54+
sqlExpression: "1=1",
55+
});
56+
57+
// Create the next subscription, which will filter out non-red colors
58+
await sbAdminClient.createSubscription(topicName, SqlFilterOnlySubscriptionName, {
59+
defaultRuleOptions: {
60+
name: "RedSqlRule",
61+
filter: { sqlExpression: "Color = 'Red'" },
62+
},
63+
});
64+
65+
// Create another subscription, which will filter out non-blue colors and set the blue
66+
// color to BlueProcessed
67+
await sbAdminClient.createSubscription(topicName, SqlFilterWithActionSubscriptionName, {
68+
defaultRuleOptions: {
69+
name: "BlueSqlRule",
70+
filter: { sqlExpression: "Color = 'Blue'" },
71+
action: { sqlExpression: "SET Color = 'BlueProcessed'" },
72+
},
73+
});
74+
75+
// Create the last subscription, which will filter out all messages that are not
76+
// important and not red
77+
await sbAdminClient.createSubscription(topicName, CorrelationFilterSubscriptionName, {
78+
defaultRuleOptions: {
79+
name: "ImportantCorrelationRule",
80+
filter: { subject: "Red", correlationId: "important" },
81+
},
82+
});
83+
84+
// Send and receive the messages using the CorrelationFilterSubscriptionName filter
85+
await sbClient.createSender(topicName).sendMessages(firstSetOfMessages);
86+
const receivedMessages = await sbClient
87+
.createReceiver(topicName, CorrelationFilterSubscriptionName)
88+
.receiveMessages(10);
89+
90+
// Print the received messages
91+
// should be test-red3 only
92+
for (const msg of receivedMessages) {
93+
console.log(`Received message: ${msg.body}`);
94+
}
95+
96+
// Clean up the resources
97+
await sbAdminClient.deleteSubscription(topicName, NoFilterSubscriptionName);
98+
await sbAdminClient.deleteSubscription(topicName, SqlFilterOnlySubscriptionName);
99+
await sbAdminClient.deleteSubscription(topicName, SqlFilterWithActionSubscriptionName);
100+
await sbAdminClient.deleteSubscription(topicName, CorrelationFilterSubscriptionName);
101+
await sbClient.close();
102+
}
103+
104+
main().catch((err) => {
105+
console.log("sendMessages Sample: Error occurred: ", err);
106+
process.exit(1);
107+
});
108+
109+
module.exports = { main };

0 commit comments

Comments
 (0)