Skip navigation links

Azure SDK for Java Reference Documentation

Current version is 1.2.0-beta.1, click here for the index

Azure App Configuration client library for Java

See: Description

Packages 
Package Description
com.azure.data.appconfiguration
Package containing classes for creating a ConfigurationAsyncClient to perform operations on Azure App Configuration service.
com.azure.data.appconfiguration.models
Package containing classes used for creating and selecting configuration settings in Azure Application Configuration service.
Current version is 1.2.0-beta.1, click here for the index

Azure App Configuration client library for Java

Azure App Configuration is a managed service that helps developers centralize their application configurations simply and securely.

Modern programs, especially programs running in a cloud, generally have many components that are distributed in nature. Spreading configuration settings across these components can lead to hard-to-troubleshoot errors during an application deployment. Use App Configuration to store all the settings for your application and secure their accesses in one place.

Use the client library for App Configuration to create and manage application configuration settings.

Source code | Package (Maven) | API reference documentation | Product documentation | Samples

Getting started

Prerequisites

Include the Package

<dependency>
  <groupId>com.azure</groupId>
  <artifactId>azure-data-appconfiguration</artifactId>
  <version>1.1.10</version>
</dependency>

Create an App Configuration Store

To create a Configuration Store you can use the Azure Portal or Azure CLI.

You need to install the Azure App Configuration CLI extension first by executing the following command:

az extension add -n appconfig

After that, create the Configuration Store:

az appconfig create --name <config-store-name> --resource-group <resource-group-name> --location eastus

Authenticate the client

In order to interact with the App Configuration service you'll need to create an instance of the Configuration Client class. To make this possible you'll need the connection string of the Configuration Store. Alternatively, use AAD token to connect to the service.

Use connection string

Get credentials

Use the Azure CLI snippet below to get the connection string from the Configuration Store.

az appconfig credential list --name <config-store-name>

Alternatively, get the connection string from the Azure Portal.

Create a Configuration Client

Once you have the value of the connection string you can create the configuration client:

ConfigurationClient configurationClient = new ConfigurationClientBuilder()
    .connectionString(connectionString)
    .buildClient();

or

ConfigurationAsyncClient configurationClient = new ConfigurationClientBuilder()
    .connectionString(connectionString)
    .buildAsyncClient();

Use AAD token

Here we demonstrate using DefaultAzureCredential to authenticate as a service principal. However, the configuration client accepts any azure-identity credential. See the azure-identity documentation for more information about other credentials.

Create a service principal (optional)

This Azure CLI snippet shows how to create a new service principal. Before using it, replace "your-application-name" with the appropriate name for your service principal.

Create a service principal:

az ad sp create-for-rbac --name http://my-application --skip-assignment

Output:

 {
     "appId": "generated app id",
     "displayName": "my-application",
     "name": "http://my-application",
     "password": "random password",
     "tenant": "tenant id"
 }

Use the output to set AZURECLIENTID ("appId" above), AZURECLIENTSECRET ("password" above) and AZURETENANTID ("tenant" above) environment variables. The following example shows a way to do this in Bash:

export AZURE_CLIENT_ID="generated app id"
export AZURE_CLIENT_SECRET="random password"
export AZURE_TENANT_ID="tenant id"

Assign one of the applicable App Configuration roles to the service principal.

Create a client

Once the AZURECLIENTID, AZURECLIENTSECRET and AZURETENANTID environment variables are set, DefaultAzureCredential will be able to authenticate the configuration client.

Constructing the client also requires your configuration store's URL, which you can get from the Azure CLI or the Azure Portal. In the Azure Portal, the URL can be found listed as the service "Endpoint".

DefaultAzureCredential credential = new DefaultAzureCredentialBuilder().build();
ConfigurationClient configurationClient = new ConfigurationClientBuilder()
    .credential(credential)
    .endpoint(endpoint)
    .buildClient();

Key concepts

Configuration Setting

A configuration setting is the fundamental resource within a configuration store. In its simplest form it is a key and a value. However, there are additional properties such as the modifiable content type and tags fields that allow the value to be interpreted or associated in different ways.

