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

[servicebus] Migrate servicebus projects to use snippets extraction #33267

Merged
merged 6 commits into from
Mar 4, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
2 changes: 2 additions & 0 deletions sdk/servicebus/service-bus/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- dev-tool snippets ignore -->

# Release History

## 7.10.0 (Unreleased)
Expand Down
93 changes: 75 additions & 18 deletions sdk/servicebus/service-bus/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,8 @@ authenticate to Service Bus using a connection string or using an Azure Active D
This method takes the connection string to your Service Bus instance. You can get
the connection string from the Azure portal.

```javascript
const { ServiceBusClient } = require("@azure/service-bus");
```ts snippet:ReadmeSampleCreateClient_ConnectionString
import { ServiceBusClient } from "@azure/service-bus";

const serviceBusClient = new ServiceBusClient("<connectionString>");
```
Expand All @@ -167,9 +167,9 @@ Authentication with Azure Active Directory uses the [Azure Identity library][azu
The example below uses the [DefaultAzureCredential][defaultazurecredential], one of many
available credential providers from the `@azure/identity` library.

```javascript
const { ServiceBusClient } = require("@azure/service-bus");
const { DefaultAzureCredential } = require("@azure/identity");
```ts snippet:ReadmeSampleCreateClient_AAD
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
Expand Down Expand Up @@ -223,7 +223,14 @@ The following sections provide code snippets that cover some of the common tasks
Once you have created an instance of a `ServiceBusClient` class, you can get a `ServiceBusSender`
using the [createSender][sbclient_createsender] method which you can use to [send][sender_sendmessages] messages.

```javascript
```ts snippet:ReadmeSampleSendMessage
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);

const sender = serviceBusClient.createSender("my-queue");

const messages = [
Expand Down Expand Up @@ -270,7 +277,14 @@ await sender.sendMessages(batch);
Once you have created an instance of a `ServiceBusClient` class, you can get a `ServiceBusReceiver`
using the [createReceiver][sbclient_createreceiver] method.

```javascript
```ts snippet:ReadmeSampleReceiveMessage
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);

const receiver = serviceBusClient.createReceiver("my-queue");
```

Expand All @@ -289,7 +303,16 @@ You can use this receiver in one of 3 ways to receive messages:
Use the [receiveMessages][receiver_receivemessages] function which returns a promise that
resolves to an array of messages.

```javascript
```ts snippet:ReadmeSampleReceiveMessage_ReceiveMessages
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);

const receiver = serviceBusClient.createReceiver("my-queue");

const myMessages = await receiver.receiveMessages(10);
```

Expand All @@ -300,7 +323,16 @@ it running as long as you need.

When you are done, call `receiver.close()` to stop receiving any more messages.

```javascript
```ts snippet:ReadmeSampleReceiveMessage_Subscribe
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);

const receiver = serviceBusClient.createReceiver("my-queue");

const myMessageHandler = async (message) => {
// your code here
console.log(`message.body: ${message.body}`);
Expand All @@ -321,7 +353,16 @@ receiver.subscribe({

Use the [getMessageIterator][receiver_getmessageiterator] to get an async iterator over messages

```javascript
```ts snippet:ReadmeSampleReceiveMessage_AsyncIterator
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);

const receiver = serviceBusClient.createReceiver("my-queue");

for await (const message of receiver.getMessageIterator()) {
// your code here
}
Expand All @@ -341,7 +382,14 @@ their maximum delivery count.

Creating a receiver for a dead letter sub-queue is similar to creating a receiver for a subscription or queue:

```javascript
```ts snippet:ReadmeSampleDeadLetterQueue
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);

// To receive from a queue's dead letter sub-queue
const deadLetterReceiverForQueue = serviceBusClient.createReceiver("queue", {
subQueueType: "deadLetter",
Expand Down Expand Up @@ -377,7 +425,14 @@ In order to send messages to a session, use the `ServiceBusClient` to create a s
When sending the message, set the `sessionId` property in the message to ensure
your message lands in the right session.

```javascript
```ts snippet:ReadmeSampleSendMessage_Session
import { DefaultAzureCredential } from "@azure/identity";
import { ServiceBusClient } from "@azure/service-bus";

const fullyQualifiedNamespace = "<name-of-service-bus-namespace>.servicebus.windows.net";
const credential = new DefaultAzureCredential();
const serviceBusClient = new ServiceBusClient(fullyQualifiedNamespace, credential);

const sender = serviceBusClient.createSender("my-session-queue");
await sender.sendMessages({
body: "my-message-body",
Expand All @@ -404,14 +459,14 @@ There are two ways of choosing which session to open:

1. Specify a `sessionId`, which locks a named session.

```javascript
```ts snippet:ignore
const receiver = await serviceBusClient.acceptSession("my-session-queue", "my-session");
```

2. Do not specify a session id. In this case Service Bus will find the next available session
that is not already locked.

