Partager via


QueueClientBuilder Class

  • java.lang.Object
    • com.azure.storage.queue.QueueClientBuilder

Implements

public final class QueueClientBuilder
implements TokenCredentialTrait<QueueClientBuilder>, ConnectionStringTrait<QueueClientBuilder>, AzureNamedKeyCredentialTrait<QueueClientBuilder>, AzureSasCredentialTrait<QueueClientBuilder>, HttpTrait<QueueClientBuilder>, ConfigurationTrait<QueueClientBuilder>, EndpointTrait<QueueClientBuilder>

This class provides a fluent builder API to help aid the configuration and instantiation of the QueueClient and QueueAsyncClient, calling buildClient() constructs an instance of QueueClient and calling buildAsyncClient() constructs an instance of QueueAsyncClient.

The client needs the endpoint of the Azure Storage Queue service, name of the queue, and authorization credentials. endpoint(String endpoint) gives the builder the endpoint and may give the builder the queueName(String queueName) and a sasToken(String sasToken) that authorizes the client.

Instantiating a synchronous Queue Client with SAS token

QueueClient client = new QueueClientBuilder()
     .endpoint("https://${accountName}.queue.core.windows.net?${SASToken}")
     .buildClient();

Instantiating an Asynchronous Queue Client with SAS token

QueueAsyncClient queueAsyncClient = new QueueClientBuilder()
     .endpoint("https://{accountName}.queue.core.windows.net?{SASToken}")
     .buildAsyncClient();

If the endpoint doesn't contain the queue name or SAS token they may be set using queueName(String queueName) and sasToken(String sasToken).

Instantiating a synchronous Queue Client with credential

QueueClient client = new QueueClientBuilder()
     .endpoint("https://${accountName}.queue.core.windows.net")
     .queueName("myqueue")
     .sasToken("{SASTokenQueryParams}")
     .buildClient();

Instantiating an Asynchronous Queue Client with credential

QueueAsyncClient queueAsyncClient = new QueueClientBuilder()
     .endpoint("https://{accountName}.queue.core.windows.net")
     .queueName("myqueue")
     .sasToken("{SASTokenQueryParams}")
     .buildAsyncClient();

Another way to authenticate the client is using a StorageSharedKeyCredential. To create a StorageSharedKeyCredential a connection string from the Storage Queue service must be used. Set the StorageSharedKeyCredential with connectionString(String connectionString). If the builder has both a SAS token and StorageSharedKeyCredential the StorageSharedKeyCredential will be preferred when authorizing requests sent to the service.

Instantiating a synchronous Queue Client with connection string.

String connectionString = "DefaultEndpointsProtocol=https;AccountName={name};"
     + "AccountKey={key};EndpointSuffix={core.windows.net}";
 QueueClient client = new QueueClientBuilder()
     .connectionString(connectionString)
     .buildClient();

Instantiating an Asynchronous Queue Client with connection string.

String connectionString = "DefaultEndpointsProtocol=https;AccountName={name};"
     + "AccountKey={key};EndpointSuffix={core.windows.net}";
 QueueAsyncClient queueAsyncClient = new QueueClientBuilder()
     .connectionString(connectionString)
     .buildAsyncClient();

Constructor Summary

Constructor Description
QueueClientBuilder()

Creates a builder instance that is able to configure and construct QueueClient and QueueAsyncClient.

Method Summary

Modifier and Type Method and Description
QueueClientBuilder addPolicy(HttpPipelinePolicy pipelinePolicy)

Adds a HttpPipelinePolicy to apply on each request sent.

QueueClientBuilder audience(QueueAudience audience)

Sets the Audience to use for authentication with Azure Active Directory (AAD).

QueueAsyncClient buildAsyncClient()

Creates a QueueAsyncClient based on options set in the builder.

QueueClient buildClient()

Creates a QueueClient based on options set in the builder.

QueueClientBuilder clientOptions(ClientOptions clientOptions)

Allows for setting common properties such as application ID, headers, proxy configuration, etc.