The Label property of a configuration setting provides a way to separate configuration settings into different dimensions. These dimensions are user defined and can take any form. Some common examples of dimensions to use for a label include regions, semantic versions, or environments. Many applications have a required set of configuration keys that have varying values as the application exists across different dimensions. For example, MaxRequests may be 100 in "NorthAmerica", and 200 in "WestEurope". By creating a configuration setting named MaxRequests with a label of "NorthAmerica" and another, only with a different value, in the "WestEurope" label, a solution can be achieved that allows the application to seamlessly retrieve Configuration Settings as it runs in these two dimensions.

Configuration Client

The client performs the interactions with the App Configuration service, getting, setting, deleting, and selecting configuration settings. An asynchronous, ConfigurationAsyncClient, and synchronous, ConfigurationClient, client exists in the SDK allowing for selection of a client based on an application's use case.

An application that needs to retrieve startup configurations is better suited using the synchronous client, for example setting up a SQL connection.

ConfigurationClient configurationClient = new ConfigurationClientBuilder()
    .connectionString(connectionString)
    .buildClient();

// urlLabel is optional
String url = configurationClient.getConfigurationSetting(urlKey, urlLabel).getValue();
Connection conn = null;
try {
    conn = DriverManager.getConnection(url);
} catch (SQLException ex) {
    System.out.printf("Failed to get connection using url %s", url);
} finally {
    if (conn != null) {
        try {
            conn.close();
        } catch (SQLException ex) {
            System.out.printf("Failed to close connection, url %s", url);
        }
    }
}

An application that has a large set of configurations that it needs to periodically update is be better suited using the asynchronous client, for example all settings with a specific label are periodically updated.

ConfigurationAsyncClient configurationClient = new ConfigurationClientBuilder()
    .connectionString(connectionString)
    .buildAsyncClient();

configurationClient.listConfigurationSettings(new SettingSelector().setLabelFilter(periodicUpdateLabel))
    .subscribe(setting -> updateConfiguration(setting));

Examples

The following sections provide several code snippets covering some of the most common configuration service tasks, including:

Create a Configuration Client

Create a configuration client by using ConfigurationClientBuilder by passing connection string.

ConfigurationClient configurationClient = new ConfigurationClientBuilder()
    .connectionString(connectionString)
    .buildClient();

Create a Configuration Setting

Create a configuration setting to be stored in the configuration store. There are two ways to store a configuration setting:

ConfigurationSetting setting = configurationClient.addConfigurationSetting("new_key", "new_label", "new_value");

Or

ConfigurationSetting setting = configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");

Retrieve a Configuration Setting

Retrieve a previously stored configuration setting by calling getConfigurationSetting.

ConfigurationSetting setting = configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");
ConfigurationSetting retrievedSetting = configurationClient.getConfigurationSetting("some_key", "some_label");

For conditional request, if you want to conditionally fetch a configuration setting, set ifChanged to true. When ifChanged is true, the configuration setting is only retrieved if it is different than the given setting. This is determined by comparing the ETag of the setting to the one in the service to see if they are the same or not. If the ETags are not the same, it means the configuration setting is different, and its value is retrieved.

ConfigurationSetting setting = configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");
Response<ConfigurationSetting> settingResponse = configurationClient.getConfigurationSettingWithResponse(setting, null, true, Context.NONE);

Update an existing Configuration Setting

Update an existing configuration setting by calling setConfigurationSetting.

ConfigurationSetting setting = configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");
ConfigurationSetting updatedSetting = configurationClient.setConfigurationSetting("some_key", "some_label", "new_value");

For conditional request, if you want to conditionally update a configuration setting, set the ifUnchanged parameter to true. When ifUnchanged is true, the configuration setting is only updated if it is same as the given setting. This is determined by comparing the ETag of the setting to the one in the service to see if they are the same or not. If the ETag are the same, it means the configuration setting is same, and its value is updated.

ConfigurationSetting setting = configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");
Response<ConfigurationSetting> settingResponse = configurationClient.setConfigurationSettingWithResponse(setting, true, Context.NONE);

Delete a Configuration Setting

Delete an existing configuration setting by calling deleteConfigurationSetting.

ConfigurationSetting setting = configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");
ConfigurationSetting deletedSetting = configurationClient.deleteConfigurationSetting("some_key", "some_label");

