public class CosmosClientBuilder extends Object
CosmosAsyncClient
and CosmosClient CosmosClient
instances as logical representation of the Azure Cosmos database service.
When building client, endpoint() and key() are mandatory APIs, without these the initialization will fail.
Though consistencyLevel is not mandatory, but we strongly suggest to pay attention to this API when building client. By default, account consistency level is used if none is provided.
By default, direct connection mode is used if none specified.
Building Cosmos Async Client minimal APIs (without any customized configurations)
CosmosAsyncClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.buildAsyncClient();
Building Cosmos Async Client with customizations
CosmosAsyncClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.directMode(directConnectionConfig, gatewayConnectionConfig)
.consistencyLevel(ConsistencyLevel.SESSION)
.connectionSharingAcrossClientsEnabled(true)
.contentResponseOnWriteEnabled(true)
.userAgentSuffix("my-application1-client")
.preferredRegions(Collections.singletonList("West US", "East US"))
.buildAsyncClient();
Building Cosmos Sync Client minimal APIs (without any customized configurations)
CosmosClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.buildClient();
Building Cosmos Sync Client with customizations
CosmosClient client = new CosmosClientBuilder()
.endpoint(serviceEndpoint)
.key(key)
.directMode(directConnectionConfig, gatewayConnectionConfig)
.consistencyLevel(ConsistencyLevel.SESSION)
.connectionSharingAcrossClientsEnabled(true)
.contentResponseOnWriteEnabled(true)
.userAgentSuffix("my-application1-client")
.preferredRegions(Collections.singletonList("West US", "East US"))
.buildClient();
Constructor and Description |
---|
CosmosClientBuilder()
Instantiates a new Cosmos client builder.
|
Modifier and Type | Method and Description |
---|---|
CosmosAsyncClient |
buildAsyncClient()
Builds a cosmos async client with the provided properties
|
CosmosClient |
buildClient()
Builds a cosmos sync client with the provided properties
|
CosmosClientBuilder |
clientTelemetryEnabled(boolean clientTelemetryEnabled)
Sets the flag to enable client telemetry which will periodically collect
database operations aggregation statistics, system information like cpu/memory
and send it to cosmos monitoring service, which will be helpful during debugging.
|
CosmosClientBuilder |
connectionSharingAcrossClientsEnabled(boolean connectionSharingAcrossClientsEnabled)
Enables connections sharing across multiple Cosmos Clients.
|
CosmosClientBuilder |
consistencyLevel(ConsistencyLevel desiredConsistencyLevel)
Sets the
ConsistencyLevel to be used
By default, ConsistencyLevel.SESSION consistency will be used. |
CosmosClientBuilder |
contentResponseOnWriteEnabled(boolean contentResponseOnWriteEnabled)
Sets the boolean to only return the headers and status code in Cosmos DB response
in case of Create, Update and Delete operations on CosmosItem.
|
CosmosClientBuilder |
credential(com.azure.core.credential.AzureKeyCredential credential)
Sets the
AzureKeyCredential to be used |
CosmosClientBuilder |
credential(com.azure.core.credential.TokenCredential credential)
Sets the
TokenCredential used to authorize requests sent to the service. |
CosmosClientBuilder |
directMode()
Sets the default DIRECT connection configuration to be used.
|
CosmosClientBuilder |
directMode(DirectConnectionConfig directConnectionConfig)
Sets the DIRECT connection configuration to be used.
|
CosmosClientBuilder |
directMode(DirectConnectionConfig directConnectionConfig,
GatewayConnectionConfig gatewayConnectionConfig)
Sets the DIRECT connection configuration to be used.
|
CosmosClientBuilder |
endpoint(String endpoint)
Sets the Azure Cosmos DB endpoint the SDK will connect to
|
CosmosClientBuilder |
endpointDiscoveryEnabled(boolean endpointDiscoveryEnabled)
Sets the flag to enable endpoint discovery for geo-replicated database accounts.
|
CosmosClientBuilder |
gatewayMode()
Sets the default GATEWAY connection configuration to be used.
|
CosmosClientBuilder |
gatewayMode(GatewayConnectionConfig gatewayConnectionConfig)
Sets the GATEWAY connection configuration to be used.
|
CosmosClientBuilder |
key(String key)
Sets either a master or readonly key used to perform authentication
for accessing resource.
|
CosmosClientBuilder |
multipleWriteRegionsEnabled(boolean multipleWriteRegionsEnabled)
Sets the flag to enable writes on any regions for geo-replicated database accounts in the Azure
Cosmos DB service.
|
CosmosClientBuilder |
permissions(List<CosmosPermissionProperties> permissions)
Sets the permission list, which contains the
resource tokens needed to access resources.
|
CosmosClientBuilder |
preferredRegions(List<String> preferredRegions)
Sets the preferred regions for geo-replicated database accounts.
|
CosmosClientBuilder |
readRequestsFallbackEnabled(boolean readRequestsFallbackEnabled)
Sets whether to allow for reads to go to multiple regions configured on an account of Azure Cosmos DB service.
|
CosmosClientBuilder |
resourceToken(String resourceToken)
Sets a resource token used to perform authentication
for accessing resource.
|
CosmosClientBuilder |
sessionCapturingOverrideEnabled(boolean sessionCapturingOverrideEnabled)
Session capturing is enabled by default for
ConsistencyLevel.SESSION . |
CosmosClientBuilder |
throttlingRetryOptions(ThrottlingRetryOptions throttlingRetryOptions)
Sets the retry policy options associated with the DocumentClient instance.
|
CosmosClientBuilder |
userAgentSuffix(String userAgentSuffix)
sets the value of the user-agent suffix.
|
public CosmosClientBuilder()
public CosmosClientBuilder sessionCapturingOverrideEnabled(boolean sessionCapturingOverrideEnabled)
ConsistencyLevel.SESSION
.
For other consistency levels, it is not needed, unless if you need occasionally send requests with Session
Consistency while the client is not configured in session.
enabling Session capturing for Session mode has no effect.
sessionCapturingOverrideEnabled
- session capturing overridepublic CosmosClientBuilder connectionSharingAcrossClientsEnabled(boolean connectionSharingAcrossClientsEnabled)
CosmosAsyncClient client1 = new CosmosClientBuilder()
.endpoint(serviceEndpoint1)
.key(key1)
.consistencyLevel(ConsistencyLevel.SESSION)
.connectionSharingAcrossClientsEnabled(true)
.buildAsyncClient();
CosmosAsyncClient client2 = new CosmosClientBuilder()
.endpoint(serviceEndpoint2)
.key(key2)
.consistencyLevel(ConsistencyLevel.SESSION)
.connectionSharingAcrossClientsEnabled(true)
.buildAsyncClient();
// when configured this way client1 and client2 will share connections when possible.
When you have multiple instances of Cosmos Client in the same JVM interacting to multiple Cosmos accounts,
enabling this allows connection sharing in Direct mode if possible between instances of Cosmos Client.
Please note, when setting this option, the connection configuration (e.g., socket timeout config, idle timeout
config) of the first instantiated client will be used for all other client instances.connectionSharingAcrossClientsEnabled
- connection sharingpublic CosmosClientBuilder endpoint(String endpoint)
endpoint
- the service endpointpublic CosmosClientBuilder key(String key)
key
- master or readonly keypublic CosmosClientBuilder resourceToken(String resourceToken)
resourceToken
- resourceToken for authenticationpublic CosmosClientBuilder credential(com.azure.core.credential.TokenCredential credential)
TokenCredential
used to authorize requests sent to the service.credential
- TokenCredential
.NullPointerException
- If credential
is null
.public CosmosClientBuilder permissions(List<CosmosPermissionProperties> permissions)
permissions
- Permission list for authentication.public CosmosClientBuilder consistencyLevel(ConsistencyLevel desiredConsistencyLevel)
ConsistencyLevel
to be used
By default, ConsistencyLevel.SESSION
consistency will be used.desiredConsistencyLevel
- ConsistencyLevel
public CosmosClientBuilder credential(com.azure.core.credential.AzureKeyCredential credential)
AzureKeyCredential
to be usedcredential
- AzureKeyCredential
public CosmosClientBuilder contentResponseOnWriteEnabled(boolean contentResponseOnWriteEnabled)
contentResponseOnWriteEnabled
- a boolean indicating whether payload will be included in the response or notpublic CosmosClientBuilder gatewayMode()
public CosmosClientBuilder gatewayMode(GatewayConnectionConfig gatewayConnectionConfig)
gatewayConnectionConfig
- gateway connection configurationpublic CosmosClientBuilder directMode()
public CosmosClientBuilder directMode(DirectConnectionConfig directConnectionConfig)
directConnectionConfig
- direct connection configurationpublic CosmosClientBuilder directMode(DirectConnectionConfig directConnectionConfig, GatewayConnectionConfig gatewayConnectionConfig)
directConnectionConfig
- direct connection configuration to be usedgatewayConnectionConfig
- gateway connection configuration to be usedpublic CosmosClientBuilder userAgentSuffix(String userAgentSuffix)
userAgentSuffix
- The value to be appended to the user-agent header, this is
used for monitoring purposes.public CosmosClientBuilder throttlingRetryOptions(ThrottlingRetryOptions throttlingRetryOptions)
Properties in the RetryOptions class allow application to customize the built-in retry policies. This property is optional. When it's not set, the SDK uses the default values for configuring the retry policies. See RetryOptions class for more details.
throttlingRetryOptions
- the RetryOptions instance.IllegalArgumentException
- thrown if an error occurspublic CosmosClientBuilder preferredRegions(List<String> preferredRegions)
When EnableEndpointDiscovery is true and PreferredRegions is non-empty, the SDK will prefer to use the regions in the container in the order they are specified to perform operations.
If EnableEndpointDiscovery is set to false, this property is ignored.
preferredRegions
- the list of preferred regions.public CosmosClientBuilder endpointDiscoveryEnabled(boolean endpointDiscoveryEnabled)
When EnableEndpointDiscovery is true, the SDK will automatically discover the current write and read regions to ensure requests are sent to the correct region based on the capability of the region and the user's preference.
The default value for this property is true indicating endpoint discovery is enabled.
endpointDiscoveryEnabled
- true if EndpointDiscovery is enabled.public CosmosClientBuilder multipleWriteRegionsEnabled(boolean multipleWriteRegionsEnabled)
When the value of this property is true, the SDK will direct write operations to available writable regions of geo-replicated database account. Writable regions are ordered by PreferredRegions property. Setting the property value to true has no effect until EnableMultipleWriteRegions in DatabaseAccount is also set to true.
DEFAULT value is true indicating that writes are directed to available writable regions of geo-replicated database account.
multipleWriteRegionsEnabled
- flag to enable writes on any regions for geo-replicated
database accounts.public CosmosClientBuilder clientTelemetryEnabled(boolean clientTelemetryEnabled)
DEFAULT value is false indicating this is opt in feature, by default no telemetry collection.
clientTelemetryEnabled
- flag to enable client telemetry.public CosmosClientBuilder readRequestsFallbackEnabled(boolean readRequestsFallbackEnabled)
DEFAULT value is true.
If this property is not set, the default is true for all Consistency Levels other than Bounded Staleness,
The default is false for Bounded Staleness.
1. CosmosClientBuilder.endpointDiscoveryEnabled
is true
2. the Azure Cosmos DB account has more than one region
readRequestsFallbackEnabled
- flag to enable reads to go to multiple regions configured on an account of
Azure Cosmos DB service.public CosmosAsyncClient buildAsyncClient()
public CosmosClient buildClient()
Copyright © 2021 Microsoft Corporation. All rights reserved.