```javascript
```ts snippet:ignore
const receiver = await serviceBusClient.acceptNextSession("my-session-queue");
```

Expand All @@ -435,7 +490,11 @@ You can read more about how sessions work [here][docsms_messagesessions].

Note: Service Bus doesn't support setting CORS rules for namespaces yet, hence `ServiceBusAdministrationClient` won't work in the browser without disabling web-security. For more info, refer [here](https://github.com/Azure/azure-sdk-for-js/issues/4983).

```js
```ts snippet:ReadmeSampleAdministrationClient
import { ServiceBusAdministrationClient } from "@azure/service-bus";

const queueName = "my-session-queue";

// 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
Expand All @@ -447,7 +506,7 @@ console.log("Created queue with name - ", createQueueResponse.name);

const queueRuntimeProperties =
await serviceBusAdministrationClient.getQueueRuntimeProperties(queueName);
console.log("Number of messages in the queue = ", queueRuntimeProperties.totalMessageCount);
console.log(`Number of messages in the queue = ${queueRuntimeProperties.totalMessageCount}`);

await serviceBusAdministrationClient.deleteQueue(queueName);
```
Expand Down Expand Up @@ -518,8 +577,6 @@ directory for detailed examples on how to use this library to send and receive m

If you'd like to contribute to this library, please read the [contributing guide](https://github.com/Azure/azure-sdk-for-js/blob/main/CONTRIBUTING.md) to learn more about how to build and test the code.



[apiref]: https://learn.microsoft.com/javascript/api/@azure/service-bus/
[azure_identity]: https://github.com/Azure/azure-sdk-for-js/blob/main/sdk/identity/identity/README.md
[defaultazurecredential]: https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/identity/identity#defaultazurecredential
Expand Down
4 changes: 2 additions & 2 deletions sdk/servicebus/service-bus/migrationguide.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
<!-- dev-tool snippets ignore -->

# Guide to migrate from @azure/service-bus v1 to v7

This guide is intended to assist in the migration from version 1 of the Service Bus client library `@azure/service-bus` to version 7 of the same library. It will focus on side-by-side comparisons for similar operations between the two packages.
Expand Down Expand Up @@ -261,8 +263,6 @@ Additionally, since a message cannot be settled if the receiver that was used to
- Optional [prefetch](https://learn.microsoft.com/azure/service-bus-messaging/service-bus-prefetch) support to speed up the message flow by having a message readily available for local retrieval when and before the application asks for one.
- An optional method on `ServiceBusSender` to allow pre-initializing the sender link to remove the upfront cost from the first send operation.



## Additional samples

More examples can be found at [Samples for @azure/service-bus](https://github.com/Azure/azure-sdk-for-js/tree/main/sdk/servicebus/service-bus/samples/)
33 changes: 17 additions & 16 deletions sdk/servicebus/service-bus/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,22 +38,22 @@
"check-format": "dev-tool run vendored prettier --list-different --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
"clean": "dev-tool run vendored rimraf --glob dist dist-* types *.tgz *.log coverage coverage-browser .nyc_output",
"execute:samples": "dev-tool samples run samples-dev",
"extract-api": "tsc -p . && dev-tool run extract-api",
"extract-api": "dev-tool run build-package && dev-tool run extract-api",
"format": "dev-tool run vendored prettier --write --config ../../../.prettierrc.json --ignore-path ../../../.prettierignore \"samples/**/*.{ts,js}\" \"src/**/*.ts\" \"test/**/*.ts\" \"samples-dev/**/*.ts\" \"*.{js,json}\"",
"integration-test": "npm run integration-test:node && npm run integration-test:browser",
"integration-test:browser": "dev-tool run build-package && dev-tool run build-test && dev-tool run test:vitest --browser --no-test-proxy",
"integration-test:node": "dev-tool run test:vitest --no-test-proxy --esm",
"lint": "eslint package.json api-extractor.json README.md src test",
"lint:fix": "eslint package.json api-extractor.json README.md src test --fix --fix-type [problem,suggestion]",
"pack": "npm pack 2>&1",
"perf-test:node": "tsc -p . --module \"commonjs\" && node dist-esm/test/perf/track-2/index.spec.js",
"perf-test:node": "dev-tool run build-package --module \"commonjs\" && node dist-esm/test/perf/track-2/index.spec.js",
"test": "npm run test:node && npm run test:browser",
"test:browser": "npm run clean && npm run integration-test:browser",
"test:node": "npm run clean && npm run integration-test:node",
"unit-test": "npm run unit-test:node && npm run unit-test:browser",
"unit-test:browser": "echo skipped",
"unit-test:node": "dev-tool run test:vitest --no-test-proxy -- -c vitest.unit.config.ts",
"update-snippets": "echo skipped"
"update-snippets": "dev-tool run update-snippets"
},
"sideEffects": false,
"//metadata": {
Expand Down Expand Up @@ -88,21 +88,21 @@
"dependencies": {
"@azure/abort-controller": "^2.1.2",
"@azure/core-amqp": "^4.3.2",
"@azure/core-auth": "^1.8.0",
"@azure/core-auth": "^1.9.0",
"@azure/core-client": "^1.9.2",
"@azure/core-paging": "^1.6.2",
"@azure/core-rest-pipeline": "^1.17.0",
"@azure/core-rest-pipeline": "^1.19.0",
"@azure/core-tracing": "^1.2.0",
"@azure/core-util": "^1.10.0",
"@azure/core-util": "^1.11.0",
"@azure/core-xml": "^1.4.3",
"@azure/logger": "^1.1.4",
"buffer": "^6.0.0",
"is-buffer": "^2.0.3",
"buffer": "^6.0.3",
"is-buffer": "^2.0.5",
"jssha": "^3.3.1",
"long": "^5.3.1",
"process": "^0.11.10",
"rhea-promise": "^3.0.0",
"tslib": "^2.7.0"
"rhea-promise": "^3.0.3",
"tslib": "^2.8.1"
},
"devDependencies": {
"@azure-tools/test-credential": "^2.1.0",
Expand All @@ -111,15 +111,15 @@
"@azure-tools/vite-plugin-browser-test-map": "^1.0.0",
"@azure/dev-tool": "^1.0.0",
"@azure/eslint-plugin-azure-sdk": "^3.0.0",
"@azure/identity": "^4.4.1",
"@azure/identity": "^4.7.0",
"@rollup/plugin-inject": "^5.0.5",
"@types/chai-as-promised": "^8.0.1",
"@types/debug": "^4.1.4",
"@types/is-buffer": "^2.0.0",
"@types/node": "^18.0.0",
"@types/ws": "^7.2.4",
"@vitest/browser": "^3.0.3",
"@vitest/coverage-istanbul": "^3.0.3",
"@vitest/browser": "^3.0.6",
"@vitest/coverage-istanbul": "^3.0.6",
"chai": "^5.1.1",
"chai-as-promised": "^8.0.0",
"chai-exclude": "^3.0.0",
Expand All @@ -128,9 +128,9 @@
"eslint": "^9.9.0",
"events": "^3.0.0",
"https-proxy-agent": "^7.0.0",
"playwright": "^1.46.1",
"playwright": "^1.50.1",
"typescript": "~5.7.2",
"vitest": "^3.0.3",
"vitest": "^3.0.6",
"ws": "^8.0.0"
},
"type": "module",
Expand Down Expand Up @@ -189,5 +189,6 @@
"default": "./dist/commonjs/experimental/index.js"
}
}
}
},
"react-native": "./dist/react-native/index.js"
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import { ServiceBusAdministrationClient } from "@azure/service-bus";
import { DefaultAzureCredential } from "@azure/identity";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

