Skip to main content
Version: Config V1 (legacy)

OpenFeature Provider for Java

ConfigCat OpenFeature Provider for Java on GitHub

Getting started

1. Install the provider

build.gradle
dependencies {
implementation 'dev.openfeature.contrib.providers:configcat:0.1.+'
implementation 'dev.openfeature:sdk:1.+'
}

2. Initialize the provider

The ConfigCatProvider constructor takes a ConfigCatProviderConfig argument containing the configuration options for the ConfigCat Java SDK:

// Build options for the ConfigCat SDK.
ConfigCatProviderConfig configCatProviderConfig = ConfigCatProviderConfig.builder()
.sdkKey("#YOUR-SDK-KEY#")
.options(options -> {
options.pollingMode(PollingModes.autoPoll());
options.logLevel(LogLevel.WARNING);
// ...
})
.build();

// Configure the provider.
OpenFeatureAPI.getInstance().setProviderAndWait(new ConfigCatProvider(configCatProviderConfig));

// Create a client.
Client client = OpenFeatureAPI.getInstance().getClient();

For more information about all the configuration options, see the Java SDK documentation.

3. Evaluate your feature flag

boolean isAwesomeFeatureEnabled = client.getBooleanValue("isAwesomeFeatureEnabled", false);
if (isAwesomeFeatureEnabled)
{
doTheNewThing();
}
else
{
doTheOldThing();
}

4. Cleaning up

On application shutdown, clean up the OpenFeature provider and the underlying ConfigCat client.

OpenFeatureAPI.getInstance().shutdown();

Evaluation Context

An evaluation context in the OpenFeature specification is a container for arbitrary contextual data that can be used as a basis for feature flag evaluation. The ConfigCat provider translates these evaluation contexts to ConfigCat User Objects.

The following table shows how the different context attributes are mapped to User Object attributes.

Evaluation contextUser ObjectRequired
TargetingKeyidentifier
Emailemail
Countrycountry
Any othercustom

To evaluate feature flags for a context, use the OpenFeature Evaluation API:

MutableContext context = new MutableContext();
context.setTargetingKey("#SOME-USER-ID#");
context.add("Email", "[email protected]");
context.add("Country", "CountryID");
context.add("Rating", 4.5);

boolean isAwesomeFeatureEnabled = client.getBooleanValue("isAwesomeFeatureEnabled", false, context);

Look under the hood