Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Service Bus] Sample for SBManagementClient #9369

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 32 additions & 7 deletions sdk/servicebus/service-bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
}
]);
```

Expand Down Expand Up @@ -156,7 +157,7 @@ const myErrorHandler = async (error) => {
};
receiver.subscribe({
processMessage: myMessageHandler,
processError: myErrorHandler,
processError: myErrorHandler
});
```

Expand Down Expand Up @@ -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"
});
```

Expand All @@ -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"
});
```

Expand All @@ -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("<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 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
Expand Down
4 changes: 3 additions & 1 deletion sdk/servicebus/service-bus/samples/javascript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ 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 |
| [advanced/deferral.js][advanced-deferral] | uses the defer() function to defer a message for later processing |
| [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

Expand Down Expand Up @@ -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/typescript/src/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/
Expand Down
Original file line number Diff line number Diff line change
@@ -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 || "<connection string>";
const queueName = process.env.QUEUE_NAME || "<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);
});
4 changes: 3 additions & 1 deletion sdk/servicebus/service-bus/samples/typescript/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@ 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 |
| [advanced/deferral.ts][advanced-deferral] | uses the defer() function to defer a message for later processing |
| [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

Expand Down Expand Up @@ -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/
Expand Down
Original file line number Diff line number Diff line change
@@ -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 || "<connection string>";
const queueName = process.env.QUEUE_NAME || "<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);
});