Skip to main content

Java SDK reference

Star on GitHub Java CI Maven Central Coverage Status Javadocs

Getting Started

1. Add the ConfigCat SDK to your project

Maven:

<dependency>
<groupId>com.configcat</groupId>
<artifactId>configcat-java-client</artifactId>
<version>[7.0.0,)</version>
</dependency>

Gradle:

compile group: 'com.configcat', name: 'configcat-java-client', version: '7.+'

2. Import the ConfigCat SDK

import com.configcat.*;

3. Create the ConfigCat client with your SDK Key

ConfigCatClient client = new ConfigCatClient("<PLACE-YOUR-SDK-KEY-HERE>");

4. Get your setting value

boolean isMyAwesomeFeatureEnabled = client.getValue(Boolean.class, "<key-of-my-awesome-feature>", false);
if(isMyAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}

5. Stop ConfigCat client

You can safely shut down the client instance and release all associated resources on application exit.

client.close()

Java compatibility

The ConfigCat Java SDK is compatible with Java 8 and higher.

Creating the ConfigCat Client

ConfigCat Client is responsible for:

  • managing the communication between your application and ConfigCat servers.
  • caching your setting values and feature flags.
  • serving values quickly in a failsafe way.

new ConfigCatClient(<sdkKey>) returns a client with default options.

Builder

ConfigCatClient client = ConfigCatClient.newBuilder()
.mode(PollingModes.autoPoll(60))
.build(<sdkkey>);
Builder optionsDescription
build(<sdkkey>)REQUIRED. Waits for the SDK Key to access your feature flags and settings. Get it from ConfigCat Dashboard.
dataGovernance(DataGovernance)Optional, defaults to Global. Describes the location of your feature flag and setting data within the ConfigCat CDN. This parameter needs to be in sync with your Data Governance preferences. More about Data Governance. Available options: Global, EuOnly.
baseUrl(string)Obsolete Optional, sets the CDN base url (forward proxy, dedicated subscription) from where the sdk will download the configurations.
httpClient(OkHttpClient)Optional, sets the underlying OkHttpClient used to download the feature flags and settings over HTTP. More about the HTTP Client.
cache(ConfigCache)Optional, sets a custom cache implementation for the client. More about cache.
mode(PollingMode)Optional, sets the polling mode for the client. More about polling modes.
logLevel(LogLevel)Optional, defaults to WARNING. Sets the internal log level. More about logging.
flagOverrides(OverrideDataSourceBuilder, OverrideBehaviour)Optional, configures local feature flag & setting overrides. More about feature flag overrides.
caution

We strongly recommend you to use the ConfigCatClient as a Singleton object in your application. If you want to use multiple SDK Keys in the same application, create only one ConfigCatClient per SDK Key.

Anatomy of getValue()

ParametersDescription
classOfTREQUIRED. The type of the setting.
keyREQUIRED. Setting-specific key. Set on ConfigCat Dashboard for each setting.
userOptional, User Object. Essential when using Targeting. Read more about Targeting.
defaultValueREQUIRED. This value will be returned in case of an error.
boolean value = client.getValue(
Boolean.class, // Setting type
"keyOfMySetting", // Setting Key
User.newBuilder().build("435170f4-8a8b-4b67-a723-505ac7cdea92"), // Optional User Object
false // Default value
);

Anatomy of getValueAsync()

ParametersDescription
classOfTREQUIRED. The type of the setting.
keyREQUIRED. Setting-specific key. Set on ConfigCat Dashboard for each setting.
userOptional, User Object. Essential when using Targeting. Read more about Targeting.
defaultValueREQUIRED. This value will be returned in case of an error.
client.getValueAsync(
Boolean.class, // Setting type
"keyOfMySetting", // Setting Key
User.newBuilder().build("435170f4-8a8b-4b67-a723-505ac7cdea92"), // Optional User Object
false // Default value
).thenAccept(isMyAwesomeFeatureEnabled -> {
if(isMyAwesomeFeatureEnabled) {
doTheNewThing();
} else {
doTheOldThing();
}
});

User Object

The User Object is essential if you'd like to use ConfigCat's Targeting feature.