import "dotenv/config";
// Define connection string and related Service Bus entity names here
const fqdn = process.env.SERVICEBUS_FQDN || "<your-servicebus-namespace>.servicebus.windows.net";
const queueName = process.env.QUEUE_NAME || "<queue name>";

export async function main() {
export async function main(): Promise<void> {
// You can also use AAD credentials from `@azure/identity` along with the host url
// instead of the connection string for authentication.
const credential = new DefaultAzureCredential();
Expand Down
15 changes: 6 additions & 9 deletions sdk/servicebus/service-bus/samples-dev/advanced/deferral.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,28 @@
* @azsdk-weight 55
*/

import {
ServiceBusClient,
delay,
import type {
ProcessErrorArgs,
ServiceBusReceivedMessage,
ServiceBusMessage,
} from "@azure/service-bus";
import { ServiceBusClient, delay } from "@azure/service-bus";
import { DefaultAzureCredential } from "@azure/identity";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

import "dotenv/config";
// Define connection string and related Service Bus entity names here
const fqdn = process.env.SERVICEBUS_FQDN || "<your-servicebus-namespace>.servicebus.windows.net";
const queueName = process.env.QUEUE_NAME || "<queue name>";
const credential = new DefaultAzureCredential();

export async function main() {
export async function main(): Promise<void> {
await sendMessages();
await receiveMessage();
}

// Shuffle and send messages
async function sendMessages() {
async function sendMessages(): Promise<void> {
const sbClient = new ServiceBusClient(fqdn, credential);
// createSender() can also be used to create a sender for a topic.
const sender = sbClient.createSender(queueName);
Expand Down Expand Up @@ -75,7 +72,7 @@ async function sendMessages() {
await sbClient.close();
}

async function receiveMessage() {
async function receiveMessage(): Promise<void> {
const sbClient = new ServiceBusClient(fqdn, credential);

// If receiving from a subscription, you can use the createReceiver(topicName, subscriptionName) overload
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,11 @@ import { ServiceBusAdministrationClient } from "@azure/service-bus";
import { DefaultAzureCredential } from "@azure/identity";

// Load the .env file if it exists
import * as dotenv from "dotenv";
dotenv.config();

import "dotenv/config";
// Define connection string and related Service Bus entity names here
const fqdn = process.env.SERVICEBUS_FQDN || "<your-servicebus-namespace>.servicebus.windows.net";

export async function main() {
export async function main(): Promise<void> {
const credential = new DefaultAzureCredential();
const serviceBusAdministrationClient = new ServiceBusAdministrationClient(fqdn, credential);
const baseQueueName = "random-queue";
Expand Down
Loading
Loading