QueueClientBuilder configuration(Configuration configuration)

Sets the configuration object used to retrieve environment configuration values during building of the client.

QueueClientBuilder connectionString(String connectionString)

Sets the connection string to connect to the service.

QueueClientBuilder credential(AzureNamedKeyCredential credential)

Sets the AzureNamedKeyCredential used to authorize requests sent to the service.

QueueClientBuilder credential(AzureSasCredential credential)

Sets the AzureSasCredential used to authorize requests sent to the service.

QueueClientBuilder credential(TokenCredential credential)

Sets the TokenCredential used to authorize requests sent to the service.

QueueClientBuilder credential(StorageSharedKeyCredential credential)

Sets the StorageSharedKeyCredential used to authorize requests sent to the service.

QueueClientBuilder endpoint(String endpoint)

Sets the endpoint for the Azure Storage Queue instance that the client will interact with.

static HttpLogOptions getDefaultHttpLogOptions()

Gets the default Storage allowlist log headers and query parameters.

QueueClientBuilder httpClient(HttpClient httpClient)

Sets the HttpClient to use for sending and receiving requests to and from the service.

QueueClientBuilder httpLogOptions(HttpLogOptions logOptions)

Sets the HttpLogOptions to use when sending and receiving requests to and from the service.

QueueClientBuilder messageEncoding(QueueMessageEncoding messageEncoding)

Sets the queue message encoding.

QueueClientBuilder pipeline(HttpPipeline httpPipeline)

Sets the HttpPipeline to use for the service client.

QueueClientBuilder processMessageDecodingError(Consumer<QueueMessageDecodingError> processMessageDecodingErrorHandler)

Sets the handler that performs the tasks needed when a message is received or peaked from the queue but cannot be decoded.

QueueClientBuilder processMessageDecodingErrorAsync(Function<QueueMessageDecodingError,Mono<Void>> processMessageDecodingErrorAsyncHandler)

Sets the asynchronous handler that performs the tasks needed when a message is received or peaked from the queue but cannot be decoded.

QueueClientBuilder queueName(String queueName)

Sets the name of the queue that the client will interact with.

QueueClientBuilder retryOptions(RetryOptions retryOptions)

Sets the RetryOptions for all the requests made through the client.

QueueClientBuilder retryOptions(RequestRetryOptions retryOptions)

Sets the request retry options for all the requests made through the client.

QueueClientBuilder sasToken(String sasToken)

Sets the SAS token used to authorize requests sent to the service.

QueueClientBuilder serviceVersion(QueueServiceVersion version)

Sets the QueueServiceVersion that is used when making API requests.

Methods inherited from java.lang.Object

Constructor Details

QueueClientBuilder

public QueueClientBuilder()

Creates a builder instance that is able to configure and construct QueueClient and QueueAsyncClient.

Method Details

addPolicy

public QueueClientBuilder addPolicy(HttpPipelinePolicy pipelinePolicy)

Adds a HttpPipelinePolicy to apply on each request sent.

Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a HttpPipeline is specified, this takes precedence over all other APIs in the trait, and they will be ignored. If no HttpPipeline is specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if an HttpPipeline is specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.

Parameters:

pipelinePolicy - A HttpPipelinePolicy.

Returns:

the updated QueueClientBuilder object

audience

public QueueClientBuilder audience(QueueAudience audience)

Sets the Audience to use for authentication with Azure Active Directory (AAD). The audience is not considered when using a shared key.

Parameters:

audience - QueueAudience to be used when requesting a token from Azure Active Directory (AAD).

Returns:

the updated QueueClientBuilder object

buildAsyncClient

public QueueAsyncClient buildAsyncClient()

Creates a QueueAsyncClient based on options set in the builder. Every time buildAsyncClient() is called a new instance of QueueAsyncClient is created.

If pipeline(HttpPipeline httpPipeline) is set, then the pipeline, endpoint(String endpoint), and queueName(String queueName) are used to create the QueueAsyncClient. All other builder settings are ignored.

