diff --git a/sdk/servicebus/service-bus/README.md b/sdk/servicebus/service-bus/README.md index 3fe9a4d2baf9..a8ed84c17c23 100644 --- a/sdk/servicebus/service-bus/README.md +++ b/sdk/servicebus/service-bus/README.md @@ -92,6 +92,7 @@ The following sections provide code snippets that cover some of the common tasks - [Settle a message](#settle-a-message) - [Send messages using Sessions](#send-messages-using-sessions) - [Receive messages from Sessions](#receive-messages-from-sessions) +- [Manage resources of a service bus namespace](#manage-resources-of-a-service-bus-namespace) - [Additional samples](https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples) ### Send messages @@ -106,17 +107,17 @@ const sender = serviceBusClient.createSender("my-queue"); // sending a single message await sender.send({ - body: "my-message-body", + body: "my-message-body" }); // sending multiple messages await sender.send([ { - body: "my-message-body", + body: "my-message-body" }, { - body: "another-message-body", - }, + body: "another-message-body" + } ]); ``` @@ -156,7 +157,7 @@ const myErrorHandler = async (error) => { }; receiver.subscribe({ processMessage: myMessageHandler, - processError: myErrorHandler, + processError: myErrorHandler }); ``` @@ -192,7 +193,7 @@ your message lands in the right session. const sender = serviceBusClient.createSender("my-session-queue"); await sender.send({ body: "my-message-body", - sessionId: "my-session", + sessionId: "my-session" }); ``` @@ -217,7 +218,7 @@ There are two ways of choosing which session to open: ```javascript const receiver = await serviceBusClient.createSessionReceiver("my-session-queue", "peekLock", { - sessionId: "my-session", + sessionId: "my-session" }); ``` @@ -238,6 +239,30 @@ Once the receiver is created you can use choose between 3 ways to receive messag You can read more about how sessions work [here][docsms_messagesessions]. +### Manage resources of a service bus namespace + +`ServiceBusManagementClient` lets you manage a namespace with CRUD operations on the entities(queues, topics, and subscriptions) and on the rules of a subscription. + +- Supports authentication with a service bus connection string as well as with the AAD credentials from `@azure/identity` similar to the `ServiceBusClient`. + +```js +// Get the connection string from the portal +// OR +// use the token credential overload, provide the host name of your Service Bus instance and the AAD credentials from the @azure/identity library +const serviceBusManagementClient = new ServiceBusManagementClient(""); + +// Similarly, you can create topics and subscriptions as well. +const createQueueResponse = await serviceBusManagementClient.createQueue(queueName); +console.log("Created queue with name - ", createQueueResponse.name); + +const queueRuntimeInfo = await serviceBusManagementClient.getQueueRuntimeInfo(queueName); +console.log("Number of messages in the queue = ", queueRuntimeInfo.messageCount); + +await serviceBusManagementClient.deleteQueue(queueName); +``` + +- 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) + ## Troubleshooting ## AMQP Dependencies diff --git a/sdk/servicebus/service-bus/samples/javascript/README.md b/sdk/servicebus/service-bus/samples/javascript/README.md index 4c1c73da4b98..6cdb39a656f0 100644 --- a/sdk/servicebus/service-bus/samples/javascript/README.md +++ b/sdk/servicebus/service-bus/samples/javascript/README.md @@ -11,7 +11,7 @@ These sample programs show how to use the JavaScript client libraries for Azure | [receiveMessagesLoop.js][receivemessagesloop] | uses the receiveMessages() function to receive Service Bus messages in a loop | | [scheduledMessages.js][scheduledmessages] | uses the scheduleMessage() function to schedule messages to appear on a Service Bus Queue/Subscription at a later time | | [session.js][session] | sends/receives messages to/from session enabled queues/subscriptions in Service Bus | -| [browseMessages.js][browsemessages] | uses the peekMessages() function to browse a Service Bus | +| [browseMessages.js][browsemessages] | uses the peekMessages() function to browse a Service Bus | | [usingAadAuth.js][usingaadauth] | creates a ServiceBusClient that authenticates using AAD credentials | | [useProxy.js][useproxy] | creates a ServiceBusClient that uses an HTTP(S) proxy server to make requests | | [advanced/movingMessagesToDLQ.js][advanced-movingmessagestodlq] | moves a message explicitly to the dead-letter queue | @@ -19,6 +19,7 @@ These sample programs show how to use the JavaScript client libraries for Azure | [advanced/processMessageFromDLQ.js][advanced-processmessagefromdlq] | retrieves a message from a dead-letter queue, edits it, and sends it back to the main queue | | [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 | | [advanced/sessionState.js][advanced-sessionstate] | uses a "shopping cart" example to demonstrate how SessionState information can be read and maintained in an application | +| [advanced/managementClient.js][advanced-management-client] | demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace | ## Prerequisites @@ -70,6 +71,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP [advanced-sessionstate]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/advanced/sessionState.js [sendmessages]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/sendMessages.js [serviceprincipallogin]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/servicePrincipalLogin.js +[advanced-management-client]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/javascript/advanced/managementClient.js [apiref]: https://docs.microsoft.com/javascript/api/@azure/service-bus [azsvcbus]: https://docs.microsoft.com/azure/service-bus-messaging/service-bus-create-namespace-portal [freesub]: https://azure.microsoft.com/free/ diff --git a/sdk/servicebus/service-bus/samples/javascript/advanced/managementClient.js b/sdk/servicebus/service-bus/samples/javascript/advanced/managementClient.js new file mode 100644 index 000000000000..8b1aaa771e05 --- /dev/null +++ b/sdk/servicebus/service-bus/samples/javascript/advanced/managementClient.js @@ -0,0 +1,56 @@ +/* + Copyright (c) Microsoft Corporation. All rights reserved. + Licensed under the MIT Licence. + + **NOTE**: This sample uses the preview of the next version of the @azure/service-bus package. + For samples using the current stable version of the package, please use the link below: + https://github.com/Azure/azure-sdk-for-js/tree/%40azure/service-bus_1.1.5/sdk/servicebus/service-bus/samples + + This sample demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace. + + See https://docs.microsoft.com/en-us/rest/api/servicebus/resource-provider-apis to learn more. +*/ + +const { ServiceBusManagementClient } = require("@azure/service-bus"); + +// Load the .env file if it exists +require("dotenv").config(); + +// Define connection string and related Service Bus entity names here +const connectionString = process.env.SERVICE_BUS_CONNECTION_STRING || ""; +const queueName = process.env.QUEUE_NAME || ""; + +export async function main() { + // You can also use AAD credentials from `@azure/identity` along with the host url + // instead of the connection string for authentication. + const serviceBusManagementClient = new ServiceBusManagementClient(connectionString); + + // Similarly, you can create topics and subscriptions as well. + const createQueueResponse = await serviceBusManagementClient.createQueue(queueName); + console.log("Created queue with name - ", createQueueResponse.name); + + const getQueueResponse = await serviceBusManagementClient.getQueue(queueName); + console.log("(Current)max delivery count = ", getQueueResponse.maxDeliveryCount); + + getQueueResponse.maxDeliveryCount = 12; + const updateQueueResponse = await serviceBusManagementClient.updateQueue(getQueueResponse); + console.log("(Updated)max delivery count = ", updateQueueResponse.maxDeliveryCount); + + const queueRuntimeInfo = await serviceBusManagementClient.getQueueRuntimeInfo(queueName); + console.log("Number of messages in the queue = ", queueRuntimeInfo.messageCount); + + const namespaceInfo = await serviceBusManagementClient.getNamespaceProperties(); + console.log("Type of the namespace - ", namespaceInfo.namespaceType); + + await serviceBusManagementClient.deleteQueue(queueName); + const queueExists = await serviceBusManagementClient.queueExists(queueName); + if (queueExists == true) { + console.log("Something went wrong, queue should have been deleted"); + return; + } + console.log(`Queue ${queueName} has been deleted`); +} + +main().catch((err) => { + console.log("Error occurred: ", err); +}); diff --git a/sdk/servicebus/service-bus/samples/typescript/README.md b/sdk/servicebus/service-bus/samples/typescript/README.md index aba5ca0ce9f8..d6882a711996 100644 --- a/sdk/servicebus/service-bus/samples/typescript/README.md +++ b/sdk/servicebus/service-bus/samples/typescript/README.md @@ -11,7 +11,7 @@ These sample programs show how to use the TypeScript client libraries for Azure | [receiveMessagesLoop.ts][receivemessagesloop] | uses the receiveMessages() function to receive Service Bus messages in a loop | | [scheduledMessages.ts][scheduledmessages] | uses the scheduleMessage() function to schedule messages to appear on a Service Bus Queue/Subscription at a later time | | [session.ts][session] | sends/receives messages to/from session enabled queues/subscriptions in Service Bus | -| [browseMessages.ts][browsemessages] | uses the peekMessages() function to browse a Service Bus | +| [browseMessages.ts][browsemessages] | uses the peekMessages() function to browse a Service Bus | | [usingAadAuth.ts][usingaadauth] | creates a ServiceBusClient that authenticates using AAD credentials | | [useProxy.ts][useproxy] | creates a ServiceBusClient that uses an HTTP(S) proxy server to make requests | | [advanced/movingMessagesToDLQ.ts][advanced-movingmessagestodlq] | moves a message explicitly to the dead-letter queue | @@ -19,6 +19,7 @@ These sample programs show how to use the TypeScript client libraries for Azure | [advanced/processMessageFromDLQ.ts][advanced-processmessagefromdlq] | retrieves a message from a dead-letter queue, edits it, and sends it back to the main queue | | [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 | | [advanced/sessionState.ts][advanced-sessionstate] | uses a "shopping cart" example to demonstrate how SessionState information can be read and maintained in an application | +| [advanced/managementClient.ts][advanced-management-client] | demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace | ## Prerequisites @@ -83,6 +84,7 @@ Take a look at our [API Documentation][apiref] for more information about the AP [advanced-session-round-robin]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/advanced/sessionRoundRobin.ts [sendmessages]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/sendMessages.ts [serviceprincipallogin]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/servicePrincipalLogin.ts +[advanced-management-client]: https://github.com/Azure/azure-sdk-for-js/tree/master/sdk/servicebus/service-bus/samples/typescript/src/advanced/managementClient.ts [apiref]: https://docs.microsoft.com/javascript/api/@azure/service-bus [azsvcbus]: https://docs.microsoft.com/azure/service-bus-messaging/service-bus-create-namespace-portal [freesub]: https://azure.microsoft.com/free/ diff --git a/sdk/servicebus/service-bus/samples/typescript/src/advanced/managementClient.ts b/sdk/servicebus/service-bus/samples/typescript/src/advanced/managementClient.ts new file mode 100644 index 000000000000..cbfd52247a75 --- /dev/null +++ b/sdk/servicebus/service-bus/samples/typescript/src/advanced/managementClient.ts @@ -0,0 +1,57 @@ +/* + Copyright (c) Microsoft Corporation. All rights reserved. + Licensed under the MIT Licence. + + **NOTE**: This sample uses the preview of the next version of the @azure/service-bus package. + For samples using the current stable version of the package, please use the link below: + https://github.com/Azure/azure-sdk-for-js/tree/%40azure/service-bus_1.1.5/sdk/servicebus/service-bus/samples + + This sample demonstrates how the ServiceBusManagementClient can be used to manage the resources of a service bus namespace. + + See https://docs.microsoft.com/en-us/rest/api/servicebus/resource-provider-apis to learn more. +*/ + +import { ServiceBusManagementClient } from "@azure/service-bus"; + +// Load the .env file if it exists +import * as dotenv from "dotenv"; +dotenv.config(); + +// Define connection string and related Service Bus entity names here +const connectionString = process.env.SERVICE_BUS_CONNECTION_STRING || ""; +const queueName = process.env.QUEUE_NAME || ""; + +export async function main() { + // You can also use AAD credentials from `@azure/identity` along with the host url + // instead of the connection string for authentication. + const serviceBusManagementClient = new ServiceBusManagementClient(connectionString); + + // Similarly, you can create topics and subscriptions as well. + const createQueueResponse = await serviceBusManagementClient.createQueue(queueName); + console.log("Created queue with name - ", createQueueResponse.name); + + const getQueueResponse = await serviceBusManagementClient.getQueue(queueName); + console.log("(Current)max delivery count = ", getQueueResponse.maxDeliveryCount); + + getQueueResponse.maxDeliveryCount = 12; + const updateQueueResponse = await serviceBusManagementClient.updateQueue(getQueueResponse); + console.log("(Updated)max delivery count = ", updateQueueResponse.maxDeliveryCount); + + const queueRuntimeInfo = await serviceBusManagementClient.getQueueRuntimeInfo(queueName); + console.log("Number of messages in the queue = ", queueRuntimeInfo.messageCount); + + const namespaceInfo = await serviceBusManagementClient.getNamespaceProperties(); + console.log("Type of the namespace - ", namespaceInfo.namespaceType); + + await serviceBusManagementClient.deleteQueue(queueName); + const queueExists = await serviceBusManagementClient.queueExists(queueName); + if (queueExists == true) { + console.log("Something went wrong, queue should have been deleted"); + return; + } + console.log(`Queue ${queueName} has been deleted`); +} + +main().catch((err) => { + console.log("Error occurred: ", err); +});