User user = User.newBuilder().build("435170f4-8a8b-4b67-a723-505ac7cdea92");   
User user = User.newBuilder().build("[email protected]");   
Builder optionsDescription
identifier()REQUIRED. Unique identifier of a user in your application. Can be any value, even an email address.
email()Optional parameter for easier targeting rule definitions.
country()Optional parameter for easier targeting rule definitions.
custom()Optional dictionary for custom attributes of a user for advanced targeting rule definitions. e.g. User role, Subscription type.
java.util.Map<String,String> customAttributes = new java.util.HashMap<String,String>();
customAttributes.put("SubscriptionType", "Pro");
customAttributes.put("UserRole", "Admin");

User user = User.newBuilder()
.email("[email protected]")
.country("United Kingdom")
.custom(customAttributes)
.build("435170f4-8a8b-4b67-a723-505ac7cdea92");

Polling Modes

The ConfigCat SDK supports 3 different polling mechanisms to acquire the setting values from ConfigCat. After latest setting values are downloaded, they are stored in the internal cache then all getValue() calls are served from there. With the following polling modes, you can customize the SDK to best fit to your application's lifecycle.
More about polling modes.

Auto polling (default)

The ConfigCat SDK downloads the latest values and stores them automatically every 60 seconds.

Use the the autoPollIntervalInSeconds option parameter of the PollingModes.autoPoll() to change the polling interval.

ConfigCatClient client = ConfigCatClient.newBuilder()
.mode(PollingModes.autoPoll(60 /* polling interval in seconds */))
.build("<PLACE-YOUR-SDK-KEY-HERE>");

Adding a callback to configurationChangeListener option parameter will get you notified about changes.

ConfigCatClient client = ConfigCatClient.newBuilder()
.mode(PollingModes.autoPoll(
60 /* polling interval in seconds */,
() -> {
// here you can subscribe to configuration changes
})
)
.build("<PLACE-YOUR-SDK-KEY-HERE>");

Available options:

Option ParameterDescriptionDefault
autoPollIntervalInSecondsPolling interval.60
maxInitWaitTimeSecondsMaximum waiting time between the client initialization and the first config acquisition in seconds.5
configurationChangeListenerCallback to get notified about changes.-

Lazy Loading

When calling getValue() the ConfigCat SDK downloads the latest setting values if they are not present or expired in the cache. In this case the getValue() will return the setting value after the cache is updated.

Use the cacheRefreshIntervalInSeconds option parameter of the PollingModes.lazyLoad() to set cache lifetime.

ConfigCatClient client = ConfigCatClient.newBuilder()
.mode(PollingModes.lazyLoad(60 /* the cache will expire in 120 seconds */))
.build("<PLACE-YOUR-SDK-KEY-HERE>");

Use the asyncRefresh option parameter of the PollingModes.lazyLoad() to define how do you want to handle the expiration of the cached configuration. If you choose asynchronous refresh then when a getValue() calls is made while the cache is expired, the previous value will be returned immediately until the downloading of the new configuration is completed.

ConfigCatClient client = ConfigCatClient.newBuilder()
.mode(PollingModes.lazyLoad(
120, // the cache will expire in 120 seconds
true // the refresh will be executed asynchronously
)
)
.build("<PLACE-YOUR-SDK-KEY-HERE>");

If you set the asyncRefresh to false, the refresh operation will be awaited until the downloading of the new configuration is completed.

Available options:

Option ParameterDescriptionDefault
cacheRefreshIntervalInSecondsCache TTL.60
useAsyncRefreshAsynchronous refresh.false

Manual Polling

Manual polling gives you full control over when the config.json (with the setting values) is downloaded. ConfigCat SDK will not update them automatically. Calling forceRefresh() is your application's responsibility.

ConfigCatClient client = ConfigCatClient.newBuilder()
.mode(PollingModes.manualPoll())
.build("#YOUR-SDK-KEY#");

client.forceRefresh();

getValue() returns defaultValue if the cache is empty. Call forceRefresh() to update the cache.

Flag Overrides

With flag overrides you can overwrite the feature flags & settings downloaded from the ConfigCat CDN with local values. Moreover, you can specify how the overrides should apply over the downloaded values. The following 3 behaviours are supported:

  • Local/Offline mode (OverrideBehaviour.LOCAL_ONLY): When evaluating values, the SDK will not use feature flags & settings from the ConfigCat CDN, but it will use all feature flags & settings that are loaded from local-override sources.

  • Local over remote (OverrideBehaviour.LOCAL_OVER_REMOTE): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the local-override version will take precedence.

  • Remote over local (OverrideBehaviour.REMOTE_OVER_LOCAL): When evaluating values, the SDK will use all feature flags & settings that are downloaded from the ConfigCat CDN, plus all feature flags & settings that are loaded from local-override sources. If a feature flag or a setting is defined both in the downloaded and the local-override source then the downloaded version will take precedence.