Returns:

A QueueAsyncClient with the options set from the builder.

buildClient

public QueueClient buildClient()

Creates a QueueClient based on options set in the builder. Every time buildClient() is called a new instance of QueueClient is created.

If pipeline(HttpPipeline httpPipeline) is set, then the pipeline, endpoint(String endpoint), and queueName(String queueName) are used to create the QueueAsyncClient. All other builder settings are ignored.

Returns:

A QueueClient with the options set from the builder.

clientOptions

public QueueClientBuilder clientOptions(ClientOptions clientOptions)

Allows for setting common properties such as application ID, headers, proxy configuration, etc. Note that it is recommended that this method be called with an instance of the HttpClientOptions class (a subclass of the ClientOptions base class). The HttpClientOptions subclass provides more configuration options suitable for HTTP clients, which is applicable for any class that implements this HttpTrait interface.

Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a HttpPipeline is specified, this takes precedence over all other APIs in the trait, and they will be ignored. If no HttpPipeline is specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if an HttpPipeline is specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.

Parameters:

clientOptions - A configured instance of HttpClientOptions.

Returns:

the updated QueueClientBuilder object

configuration

public QueueClientBuilder configuration(Configuration configuration)

Sets the configuration object used to retrieve environment configuration values during building of the client.

Parameters:

configuration - Configuration store used to retrieve environment configurations.

Returns:

the updated QueueClientBuilder object

connectionString

public QueueClientBuilder connectionString(String connectionString)

Sets the connection string to connect to the service.

Parameters:

connectionString - Connection string of the storage account.

Returns:

the updated QueueClientBuilder

credential

public QueueClientBuilder credential(AzureNamedKeyCredential credential)

Sets the AzureNamedKeyCredential used to authorize requests sent to the service.

Parameters:

credential - AzureNamedKeyCredential.

Returns:

the updated QueueClientBuilder

credential

public QueueClientBuilder credential(AzureSasCredential credential)

Sets the AzureSasCredential used to authorize requests sent to the service.

Parameters:

credential - AzureSasCredential used to authorize requests sent to the service.

Returns:

the updated QueueClientBuilder

credential

public QueueClientBuilder credential(TokenCredential credential)

Sets the TokenCredential used to authorize requests sent to the service. Refer to the Azure SDK for Java identity and authentication documentation for more details on proper usage of the TokenCredential type.

Parameters:

credential - TokenCredential used to authorize requests sent to the service.

Returns:

the updated QueueClientBuilder

credential

public QueueClientBuilder credential(StorageSharedKeyCredential credential)

Sets the StorageSharedKeyCredential used to authorize requests sent to the service.

Parameters:

Returns:

the updated QueueClientBuilder

endpoint

public QueueClientBuilder endpoint(String endpoint)

Sets the endpoint for the Azure Storage Queue instance that the client will interact with.

The first path segment, if the endpoint contains path segments, will be assumed to be the name of the queue that the client will interact with.

Query parameters of the endpoint will be parsed in an attempt to generate a sasToken(String sasToken) to authenticate requests sent to the service.

Parameters:

endpoint - The URL of the Azure Storage Queue instance to send service requests to and receive responses from.

Returns:

the updated QueueClientBuilder object

getDefaultHttpLogOptions

public static HttpLogOptions getDefaultHttpLogOptions()

Gets the default Storage allowlist log headers and query parameters.

Returns:

the default http log options.

httpClient

public QueueClientBuilder httpClient(HttpClient httpClient)

Sets the HttpClient to use for sending and receiving requests to and from the service.

Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a HttpPipeline is specified, this takes precedence over all other APIs in the trait, and they will be ignored. If no HttpPipeline is specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if an HttpPipeline is specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.

Parameters:

httpClient - The HttpClient to use for requests.

Returns:

the updated QueueClientBuilder object

httpLogOptions

public QueueClientBuilder httpLogOptions(HttpLogOptions logOptions)

