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] Migration guide changes for errors, api changes, etc... #17656

Merged
3 commits merged into from
Nov 18, 2020
Merged
Changes from all 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
68 changes: 34 additions & 34 deletions sdk/servicebus/azure-messaging-servicebus/migration-guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ library to share a single authentication solution between clients of different A
TokenCredential credential = new DefaultAzureCredentialBuilder()
.build();
String fullyQualifiedNamespace = "yournamespace.servicebus.windows.net";
ServiceBusSenderClient client = new ServiceBusClientBuilder()
ServiceBusSenderClient senderUsingTokenCredential = new ServiceBusClientBuilder()
.credential(fullyQualifiedNamespace, credential)
.sender()
.queueName("my-queue")
.buildClient();

// Create a sender client that will authenticate using a connection string
String connectionString = "Endpoint=sb://yournamespace.servicebus.windows.net/;SharedAccessKeyName=your-key-name;SharedAccessKey=your-key";
ServiceBusSenderClient client = new ServiceBusClientBuilder()
ServiceBusSenderClient senderUsingConnectionString = new ServiceBusClientBuilder()
.connectionString(connectionString)
.sender()
.queueName("my-queue")
Expand All @@ -139,6 +139,8 @@ While the `IQueueClient` supported the simple send operation, the `IMessageSende
like scheduling to send messages at a later time and cancelling such scheduled messages.

```java
// NOTE: this example is using code for the older package

String queueName = "my-queue";
String connectionString = "Endpoint=sb://yournamespace.servicebus.windows.net/;"
+ "SharedAccessKeyName=your-key-name;SharedAccessKey=your-key";
Expand Down Expand Up @@ -166,9 +168,11 @@ We continue to support sending bytes in the message. Though, if you are working
message directly without having to convert it to bytes first. The snippet below demonstrates the sync sender client.

```java
// code using the latest package.

// create the sync sender via the builder and its sub-builder
ServiceBusSenderClient client = new ServiceBusClientBuilder()
.connectionString(connectionString)
richardpark-msft marked this conversation as resolved.
Show resolved Hide resolved
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
.connectionString("connection string")
.sender()
.queueName("my-queue")
.buildClient();
Expand All @@ -192,13 +196,17 @@ send. This uses the sync sender as well.

```java
// create the sync sender via the builder and its sub-builder
ServiceBusSenderClient client = new ServiceBusClientBuilder()
.connectionString(connectionString)
ServiceBusSenderClient sender = new ServiceBusClientBuilder()
.connectionString("connection string")
.sender()
.queueName("my-queue")
.buildClient();

ServiceBusMessage[] inputMessageArray = new ServiceBusMessage[10];
ServiceBusMessage[] inputMessageArray = new ServiceBusMessage[] {
new ServiceBusMessage("example message"),
new ServiceBusMessage("example message 2")
};

ServiceBusMessageBatch messageBatch = sender.createBatch();

for (int i = 0; i < inputMessageArray.length; i++) {
Expand Down Expand Up @@ -233,6 +241,8 @@ handlers/callbacks, the `IMessageReceiver` provided you with ways to receive mes
batches, settle messages and renew locks.

```java
// NOTE: this example is using code for the older package

QueueClient client = new QueueClient(new ConnectionStringBuilder(connectionString, queueName),
ReceiveMode.PEEKLOCK);

Expand Down Expand Up @@ -275,23 +285,19 @@ For a more fine grained control and advanced features, you still have the `Servi
counterpart `ServiceBusReceiverAsyncClient`.

```java
// code using the latest package.

// Sample code that processes a single message
Consumer<ServiceBusReceivedMessageContext> processMessage = messageContext -> {
try {
System.out.println(messageContext.getMessage().getMessageId());
// other message processing code
messageContext.complete();
} catch (Exception ex) {
messageContext.abandon();
}
}
System.out.println(messageContext.getMessage().getMessageId());
// other message processing code
};

// Sample code that gets called if there's an error
Consumer<Throwable> processError = throwable -> {
logError(throwable);
metrics.recordError(throwable);
}
Consumer<ServiceBusErrorContext> processError = errorContext -> {
logError(errorContext.getException());
metrics.recordError(errorContext.getException());
};

// create the processor client via the builder and its sub-builder
ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
Expand All @@ -304,7 +310,6 @@ ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()

// Starts the processor in the background and returns immediately
processorClient.start();

```

### Working with sessions
Expand All @@ -324,25 +329,20 @@ The below code snippet shows you how to use the processor client to receive mess
```java
// Sample code that processes a single message
Consumer<ServiceBusReceivedMessageContext> processMessage = messageContext -> {
try {
System.out.println(messageContext.getMessage().getMessageId());
// other message processing code
messageContext.complete();
} catch (Exception ex) {
messageContext.abandon();
}
}
System.out.println(messageContext.getMessage().getMessageId());
// other message processing code
};

// Sample code that gets called if there's an error
Consumer<Throwable> processError = throwable -> {
logError(throwable);
metrics.recordError(throwable);
}
Consumer<ServiceBusErrorContext> processError = errorContext -> {
logError(errorContext.getException());
metrics.recordError(errorContext.getException());
};

// create the processor client via the builder and its sub-builder
ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder()
.connectionString("connection-string")
.processor()
.sessionProcessor()
.queueName("queue-name")
.maxConcurrentSessions(3)
.processMessage(processMessage)
Expand All @@ -363,7 +363,7 @@ While the below code uses `acceptSession()` that takes a sessionId, you can also

```java
ServiceBusSessionReceiverClient sessionClient = new ServiceBusClientBuilder()
.connectionString(connectionString)
.connectionString("connection string")
.sessionReceiver()
.queueName("queue")
.buildClient();
Expand Down