You can load your feature flag & setting overrides from a file or from a simple Map<String, Object> structure.

JSON File

The SDK can be configured to load your feature flag & setting overrides from a file or classpath resource. You can also specify whether the file should be reloaded when it gets modified.

File

ConfigCatClient client = ConfigCatClient.newBuilder()
flagOverrides(OverrideDataSourceBuilder.localFile(
"path/to/the/local_flags.json", // path to the file
true // reload the file when it gets modified
),
OverrideBehaviour.LOCAL_ONLY // local/offline mode
)
.build("localhost");

Classpath Resource

ConfigCatClient client = ConfigCatClient.newBuilder()
flagOverrides(OverrideDataSourceBuilder.classPathResource(
"local_flags.json", // name of the resource
true // reload the resource when it gets modified
),
OverrideBehaviour.LOCAL_ONLY // local/offline mode
)
.build("localhost");

JSON File Structure

The SDK supports 2 types of JSON structures to describe feature flags & settings.

1. Simple (key-value) structure
{
"flags": {
"enabledFeature": true,
"disabledFeature": false,
"intSetting": 5,
"doubleSetting": 3.14,
"stringSetting": "test"
}
}

This is the same format that the SDK downloads from the ConfigCat CDN. It allows the usage of all features you can do on the ConfigCat Dashboard.

You can download your current config.json from ConfigCat's CDN and use it as a baseline.

The URL to your current config.json is based on your Data Governance settings:

  • GLOBAL: https://cdn-global.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v5.json
  • EU: https://cdn-eu.configcat.com/configuration-files/{YOUR-SDK-KEY}/config_v5.json
{
"f": { // list of feature flags & settings
"isFeatureEnabled": { // key of a particular flag
"v": false, // default value, served when no rules are defined
"i": "430bded3", // variation id (for analytical purposes)
"t": 0, // feature flag's type, possible values:
// 0 -> BOOLEAN
// 1 -> STRING
// 2 -> INT
// 3 -> DOUBLE
"p": [ // list of percentage rules
{
"o": 0, // rule's order
"v": true, // value served when the rule is selected during evaluation
"p": 10, // % value
"i": "bcfb84a7" // variation id (for analytical purposes)
},
{
"o": 1, // rule's order
"v": false, // value served when the rule is selected during evaluation
"p": 90, // % value
"i": "bddac6ae" // variation id (for analytical purposes)
}
],
"r": [ // list of targeting rules
{
"o": 0, // rule's order
"a": "Identifier", // comparison attribute
"t": 2, // comparator, possible values:
// 0 -> 'IS ONE OF',
// 1 -> 'IS NOT ONE OF',
// 2 -> 'CONTAINS',
// 3 -> 'DOES NOT CONTAIN',
// 4 -> 'IS ONE OF (SemVer)',
// 5 -> 'IS NOT ONE OF (SemVer)',
// 6 -> '< (SemVer)',
// 7 -> '<= (SemVer)',
// 8 -> '> (SemVer)',
// 9 -> '>= (SemVer)',
// 10 -> '= (Number)',
// 11 -> '<> (Number)',
// 12 -> '< (Number)',
// 13 -> '<= (Number)',
// 14 -> '> (Number)',
// 15 -> '>= (Number)',
// 16 -> 'IS ONE OF (Sensitive)',
// 17 -> 'IS NOT ONE OF (Sensitive)'
"c": "@example.com", // comparison value
"v": true, // value served when the rule is selected during evaluation
"i": "bcfb84a7" // variation id (for analytical purposes)
}
]
},
}
}

Map

You can set up the SDK to load your feature flag & setting overrides from a Map<String, Object>.

Map<String, Object> map = new HashMap<>();
map.put("enabledFeature", true);
map.put("disabledFeature", false);
map.put("intSetting", 5);
map.put("doubleSetting", 3.14);
map.put("stringSetting", "test");

ConfigCatClient client = ConfigCatClient.newBuilder()
.flagOverrides(OverrideDataSourceBuilder.map(map), OverrideBehaviour.LOCAL_ONLY)
.build("localhost");

getAllKeys(), getAllKeysAsync()

You can query the keys of each feature flag and setting with the getAllKeys() or getAllKeysAsync() method.

