Skip to content

Commit 0363f89

Browse files
[Service Bus] Sample for SBManagementClient (Azure#9369)
* ts sample * js sample * update samples readmes * readme updates for ATOM * Update sdk/servicebus/service-bus/samples/javascript/README.md Co-authored-by: Richard Park <51494936+richardpark-msft@users.noreply.github.com> Co-authored-by: Richard Park <51494936+richardpark-msft@users.noreply.github.com>
1 parent ac442fd commit 0363f89

File tree

5 files changed

+151
-9
lines changed

5 files changed

+151
-9
lines changed

sdk/servicebus/service-bus/README.md

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ The following sections provide code snippets that cover some of the common tasks
9292
- [Settle a message](#settle-a-message)
9393
- [Send messages using Sessions](#send-messages-using-sessions)
9494
- [Receive messages from Sessions](#receive-messages-from-sessions)
95+
- [Manage resources of a service bus namespace](#manage-resources-of-a-service-bus-namespace)
9596
- [Additional samples](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples)
9697

9798
### Send messages
@@ -106,17 +107,17 @@ const sender = serviceBusClient.createSender("my-queue");
106107

107108
// sending a single message
108109
await sender.send({
109-
body: "my-message-body",
110+
body: "my-message-body"
110111
});
111112

112113
// sending multiple messages
113114
await sender.send([
114115
{
115-
body: "my-message-body",
116+
body: "my-message-body"
116117
},
117118
{
118-
body: "another-message-body",
119-
},
119+
body: "another-message-body"
120+
}
120121
]);
121122
```
122123

@@ -156,7 +157,7 @@ const myErrorHandler = async (error) => {
156157
};
157158
receiver.subscribe({
158159
processMessage: myMessageHandler,
159-
processError: myErrorHandler,
160+
processError: myErrorHandler
160161
});
161162
```
162163

@@ -192,7 +193,7 @@ your message lands in the right session.
192193
const sender = serviceBusClient.createSender("my-session-queue");
193194
await sender.send({
194195
body: "my-message-body",
195-
sessionId: "my-session",
196+
sessionId: "my-session"
196197
});
197198
```
198199

@@ -217,7 +218,7 @@ There are two ways of choosing which session to open:
217218

218219
```javascript
219220
const receiver = await serviceBusClient.createSessionReceiver("my-session-queue", "peekLock", {
220-
sessionId: "my-session",
221+
sessionId: "my-session"
221222
});
222223
```
223224

@@ -238,6 +239,30 @@ Once the receiver is created you can use choose between 3 ways to receive messag
238239

239240
You can read more about how sessions work [here][docsms_messagesessions].
240241

242+
### Manage resources of a service bus namespace
243+
244+
`ServiceBusManagementClient` lets you manage a namespace with CRUD operations on the entities(queues, topics, and subscriptions) and on the rules of a subscription.
245+
246+
- Supports authentication with a service bus connection string as well as with the AAD credentials from `@azure/identity` similar to the `ServiceBusClient`.
247+
248+
```js
249+
// Get the connection string from the portal
250+
// OR
251+
// use the token credential overload, provide the host name of your Service Bus instance and the AAD credentials from the @azure/identity library
252+
const serviceBusManagementClient = new ServiceBusManagementClient("<connectionString>");
253+
254+
// Similarly, you can create topics and subscriptions as well.
255+
const createQueueResponse = await serviceBusManagementClient.createQueue(queueName);
256+
console.log("Created queue with name - ", createQueueResponse.name);
257+
258+
const queueRuntimeInfo = await serviceBusManagementClient.getQueueRuntimeInfo(queueName);
259+
console.log("Number of messages in the queue = ", queueRuntimeInfo.messageCount);
260+
261+
await serviceBusManagementClient.deleteQueue(queueName);
262+
```
263+
264+
- Sample for reference - [managementClient.ts](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/advanced/managementClient.ts)
265+
241266
## Troubleshooting
242267

243268
## AMQP Dependencies

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ These sample programs show how to use the JavaScript client libraries for Azure
1111
| [receiveMessagesLoop.js][receivemessagesloop] | uses the receiveMessages() function to receive Service Bus messages in a loop |
1212
| [scheduledMessages.js][scheduledmessages] | uses the scheduleMessage() function to schedule messages to appear on a Service Bus Queue/Subscription at a later time |
1313
| [session.js][session] | sends/receives messages to/from session enabled queues/subscriptions in Service Bus |
14-
| [browseMessages.js][browsemessages] | uses the peekMessages() function to browse a Service Bus |
14+
| [browseMessages.js][browsemessages] | uses the peekMessages() function to browse a Service Bus |
1515
| [usingAadAuth.js][usingaadauth] | creates a ServiceBusClient that authenticates using AAD credentials |
1616
| [useProxy.js][useproxy] | creates a ServiceBusClient that uses an HTTP(S) proxy server to make requests |
1717
| [advanced/movingMessagesToDLQ.js][advanced-movingmessagestodlq] | moves a message explicitly to the dead-letter queue |
1818
| [advanced/deferral.js][advanced-deferral] | uses the defer() function to defer a message for later processing |
1919
| [advanced/processMessageFromDLQ.js][advanced-processmessagefromdlq] | retrieves a message from a dead-letter queue, edits it, and sends it back to the main queue |
2020
| [advanced/sessionRoundRobin.js][advanced-session-round-robin] | uses `SessionReceiver`'s ability to get the next available session to round-robin through all sessions in a Queue/Subscription |
2121
| [advanced/sessionState.js][advanced-sessionstate] | uses a "shopping cart" example to demonstrate how SessionState information can be read and maintained in an application |
22+
| [advanced/managementClient.js][advanced-management-client] | demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace |
2223

2324
## Prerequisites
2425

@@ -70,6 +71,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
7071
[advanced-sessionstate]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/advanced/sessionState.js
7172
[sendmessages]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/sendMessages.js
7273
[serviceprincipallogin]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/servicePrincipalLogin.js
74+
[advanced-management-client]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/advanced/managementClient.js
7375
[apiref]: https://docs.microsoft.com/javascript/api/@azure/service-bus
7476
[azsvcbus]: https://docs.microsoft.com/azure/service-bus-messaging/service-bus-create-namespace-portal
7577
[freesub]: https://azure.microsoft.com/free/
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/*
2+
Copyright (c) Microsoft Corporation. All rights reserved.
3+
Licensed under the MIT Licence.
4+
5+
**NOTE**: This sample uses the preview of the next version of the @azure/service-bus package.
6+
For samples using the current stable version of the package, please use the link below:
7+
https://github.com/Azure/azure-sdk-for-js/tree/%40azure/service-bus_1.1.5/sdk/servicebus/service-bus/samples
8+
9+
This sample demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace.
10+
11+
See https://docs.microsoft.com/en-us/rest/api/servicebus/resource-provider-apis to learn more.
12+
*/
13+
14+
const { ServiceBusManagementClient } = require("@azure/service-bus");
15+
16+
// Load the .env file if it exists
17+
require("dotenv").config();
18+
19+
// Define connection string and related Service Bus entity names here
20+
const connectionString = process.env.SERVICE_BUS_CONNECTION_STRING || "<connection string>";
21+
const queueName = process.env.QUEUE_NAME || "<queue name>";
22+
23+
export async function main() {
24+
// You can also use AAD credentials from `@azure/identity` along with the host url
25+
// instead of the connection string for authentication.
26+
const serviceBusManagementClient = new ServiceBusManagementClient(connectionString);
27+
28+
// Similarly, you can create topics and subscriptions as well.
29+
const createQueueResponse = await serviceBusManagementClient.createQueue(queueName);
30+
console.log("Created queue with name - ", createQueueResponse.name);
31+
32+
const getQueueResponse = await serviceBusManagementClient.getQueue(queueName);
33+
console.log("(Current)max delivery count = ", getQueueResponse.maxDeliveryCount);
34+
35+
getQueueResponse.maxDeliveryCount = 12;
36+
const updateQueueResponse = await serviceBusManagementClient.updateQueue(getQueueResponse);
37+
console.log("(Updated)max delivery count = ", updateQueueResponse.maxDeliveryCount);
38+
39+
const queueRuntimeInfo = await serviceBusManagementClient.getQueueRuntimeInfo(queueName);
40+
console.log("Number of messages in the queue = ", queueRuntimeInfo.messageCount);
41+
42+
const namespaceInfo = await serviceBusManagementClient.getNamespaceProperties();
43+
console.log("Type of the namespace - ", namespaceInfo.namespaceType);
44+
45+
await serviceBusManagementClient.deleteQueue(queueName);
46+
const queueExists = await serviceBusManagementClient.queueExists(queueName);
47+
if (queueExists == true) {
48+
console.log("Something went wrong, queue should have been deleted");
49+
return;
50+
}
51+
console.log(`Queue ${queueName} has been deleted`);
52+
}
53+
54+
main().catch((err) => {
55+
console.log("Error occurred: ", err);
56+
});

sdk/servicebus/service-bus/samples/typescript/README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,15 @@ These sample programs show how to use the TypeScript client libraries for Azure
1111
| [receiveMessagesLoop.ts][receivemessagesloop] | uses the receiveMessages() function to receive Service Bus messages in a loop |
1212
| [scheduledMessages.ts][scheduledmessages] | uses the scheduleMessage() function to schedule messages to appear on a Service Bus Queue/Subscription at a later time |
1313
| [session.ts][session] | sends/receives messages to/from session enabled queues/subscriptions in Service Bus |
14-
| [browseMessages.ts][browsemessages] | uses the peekMessages() function to browse a Service Bus |
14+
| [browseMessages.ts][browsemessages] | uses the peekMessages() function to browse a Service Bus |
1515
| [usingAadAuth.ts][usingaadauth] | creates a ServiceBusClient that authenticates using AAD credentials |
1616
| [useProxy.ts][useproxy] | creates a ServiceBusClient that uses an HTTP(S) proxy server to make requests |
1717
| [advanced/movingMessagesToDLQ.ts][advanced-movingmessagestodlq] | moves a message explicitly to the dead-letter queue |
1818
| [advanced/deferral.ts][advanced-deferral] | uses the defer() function to defer a message for later processing |
1919
| [advanced/processMessageFromDLQ.ts][advanced-processmessagefromdlq] | retrieves a message from a dead-letter queue, edits it, and sends it back to the main queue |
2020
| [advanced/sessionRoundRobin.ts][advanced-session-round-robin] | uses `SessionReceiver`'s ability to get the next available session to round-robin through all sessions in a Queue/Subscription |
2121
| [advanced/sessionState.ts][advanced-sessionstate] | uses a "shopping cart" example to demonstrate how SessionState information can be read and maintained in an application |
22+
| [advanced/managementClient.ts][advanced-management-client] | demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace |
2223

2324
## Prerequisites
2425

@@ -83,6 +84,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP
8384
[advanced-session-round-robin]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/advanced/sessionRoundRobin.ts
8485
[sendmessages]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/sendMessages.ts
8586
[serviceprincipallogin]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/servicePrincipalLogin.ts
87+
[advanced-management-client]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/advanced/managementClient.ts
8688
[apiref]: https://docs.microsoft.com/javascript/api/@azure/service-bus
8789
[azsvcbus]: https://docs.microsoft.com/azure/service-bus-messaging/service-bus-create-namespace-portal
8890
[freesub]: https://azure.microsoft.com/free/
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
Copyright (c) Microsoft Corporation. All rights reserved.
3+
Licensed under the MIT Licence.
4+
5+
**NOTE**: This sample uses the preview of the next version of the @azure/service-bus package.
6+
For samples using the current stable version of the package, please use the link below:
7+
https://github.com/Azure/azure-sdk-for-js/tree/%40azure/service-bus_1.1.5/sdk/servicebus/service-bus/samples
8+
9+
This sample demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace.
10+
11+
See https://docs.microsoft.com/en-us/rest/api/servicebus/resource-provider-apis to learn more.
12+
*/
13+
14+
import { ServiceBusManagementClient } from "@azure/service-bus";
15+
16+
// Load the .env file if it exists
17+
import * as dotenv from "dotenv";
18+
dotenv.config();
19+
20+
// Define connection string and related Service Bus entity names here
21+
const connectionString = process.env.SERVICE_BUS_CONNECTION_STRING || "<connection string>";
22+
const queueName = process.env.QUEUE_NAME || "<queue name>";
23+
24+
export async function main() {
25+
// You can also use AAD credentials from `@azure/identity` along with the host url
26+
// instead of the connection string for authentication.
27+
const serviceBusManagementClient = new ServiceBusManagementClient(connectionString);
28+
29+
// Similarly, you can create topics and subscriptions as well.
30+
const createQueueResponse = await serviceBusManagementClient.createQueue(queueName);
31+
console.log("Created queue with name - ", createQueueResponse.name);
32+
33+
const getQueueResponse = await serviceBusManagementClient.getQueue(queueName);
34+
console.log("(Current)max delivery count = ", getQueueResponse.maxDeliveryCount);
35+
36+
getQueueResponse.maxDeliveryCount = 12;
37+
const updateQueueResponse = await serviceBusManagementClient.updateQueue(getQueueResponse);
38+
console.log("(Updated)max delivery count = ", updateQueueResponse.maxDeliveryCount);
39+
40+
const queueRuntimeInfo = await serviceBusManagementClient.getQueueRuntimeInfo(queueName);
41+
console.log("Number of messages in the queue = ", queueRuntimeInfo.messageCount);
42+
43+
const namespaceInfo = await serviceBusManagementClient.getNamespaceProperties();
44+
console.log("Type of the namespace - ", namespaceInfo.namespaceType);
45+
46+
await serviceBusManagementClient.deleteQueue(queueName);
47+
const queueExists = await serviceBusManagementClient.queueExists(queueName);
48+
if (queueExists == true) {
49+
console.log("Something went wrong, queue should have been deleted");
50+
return;
51+
}
52+
console.log(`Queue ${queueName} has been deleted`);
53+
}
54+
55+
main().catch((err) => {
56+
console.log("Error occurred: ", err);
57+
});

0 commit comments

Comments
 (0)