Sets the HttpLogOptions to use when sending and receiving requests to and from the service. If a logLevel is not provided, default value of HttpLogDetailLevel#NONE is set.

Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a HttpPipeline is specified, this takes precedence over all other APIs in the trait, and they will be ignored. If no HttpPipeline is specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if an HttpPipeline is specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.

Parameters:

logOptions - The HttpLogOptions to use when sending and receiving requests to and from the service.

Returns:

the updated QueueClientBuilder object

messageEncoding

public QueueClientBuilder messageEncoding(QueueMessageEncoding messageEncoding)

Sets the queue message encoding.

Parameters:

messageEncoding - QueueMessageEncoding.

Returns:

the updated QueueClientBuilder object

pipeline

public QueueClientBuilder pipeline(HttpPipeline httpPipeline)

Sets the HttpPipeline to use for the service client.

Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a HttpPipeline is specified, this takes precedence over all other APIs in the trait, and they will be ignored. If no HttpPipeline is specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if an HttpPipeline is specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.

The endpoint(String endpoint) is not ignored when pipeline is set.

Parameters:

httpPipeline - HttpPipeline to use for sending service requests and receiving responses.

Returns:

the updated QueueClientBuilder object

processMessageDecodingError

public QueueClientBuilder processMessageDecodingError(Consumer processMessageDecodingErrorHandler)

Sets the handler that performs the tasks needed when a message is received or peaked from the queue but cannot be decoded.

Such message can be received or peaked when queue is expecting certain QueueMessageEncoding but there's another producer that is not encoding messages in expected way. I.e. the queue contains messages with different encoding.

QueueMessageDecodingError contains QueueAsyncClient for the queue that has received the message as well as getQueueMessageItem() or getPeekedMessageItem() with raw body, i.e. no decoding will be attempted so that body can be inspected as has been received from the queue.

The handler won't attempt to remove the message from the queue. Therefore, such handling should be included into handler itself.

Code Samples

String connectionString = "DefaultEndpointsProtocol=https;AccountName={name};"
     + "AccountKey={key};EndpointSuffix={core.windows.net}";

 Consumer<QueueMessageDecodingError> processMessageDecodingErrorHandler =
     (queueMessageDecodingFailure) -> {
         QueueMessageItem queueMessageItem = queueMessageDecodingFailure.getQueueMessageItem();
         PeekedMessageItem peekedMessageItem = queueMessageDecodingFailure.getPeekedMessageItem();
         if (queueMessageItem != null) {
             System.out.printf("Received badly encoded message, messageId=%s, messageBody=%s",
                 queueMessageItem.getMessageId(),
                 queueMessageItem.getBody().toString());
             queueMessageDecodingFailure
                 .getQueueClient()
                 .deleteMessage(queueMessageItem.getMessageId(), queueMessageItem.getPopReceipt());
         } else if (peekedMessageItem != null) {
             System.out.printf("Peeked badly encoded message, messageId=%s, messageBody=%s",
                 peekedMessageItem.getMessageId(),
                 peekedMessageItem.getBody().toString());
         }
     };

 QueueClient client = new QueueClientBuilder()
     .connectionString(connectionString)
     .processMessageDecodingError(processMessageDecodingErrorHandler)
     .buildClient();

Parameters:

processMessageDecodingErrorHandler - the handler.

Returns:

the updated QueueClientBuilder object

processMessageDecodingErrorAsync

public QueueClientBuilder processMessageDecodingErrorAsync(Function> processMessageDecodingErrorAsyncHandler)

Sets the asynchronous handler that performs the tasks needed when a message is received or peaked from the queue but cannot be decoded.

Such message can be received or peaked when queue is expecting certain QueueMessageEncoding but there's another producer that is not encoding messages in expected way. I.e. the queue contains messages with different encoding.

QueueMessageDecodingError contains QueueAsyncClient for the queue that has received the message as well as getQueueMessageItem() or getPeekedMessageItem() with raw body, i.e. no decoding will be attempted so that body can be inspected as has been received from the queue.