ConfigCatClient client = new ConfigCatClient("#YOUR-SDK-KEY#");
java.util.Collection<String> keys = client.getAllKeys();
ConfigCatClient client = new ConfigCatClient("#YOUR-SDK-KEY#");
client.getAllKeysAsync().thenAccept(keys -> {
});

getAllValues(), getAllValuesAsync()

Evaluates and returns the values of all feature flags and settings. Passing a User Object is optional.

ConfigCatClient client = new ConfigCatClient("#YOUR-SDK-KEY#");
Map<String, Object> settingValues = client.getAllValues();

// invoke with user object
User user = User.newBuilder().build("435170f4-8a8b-4b67-a723-505ac7cdea92")
Map<String, Object> settingValuesTargeting = client.getAllValues(user);
ConfigCatClient client = new ConfigCatClient("#YOUR-SDK-KEY#");
client.getAllValuesAsync().thenAccept(settingValues -> { });

// invoke with user object
User user = User.newBuilder().build("435170f4-8a8b-4b67-a723-505ac7cdea92")
client.getAllValuesAsync(user).thenAccept(settingValuesTargeting -> { });

Custom Cache

You have the option to inject your custom cache implementation into the client. All you have to do is to inherit from the ConfigCache abstract class:

public class MyCustomCache extends ConfigCache {

@Override
public String read(String key) {
// here you have to return with the cached value
}

@Override
public void write(String key, String value) {
// here you have to store the new value in the cache
}
}

Then use your custom cache implementation:

ConfigCatClient client = ConfigCatClient.newBuilder()
.cache(new MyCustomCache()) // inject your custom cache
.build("<PLACE-YOUR-SDK-KEY-HERE>");

HttpClient

The ConfigCat SDK internally uses an OkHttpClient instance to download the latest configuration over HTTP. You have the option to override the internal Http client with your customized one.

HTTP Proxy

If your application runs behind a proxy you can do the following:

import java.net.InetSocketAddress;
import java.net.Proxy;

Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("proxyHost", proxyPort));

ConfigCatClient client = ConfigCatClient.newBuilder()
.httpClient(new OkHttpClient.Builder()
.proxy(proxy)
.build())
.build("<PLACE-YOUR-SDK-KEY-HERE>");

HTTP Timeout

You can set the maximum wait time for a ConfigCat HTTP response by using OkHttpClient's timeouts.

ConfigCatClient client = ConfigCatClient.newBuilder()
.httpClient(new OkHttpClient.Builder()
.readTimeout(2, TimeUnit.SECONDS) // set the read timeout to 2 seconds
.build())
.build("<PLACE-YOUR-SDK-KEY-HERE>");

OkHttpClient's default timeout is 10 seconds.

As the ConfigCatClient SDK maintains the whole lifetime of the internal http client, it's being closed simultaneously with the ConfigCatClient, refrain from closing the http client manually.

Force refresh

Any time you want to refresh the cached configuration with the latest one, you can call the forceRefresh() method of the library, which initiates a new download and updates the local cache.

Logging

As the SDK uses the facade of slf4j for logging, so you can use any of the slf4j implementation packages.

dependencies {
compile group: 'org.slf4j', name: 'slf4j-simple', version: '1.+'
}

You can change the verbosity of the logs by passing a LogLevel parameter to the ConfigCatClientBuilder's logLevel function.

ConfigCatClient client = ConfigCatClient.newBuilder()
.logLevel(LogLevel.INFO)
.build("<PLACE-YOUR-SDK-KEY-HERE>");

Available log levels:

LevelDescription
NO_LOGTurn the logging off.
ERROROnly error level events are logged.
WARNINGDefault. Errors and Warnings are logged.
INFOErrors, Warnings and feature flag evaluation is logged.
DEBUGAll of the above plus debug info is logged. Debug logs can be different for other SDKs.

Info level logging helps to inspect how a feature flag was evaluated:

INFO com.configcat.ConfigCatClient - Evaluating getValue(isPOCFeatureEnabled).
User object: User{Identifier=435170f4-8a8b-4b67-a723-505ac7cdea92, Email=[email protected]}
Evaluating rule: [Email:[email protected]] [CONTAINS] [@something.com] => no match
Evaluating rule: [Email:[email protected]] [CONTAINS] [@example.com] => match, returning "true"

Sample Apps

Check out our Sample Applications how they use the ConfigCat SDK

Look Under the Hood