diff --git a/sdk/servicebus/azure-messaging-servicebus/migration-guide.md b/sdk/servicebus/azure-messaging-servicebus/migration-guide.md index cf252a91b7da6..f32a38e008c94 100644 --- a/sdk/servicebus/azure-messaging-servicebus/migration-guide.md +++ b/sdk/servicebus/azure-messaging-servicebus/migration-guide.md @@ -115,7 +115,7 @@ 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") @@ -123,7 +123,7 @@ ServiceBusSenderClient client = new ServiceBusClientBuilder() // 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") @@ -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"; @@ -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) +ServiceBusSenderClient sender = new ServiceBusClientBuilder() + .connectionString("connection string") .sender() .queueName("my-queue") .buildClient(); @@ -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++) { @@ -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); @@ -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 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 processError = throwable -> { - logError(throwable); - metrics.recordError(throwable); -} +Consumer processError = errorContext -> { + logError(errorContext.getException()); + metrics.recordError(errorContext.getException()); +}; // create the processor client via the builder and its sub-builder ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder() @@ -304,7 +310,6 @@ ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder() // Starts the processor in the background and returns immediately processorClient.start(); - ``` ### Working with sessions @@ -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 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 processError = throwable -> { - logError(throwable); - metrics.recordError(throwable); -} +Consumer 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) @@ -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();