The handler won't attempt to remove the message from the queue. Therefore, such handling should be included into handler itself.

Code Samples

String connectionString = "DefaultEndpointsProtocol=https;AccountName={name};"
     + "AccountKey={key};EndpointSuffix={core.windows.net}";

 Function<QueueMessageDecodingError, Mono<Void>> processMessageDecodingErrorHandler =
     (queueMessageDecodingFailure) -> {
         QueueMessageItem queueMessageItem = queueMessageDecodingFailure.getQueueMessageItem();
         PeekedMessageItem peekedMessageItem = queueMessageDecodingFailure.getPeekedMessageItem();
         if (queueMessageItem != null) {
             System.out.printf("Received badly encoded message, messageId=%s, messageBody=%s",
                 queueMessageItem.getMessageId(),
                 queueMessageItem.getBody().toString());
             return queueMessageDecodingFailure
                 .getQueueAsyncClient()
                 .deleteMessage(queueMessageItem.getMessageId(), queueMessageItem.getPopReceipt());
         } else if (peekedMessageItem != null) {
             System.out.printf("Peeked badly encoded message, messageId=%s, messageBody=%s",
                 peekedMessageItem.getMessageId(),
                 peekedMessageItem.getBody().toString());
             return Mono.empty();
         } else {
             return Mono.empty();
         }
     };

 QueueClient client = new QueueClientBuilder()
     .connectionString(connectionString)
     .processMessageDecodingErrorAsync(processMessageDecodingErrorHandler)
     .buildClient();

Parameters:

processMessageDecodingErrorAsyncHandler - the handler.

Returns:

the updated QueueClientBuilder object

queueName

public QueueClientBuilder queueName(String queueName)

Sets the name of the queue that the client will interact with.

Parameters:

queueName - Name of the queue

Returns:

the updated QueueClientBuilder object

retryOptions

public QueueClientBuilder retryOptions(RetryOptions retryOptions)

Sets the RetryOptions for all the requests made through the client.

Note: It is important to understand the precedence order of the HttpTrait APIs. In particular, if a HttpPipeline is specified, this takes precedence over all other APIs in the trait, and they will be ignored. If no HttpPipeline is specified, a HTTP pipeline will be constructed internally based on the settings provided to this trait. Additionally, there may be other APIs in types that implement this trait that are also ignored if an HttpPipeline is specified, so please be sure to refer to the documentation of types that implement this trait to understand the full set of implications.

Setting this is mutually exclusive with using retryOptions(RequestRetryOptions retryOptions). Consider using retryOptions(RequestRetryOptions retryOptions) to also set storage specific options.

Parameters:

retryOptions - The RetryOptions to use for all the requests made through the client.

Returns:

the updated QueueClientBuilder object

retryOptions

public QueueClientBuilder retryOptions(RequestRetryOptions retryOptions)

Sets the request retry options for all the requests made through the client. Setting this is mutually exclusive with using retryOptions(RetryOptions retryOptions).

Parameters:

retryOptions - RequestRetryOptions.

Returns:

the updated QueueClientBuilder object.

sasToken

public QueueClientBuilder sasToken(String sasToken)

Sets the SAS token used to authorize requests sent to the service.

Parameters:

sasToken - The SAS token to use for authenticating requests. This string should only be the query parameters (with or without a leading '?') and not a full url.

Returns:

the updated QueueClientBuilder

serviceVersion

public QueueClientBuilder serviceVersion(QueueServiceVersion version)

Sets the QueueServiceVersion that is used when making API requests.

If a service version is not provided, the service version that will be used will be the latest known service version based on the version of the client library being used. If no service version is specified, updating to a newer version of the client library will have the result of potentially moving to a newer service version.

Targeting a specific service version may also mean that the service will return an error for newer APIs.

Parameters:

version - QueueServiceVersion of the service to be used when making requests.

Returns:

the updated QueueClientBuilder object

Applies to