diff --git a/sdk/android/azure-samples/src/androidTest/java/com/azure/android/ServicebusSampleTests.java b/sdk/android/azure-samples/src/androidTest/java/com/azure/android/ServicebusSampleTests.java index 4dd252f0fa65c..72fb077c5865f 100644 --- a/sdk/android/azure-samples/src/androidTest/java/com/azure/android/ServicebusSampleTests.java +++ b/sdk/android/azure-samples/src/androidTest/java/com/azure/android/ServicebusSampleTests.java @@ -206,9 +206,7 @@ public void serviceBusSessionProcessor() throws InterruptedException { .processError(ServiceBusSessionProcessor::processError) .buildProcessorClient(); - if (processorClient.equals(null)) { - throw new RuntimeException("Sample was not successful: processorClient equals null"); - } + assertNotNull(processorClient); Log.i(PROCESSOR_TAG, "Starting the processor"); processorClient.start(); diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/ConditionalRequestAsync.java b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/ConditionalRequestAsync.java new file mode 100644 index 0000000000000..5214d7c2c07f3 --- /dev/null +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/ConditionalRequestAsync.java @@ -0,0 +1,80 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.android.appconfiguration; + + +import android.util.Log; + +import com.azure.data.appconfiguration.ConfigurationAsyncClient; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.data.appconfiguration.models.ConfigurationSetting; +import com.azure.identity.ClientSecretCredential; + + +/** + * Sample demonstrates how to add, get, and delete a configuration setting by conditional request asynchronously. + */ +public class ConditionalRequestAsync { + + private static final String TAG = "ConditionRequestAsyncOutput"; + + /** + * Runs the sample algorithm and demonstrates how to add, get, and delete a configuration setting by conditional + * request asynchronously + * @throws InterruptedException when a thread is waiting, sleeping, or otherwise occupied, + * and the thread is interrupted, either before or during the activity. + */ + public static void main(String endpoint, ClientSecretCredential credential) throws InterruptedException { + + // Instantiate a client that will be used to call the service. + final ConfigurationAsyncClient client = new ConfigurationClientBuilder() + .credential(credential) + .endpoint(endpoint) + .buildAsyncClient(); + + ConfigurationSetting setting = new ConfigurationSetting().setKey("key").setLabel("label").setValue("value"); + + // If you want to conditionally update the setting, set `ifUnchanged` to true. If the ETag of the + // given setting matches the one in the service, then the setting is updated. Otherwise, it is + // not updated. + // If the given setting is not exist in the service, the setting will be added to the service. + client.setConfigurationSettingWithResponse(setting, true).subscribe( + result -> { + final ConfigurationSetting output = result.getValue(); + final int statusCode = result.getStatusCode(); + Log.i(TAG, String.format("Set config with status code: %s, Key: %s, Value: %s", statusCode, output.getKey(), + output.getValue())); + }, + error -> Log.e(TAG, "There was an error while setting the setting: " + error)); + + + + + // If you want to conditionally retrieve the setting, set `ifChanged` to true. If the ETag of the + // given setting matches the one in the service, then 304 status code with null value returned in the response. + // Otherwise, a setting with new ETag returned, which is the latest setting retrieved from the service. + client.getConfigurationSettingWithResponse(setting, null, true).subscribe( + result -> { + final ConfigurationSetting output = result.getValue(); + final int statusCode = result.getStatusCode(); + Log.i(TAG, String.format("Get config with status code: %s, Key: %s, Value: %s", statusCode, output.getKey(), + output.getValue())); + }, + error -> Log.e(TAG, "There was an error while getting the setting: " + error)); + + + // If you want to conditionally delete the setting, set `ifUnchanged` to true. If the ETag of the + // given setting matches the one in the service, then the setting is deleted. Otherwise, it is + // not deleted. + client.deleteConfigurationSettingWithResponse(setting, true).subscribe( + result -> { + final ConfigurationSetting output = result.getValue(); + final int statusCode = result.getStatusCode(); + Log.i(TAG, String.format("Deleted config with status code: %s, Key: %s, Value: %s", statusCode, output.getKey(), + output.getValue())); + }, + error -> Log.e(TAG, "There was an error while deleting the setting: " + error)); + } + +} diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/HelloWorld.java b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/HelloWorld.java new file mode 100644 index 0000000000000..9da2685b9b3b9 --- /dev/null +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/HelloWorld.java @@ -0,0 +1,50 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.android.appconfiguration; + +import android.util.Log; + +import com.azure.data.appconfiguration.ConfigurationClient; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.data.appconfiguration.models.ConfigurationSetting; +import com.azure.identity.ClientSecretCredential; + +/** + * Sample demonstrates how to add, get, and delete a configuration setting. + */ +public class HelloWorld { + /** + * Runs the sample algorithm and demonstrates how to add, get, and delete a configuration setting. + */ + + private static final String TAG = "AppconfigHelloWorldOutput"; + public static void main(String endpoint, ClientSecretCredential credential) { + final ConfigurationClient client = new ConfigurationClientBuilder() + .credential(credential) + .endpoint(endpoint) + .buildClient(); + + // Name of the key to add to the configuration service. + final String key = "hello"; + final String value = "world"; + + // Sometimes the client can be set to read only from previous tests. + client.setReadOnly(key, null, false); + + Log.i(TAG, "Beginning of synchronous sample..."); + + ConfigurationSetting setting = client.setConfigurationSetting(key, null, value); + Log.i(TAG, String.format("[SetConfigurationSetting] Key: %s, Value: %s", setting.getKey(), setting.getValue())); + + + setting = client.getConfigurationSetting(key, null, null); + Log.i(TAG, String.format("[GetConfigurationSetting] Key: %s, Value: %s", setting.getKey(), setting.getValue())); + + setting = client.deleteConfigurationSetting(key, null); + Log.i(TAG, String.format("[DeleteConfigurationSetting] Key: %s, Value: %s", setting.getKey(), setting.getValue())); + + Log.i(TAG, "End of synchronous sample."); + + } +} diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/ReadOnly.java b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/ReadOnly.java new file mode 100644 index 0000000000000..f3b0fe7b0a4da --- /dev/null +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/ReadOnly.java @@ -0,0 +1,46 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.android.appconfiguration; + +import android.util.Log; + +import com.azure.data.appconfiguration.ConfigurationClient; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.data.appconfiguration.models.ConfigurationSetting; +import com.azure.identity.ClientSecretCredential; + +/** + * Sample demonstrates how to add, get, and delete a configuration setting. + */ +public class ReadOnly { + /** + * Runs the sample algorithm and demonstrates how to add, get, and delete a configuration setting. + */ + + private static final String TAG = "AppconfigReadOnlyOutput"; + + public static void main(String endpoint, ClientSecretCredential credential) { + + final ConfigurationClient client = new ConfigurationClientBuilder() + .credential(credential) + .endpoint(endpoint) + .buildClient(); + + // Name of the key to add to the configuration service. + final String key = "hello"; + final String value = "world"; + + final ConfigurationSetting setting = client.setConfigurationSetting(key, null, value); + // Read-Only + final ConfigurationSetting readOnlySetting = client.setReadOnly(setting.getKey(), setting.getLabel(), true); + Log.i(TAG, String.format("Setting is read-only now, Key: %s, Value: %s", + readOnlySetting.getKey(), readOnlySetting.getValue())); + + // Clear Read-Only + final ConfigurationSetting clearedReadOnlySetting = client.setReadOnly(setting.getKey(), setting.getLabel(), false); + Log.i(TAG, String.format("Setting is no longer read-only, Key: %s, Value: %s", + clearedReadOnlySetting.getKey(), clearedReadOnlySetting.getValue())); + + } +} diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/SecretReferenceConfigurationSettingSample.java b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/SecretReferenceConfigurationSettingSample.java index d087e8f387390..90f183103d1c8 100644 --- a/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/SecretReferenceConfigurationSettingSample.java +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/SecretReferenceConfigurationSettingSample.java @@ -6,13 +6,64 @@ import android.util.Log; +import com.azure.core.http.rest.PagedIterable; +import com.azure.data.appconfiguration.ConfigurationClient; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; +import com.azure.data.appconfiguration.models.ConfigurationSetting; import com.azure.data.appconfiguration.models.SecretReferenceConfigurationSetting; - +import com.azure.data.appconfiguration.models.SettingSelector; +import com.azure.identity.ClientSecretCredential; public class SecretReferenceConfigurationSettingSample { private static final String TAG = "SecretReferenceConfigurationSettingOutput"; + public static void main(String endPoint, ClientSecretCredential credential) { + + final ConfigurationClient client = new ConfigurationClientBuilder() + .credential(credential) + .endpoint(endPoint) + .buildClient(); + + // Name of the key to add to the configuration service. + final String key = "hello"; + final String secretIdValue = "{the-keyVault-secret-id-uri}"; + + Log.i(TAG, "Beginning of synchronous sample..."); + + SecretReferenceConfigurationSetting referenceConfigurationSetting = + new SecretReferenceConfigurationSetting(key, secretIdValue); + + // setConfigurationSetting adds or updates a setting to Azure App Configuration store. Alternatively, you can + // call addConfigurationSetting which only succeeds if the setting does not exist in the store. Or, + // you can call setConfigurationSetting to update a setting that is already present in the store. + Log.i(TAG, "[Set-SecretReferenceConfigurationSetting]"); + SecretReferenceConfigurationSetting setting = (SecretReferenceConfigurationSetting) client.setConfigurationSetting(referenceConfigurationSetting); + printSecretReferenceConfigurationSetting(setting); + + Log.i(TAG, "[Get-SecretReferenceConfigurationSetting]"); + setting = (SecretReferenceConfigurationSetting) client.getConfigurationSetting(setting); + printSecretReferenceConfigurationSetting(setting); + + Log.i(TAG, "[List-SecretReferenceConfigurationSetting]"); + final PagedIterable configurationSettings = client.listConfigurationSettings(new SettingSelector()); + for (ConfigurationSetting configurationSetting : configurationSettings) { + if (configurationSetting instanceof SecretReferenceConfigurationSetting) { + Log.i(TAG, "-Listing-SecretReferenceConfigurationSetting"); + printSecretReferenceConfigurationSetting((SecretReferenceConfigurationSetting) configurationSetting); + } else { + Log.i(TAG, "-Listing-non-SecretReferenceConfigurationSetting"); + Log.i(TAG, String.format("Key: %s, Value: %s%n", configurationSetting.getKey(), configurationSetting.getValue())); + } + } + + Log.i(TAG, "[Delete-SecretReferenceConfigurationSetting"); + setting = (SecretReferenceConfigurationSetting) client.deleteConfigurationSetting(setting); + printSecretReferenceConfigurationSetting(setting); + + Log.i(TAG, "End of synchronous sample."); + } + public static void printSecretReferenceConfigurationSetting(SecretReferenceConfigurationSetting setting) { Log.i(TAG, String.format("Key: %s, Secret ID: %s, Content Type: %s, Value: %s%n", setting.getKey(), setting.getSecretId(), setting.getContentType(), setting.getValue())); diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/WatchFeature.java b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/WatchFeature.java index 61be82afb29bb..d95a5ef364168 100644 --- a/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/WatchFeature.java +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/appconfiguration/WatchFeature.java @@ -6,8 +6,11 @@ import android.util.Log; import com.azure.data.appconfiguration.ConfigurationClient; +import com.azure.data.appconfiguration.ConfigurationClientBuilder; import com.azure.data.appconfiguration.models.ConfigurationSetting; +import com.azure.identity.ClientSecretCredential; +import java.util.Arrays; import java.util.List; import java.util.stream.Collectors; @@ -18,6 +21,55 @@ public class WatchFeature { private static final String TAG = "WatchFeatureOutput"; + public static void main(String endPoint, ClientSecretCredential credential) { + + // Instantiate a client that will be used to call the service. + ConfigurationClient client = new ConfigurationClientBuilder() + .credential(credential) + .endpoint(endPoint) + .buildClient(); + + + // Prepare a list of watching settings and update one same setting value to the service. + String prodDBConnectionKey = "prodDBConnection"; + String prodDBConnectionLabel = "prodLabel"; + + // Assume we have a list of watching setting that stored somewhere. + List watchingSettings = Arrays.asList( + client.addConfigurationSetting(prodDBConnectionKey, prodDBConnectionLabel, "prodValue"), + client.addConfigurationSetting("stageDBConnection", "stageLabel", "stageValue") + ); + + Log.i(TAG, "Watching settings:"); + for (ConfigurationSetting setting : watchingSettings) { + Log.i(TAG, String.format("\tkey=%s, label=%s, value=%s, ETag=%s.%n", + setting.getKey(), setting.getLabel(), setting.getValue(), setting.getETag())); + } + + // One of the watching settings is been updated by someone in other place. + ConfigurationSetting updatedSetting = client.setConfigurationSetting( + prodDBConnectionKey, prodDBConnectionLabel, "updatedProdValue"); + Log.i(TAG, "Updated settings:"); + Log.i(TAG, String.format("\tkey=%s, label=%s, value=%s, ETag=%s.%n", + updatedSetting.getKey(), updatedSetting.getLabel(), updatedSetting.getValue(), updatedSetting.getETag())); + + // Updates the watching settings if needed, and only returns a list of updated settings. + List refreshedSettings = refresh(client, watchingSettings); + + Log.i(TAG, "Refreshed settings:"); + for (ConfigurationSetting setting : refreshedSettings) { + Log.i(TAG, String.format("\tkey=%s, label=%s, value=%s, ETag=%s.%n", + setting.getKey(), setting.getLabel(), setting.getValue(), setting.getETag())); + } + + // Cleaning up after ourselves by deleting the values. + Log.i(TAG, "Deleting settings:"); + watchingSettings.forEach(setting -> { + client.deleteConfigurationSetting(setting.getKey(), setting.getLabel()); + Log.i(TAG, String.format("\tkey: %s, value: %s.%n", setting.getKey(), setting.getValue())); + }); + } + /** * A refresh method that runs every day to update settings and returns a updated settings. * diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/PeekMessageAsync.java b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/PeekMessageAsync.java new file mode 100644 index 0000000000000..05803b1f72fda --- /dev/null +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/PeekMessageAsync.java @@ -0,0 +1,56 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.android.servicebus; + +import com.azure.identity.ClientSecretCredential; +import com.azure.messaging.servicebus.ServiceBusClientBuilder; +import com.azure.messaging.servicebus.ServiceBusReceiverAsyncClient; + +import android.util.Log; + +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +/** + * Sample example showing how peek would work. + */ +public class PeekMessageAsync { + + private static final String TAG = "ServiceBusPeekMessageAsyncOutput"; + + + public static void main(String queueName, ClientSecretCredential credential) throws InterruptedException { + AtomicBoolean sampleSuccessful = new AtomicBoolean(false); + CountDownLatch countdownLatch = new CountDownLatch(1); + + ServiceBusReceiverAsyncClient receiver = new ServiceBusClientBuilder() + .fullyQualifiedNamespace("android-service-bus.servicebus.windows.net") + .credential(credential) + .receiver() + .queueName(queueName) + .buildAsyncClient(); + + + receiver.peekMessage().subscribe( + message -> { + Log.i(TAG, "Received Message Id: " + message.getMessageId()); + Log.i(TAG, "Received Message: " + message.getBody()); + }, + error -> Log.e(TAG, "Error occurred while receiving message: " + error), + () -> { + Log.i(TAG, "Receiving complete."); + sampleSuccessful.set(true); + }); + + // Subscribe is not a blocking call so we wait here so the program does not end while finishing + // the peek operation. + countdownLatch.await(10, TimeUnit.SECONDS); + + // Close the receiver. + receiver.close(); + + + } +} diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/SendMessageBatch.java b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/SendMessageBatch.java new file mode 100644 index 0000000000000..d6f7c2995c208 --- /dev/null +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/SendMessageBatch.java @@ -0,0 +1,71 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.android.servicebus; + +import com.azure.core.util.BinaryData; +import com.azure.identity.ClientSecretCredential; +import com.azure.messaging.servicebus.ServiceBusClientBuilder; +import com.azure.messaging.servicebus.ServiceBusMessage; +import com.azure.messaging.servicebus.ServiceBusMessageBatch; +import com.azure.messaging.servicebus.ServiceBusSenderClient; +import com.azure.messaging.servicebus.models.CreateMessageBatchOptions; + +import android.util.Log; + +import java.util.Arrays; +import java.util.List; + +/** + * Sample demonstrates how to send {@link ServiceBusMessageBatch} to an Azure Service Bus Topic with the synchronous + * sender. + */ +public class SendMessageBatch { + + private static final String TAG = "ServiceBusSendMessageBatchOutput"; + + public static void main(String queueName, ClientSecretCredential credential) { + List testMessages = Arrays.asList( + new ServiceBusMessage(BinaryData.fromString("Green")), + new ServiceBusMessage(BinaryData.fromString("Red")), + new ServiceBusMessage(BinaryData.fromString("Blue")), + new ServiceBusMessage(BinaryData.fromString("Orange"))); + + // Instantiate a client that will be used to call the service. + ServiceBusSenderClient sender = new ServiceBusClientBuilder() + .fullyQualifiedNamespace("android-service-bus.servicebus.windows.net") + .credential(credential) + .sender() + .queueName(queueName) + .buildClient(); + + // Creates an ServiceBusMessageBatch where the ServiceBus. + // If no maximumSizeInBatch is set, the maximum message size is used. + ServiceBusMessageBatch currentBatch = sender.createMessageBatch( + new CreateMessageBatchOptions().setMaximumSizeInBytes(1024)); + + // We try to add as many messages as a batch can fit based on the maximum size and send to Service Bus when + // the batch can hold no more messages. Create a new batch for next set of messages and repeat until all + // messages are sent. + for (ServiceBusMessage message : testMessages) { + if (currentBatch.tryAddMessage(message)) { + continue; + } + + // The batch is full, so we create a new batch and send the batch. + sender.sendMessages(currentBatch); + currentBatch = sender.createMessageBatch(); + + // Add that message that we couldn't before. + if (!currentBatch.tryAddMessage(message)) { + Log.e(TAG, String.format("Message is too large for an empty batch. Skipping. Max size: %s. Message: %s%n", + currentBatch.getMaxSizeInBytes(), message.getBody().toString())); + } + } + + sender.sendMessages(currentBatch); + + //close the client + sender.close(); + } +} diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/SendSessionMessageAsync.java b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/SendSessionMessageAsync.java new file mode 100644 index 0000000000000..b58b4cffa8401 --- /dev/null +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/SendSessionMessageAsync.java @@ -0,0 +1,70 @@ +// Copyright (c) Microsoft Corporation. All rights reserved. +// Licensed under the MIT License. + +package com.azure.android.servicebus; + +import com.azure.core.util.BinaryData; +import com.azure.identity.ClientSecretCredential; +import com.azure.messaging.servicebus.ServiceBusClientBuilder; +import com.azure.messaging.servicebus.ServiceBusMessage; +import com.azure.messaging.servicebus.ServiceBusSenderAsyncClient; + + +import android.util.Log; + +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; + +import static java.nio.charset.StandardCharsets.UTF_8; + +public class SendSessionMessageAsync { + + private static final String TAG = "ServiceBusSendSessionMessageAsyncOutput"; + + + public static void main(String queueName, ClientSecretCredential credential) throws InterruptedException { + AtomicBoolean sampleSuccessful = new AtomicBoolean(false); + CountDownLatch countdownLatch = new CountDownLatch(1); + + // We want all our greetings in the same session to be processed. + String sessionId = "greetings-id"; + + // Any clients built from the same ServiceBusClientBuilder share the same connection. + ServiceBusClientBuilder builder = new ServiceBusClientBuilder() + .fullyQualifiedNamespace("android-service-bus.servicebus.windows.net") + .credential(credential); + + // Instantiate a client that will be used to send messages. + ServiceBusSenderAsyncClient sender = builder + .sender() + .queueName(queueName) + .buildAsyncClient(); + + // Setting the sessionId parameter ensures all messages end up in the same session and are received in order. + List messages = Arrays.asList( + new ServiceBusMessage(BinaryData.fromBytes("Hello".getBytes(UTF_8))).setSessionId(sessionId), + new ServiceBusMessage(BinaryData.fromBytes("Bonjour".getBytes(UTF_8))).setSessionId(sessionId) + ); + + // This sends all the messages in a single message batch. + // This call returns a Mono, which we subscribe to. It completes successfully when the + // event has been delivered to the Service queue or topic. It completes with an error if an exception occurred + // while sending the message. + sender.sendMessages(messages).subscribe(unused -> Log.i(TAG, "Batch sent."), + error -> Log.e(TAG, "Error occurred while publishing message batch: " + error), + () -> { + Log.i(TAG, "Batch send complete."); + sampleSuccessful.set(true); + }); + + // subscribe() is not a blocking call. We wait here so the program does not end before the send is complete. + countdownLatch.await(10, TimeUnit.SECONDS); + + // Close the sender. + sender.close(); + + } +} diff --git a/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/ServiceBusSessionProcessor.java b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/ServiceBusSessionProcessor.java index bce2226f9d965..1ef43aadeb271 100644 --- a/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/ServiceBusSessionProcessor.java +++ b/sdk/android/azure-samples/src/main/java/com/azure/android/servicebus/ServiceBusSessionProcessor.java @@ -4,6 +4,8 @@ package com.azure.android.servicebus; +import com.azure.identity.ClientSecretCredential; +import com.azure.messaging.servicebus.ServiceBusClientBuilder; import com.azure.messaging.servicebus.ServiceBusErrorContext; import com.azure.messaging.servicebus.ServiceBusException; import com.azure.messaging.servicebus.ServiceBusProcessorClient; @@ -12,6 +14,8 @@ import android.util.Log; +import java.util.concurrent.TimeUnit; + /** * Sample to demonstrate the creation of a session-enabled {@link ServiceBusProcessorClient} and starting the processor * to receive messages. @@ -20,6 +24,33 @@ public class ServiceBusSessionProcessor { private static final String TAG = "ServiceBusSessionProcessorOutput"; + public static void main(String queueName, ClientSecretCredential credential) throws InterruptedException { + ServiceBusProcessorClient processorClient = new ServiceBusClientBuilder() + .fullyQualifiedNamespace("android-service-bus.servicebus.windows.net") + .credential(credential) + .sessionProcessor() + .queueName(queueName) + .maxConcurrentSessions(2) + .processMessage(ServiceBusSessionProcessor::processMessage) + .processError(ServiceBusSessionProcessor::processError) + .buildProcessorClient(); + + Log.i(TAG, "Starting the processor"); + processorClient.start(); + + TimeUnit.SECONDS.sleep(1); + Log.i(TAG, "Stopping the processor"); + processorClient.stop(); + + TimeUnit.SECONDS.sleep(1); + Log.i(TAG, "Resuming the processor"); + processorClient.start(); + + TimeUnit.SECONDS.sleep(1); + Log.i(TAG, "Closing the processor"); + processorClient.close(); + } + /** * Processes each message from the Service Bus entity. *