For conditional request, if you want to conditionally delete a configuration setting, set the ifUnchanged parameter to true. When ifUnchanged parameter to true. When ifUnchanged is true, the configuration setting is only deleted if it is same as the given setting. This is determined by comparing the ETag of the setting to the one in the service to see if they are the same or not. If the ETag are same, it means the configuration setting is same, and its value is deleted.

ConfigurationSetting setting = configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");
Response<ConfigurationSetting> settingResponse = configurationClient.deleteConfigurationSettingWithResponse(setting, true, Context.NONE);

List Configuration Settings with multiple keys

List multiple configuration settings by calling listConfigurationSettings. Pass a null SettingSelector into the method if you want to fetch all the configuration settings and their fields.

String key = "some_key";
String key2 = "new_key";
configurationClient.setConfigurationSetting(key, "some_label", "some_value");
configurationClient.setConfigurationSetting(key2, "new_label", "new_value");
SettingSelector selector = new SettingSelector().setKeyFilter(key + "," + key2);
PagedIterable<ConfigurationSetting> settings = configurationClient.listConfigurationSettings(selector);

List revisions of multiple Configuration Settings

List all revisions of a configuration setting by calling listRevisions.

String key = "revisionKey";
configurationClient.setConfigurationSetting(key, "some_label", "some_value");
configurationClient.setConfigurationSetting(key, "new_label", "new_value");
SettingSelector selector = new SettingSelector().setKeyFilter(key);
PagedIterable<ConfigurationSetting> settings = configurationClient.listRevisions(selector);

Set a Configuration Setting to read only

Set a configuration setting to read-only status.

configurationClient.setConfigurationSetting("some_key", "some_label", "some_value");
ConfigurationSetting setting = configurationClient.setReadOnly("some_key", "some_label", true);

Clear read only from a Configuration Setting

Clear read-only from a configuration setting.

ConfigurationSetting setting = configurationClient.setReadOnly("some_key", "some_label", false);

Create a client with Proxy Options

Create a configuration client with proxy options.

// Proxy options
final String hostname = "{your-host-name}";
final int port = 447; // your port number

ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP,
    new InetSocketAddress(hostname, port));
HttpClient httpClient = new NettyAsyncHttpClientBuilder()
    .proxy(proxyOptions)
    .build();
ConfigurationAsyncClient configurationAsyncClient = new ConfigurationClientBuilder()
    .connectionString("{your_connection_string}")
    .httpClient(httpClient)
    .buildAsyncClient();

Troubleshooting

General

When you interact with App Configuration using this Java client library, errors returned by the service correspond to the same HTTP status codes returned for REST API requests. For example, if you try to retrieve a configuration setting that doesn't exist in your configuration store, a 404 error is returned, indicating Not Found.

App Configuration provides a way to define customized headers through Context object in the public API.

// Add your headers
HttpHeaders headers = new HttpHeaders();
headers.put("my-header1", "my-header1-value");
headers.put("my-header2", "my-header2-value");
headers.put("my-header3", "my-header3-value");
// Call API by passing headers in Context.
configurationClient.addConfigurationSettingWithResponse(
    new ConfigurationSetting().setKey("key").setValue("value"),
    new Context(AddHeadersFromContextPolicy.AZURE_REQUEST_HTTP_HEADERS_KEY, headers));
// Above three HttpHeader will be added in outgoing HttpRequest.

For more detail information, check out the AddHeadersFromContextPolicy

Default HTTP Client

All client libraries by default use the Netty HTTP client. Adding the above dependency will automatically configure the client library to use the Netty HTTP client. Configuring or changing the HTTP client is detailed in the HTTP clients wiki.

Default SSL library

All client libraries, by default, use the Tomcat-native Boring SSL library to enable native-level performance for SSL operations. The Boring SSL library is an uber jar containing native libraries for Linux / macOS / Windows, and provides better performance compared to the default SSL implementation within the JDK. For more information, including how to reduce the dependency size, refer to the performance tuning section of the wiki.

Next steps

Contributing

This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution.

When you submit a pull request, a CLA-bot will automatically determine whether you need to provide a CLA and decorate the PR appropriately (e.g., label, comment). Simply follow the instructions provided by the bot. You will only need to do this once across all repos using our CLA.

This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.

Impressions

Skip navigation links
Visit the Azure for Java Developerssite for more Java documentation, including quick starts, tutorials, and code samples.

Copyright © 2021 Microsoft Corporation. All rights reserved.