Skip to main content

OpenFeature Java SDK

SpecificationRelease
JavadocMaven CentralCodecovCII Best Practices

Quick start​

MCP Install

Follow the MCP Getting Started guide to quickly set up the OpenFeature MCP server and connect your AI tool.

  • Run this prompt: "Install OpenFeature into this app"

Quick Install:

claude mcp add --transport stdio openfeature npx -y @openfeature/mcp

Requirements​

  • Java 11+ (compiler target is 11)

Note that this library is intended to be used in server-side contexts and has not been evaluated for use on mobile devices.

Install​

Maven​

<dependency>
<groupId>dev.openfeature</groupId>
<artifactId>sdk</artifactId>
<version>1.23.0</version>
</dependency>

If you would like snapshot builds, this is the relevant repository information:

<repositories>
<repository>
<snapshots>
<enabled>true</enabled>
</snapshots>
<id>sonatype</id>
<name>Sonatype Repository</name>
<url>https://s01.oss.sonatype.org/content/repositories/snapshots/</url>
</repository>
</repositories>

Gradle​

dependencies {
implementation 'dev.openfeature:sdk:1.23.0'
}

Usage​

public void example(){

// flags defined in memory
Map<String, Flag<?>> myFlags = new HashMap<>();
myFlags.put("v2_enabled", Flag.builder()
.variant("on", true)
.variant("off", false)
.defaultVariant("on")
.build());

// configure a provider
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
try {
api.setProviderAndWait(new InMemoryProvider(myFlags));
} catch (Exception e) {
// handle initialization failure
e.printStackTrace();
}

// create a client
Client client = api.getClient();

// get a bool flag value
boolean flagValue = client.getBooleanValue("v2_enabled", false);
}

API Reference​

See here for the Javadocs.

Features​

StatusFeaturesDescription
✅ProvidersIntegrate with a commercial, open source, or in-house feature management tool.
✅TargetingContextually-aware flag evaluation using evaluation context.
✅Multi-provider (experimental)Combine multiple providers and delegate evaluations according to a strategy.
✅HooksAdd functionality to various stages of the flag evaluation life-cycle.
✅TrackingAssociate user actions with feature flag evaluations.
✅LoggingIntegrate with popular logging packages.
✅DomainsLogically bind clients with providers.
✅EventingReact to state changes in the provider or flag management system.
✅ShutdownGracefully clean up a provider during application shutdown.
✅Transaction Context PropagationSet a specific evaluation context for a transaction (e.g. an HTTP request or a thread).
✅ExtendingExtend OpenFeature with custom providers and hooks.
Implemented: ✅ | In-progress: ⚠️ | Not implemented yet: ❌

Providers​

Providers are an abstraction between a flag management system and the OpenFeature SDK. Look here for a complete list of available providers. If the provider you're looking for hasn't been created yet, see the develop a provider section to learn how to build it yourself.

Once you've added a provider as a dependency, it can be registered with OpenFeature like this:

In some situations, it may be beneficial to register multiple providers in the same application. This is possible using domains, which is covered in more detail below.

Multi-provider (experimental)​

In addition to domains, you may want to delegate flag evaluation across multiple providers using a configurable strategy. The multi-provider allows you to compose several FeatureProvider implementations and determine which provider's result to use.

Experimental: This API is experimental and may change in future releases.

import dev.openfeature.sdk.OpenFeatureAPI;
import dev.openfeature.sdk.Client;
import dev.openfeature.sdk.FeatureProvider;
import dev.openfeature.sdk.multiprovider.MultiProvider;

import java.util.List;

public void multiProviderExample() throws Exception {
FeatureProvider primaryProvider = new MyPrimaryProvider();
FeatureProvider fallbackProvider = new MyFallbackProvider();

MultiProvider multiProvider = new MultiProvider(List.of(primaryProvider, fallbackProvider));

OpenFeatureAPI api = OpenFeatureAPI.getInstance();
api.setProviderAndWait(multiProvider);

Client client = api.getClient();
boolean value = client.getBooleanValue("some-flag", false);
}

Synchronous​

To register a provider in a blocking manner to ensure it is ready before further actions are taken, you can use the setProviderAndWait method as shown below:

OpenFeatureAPI api = OpenFeatureAPI.getInstance();
try {
api.setProviderAndWait(new MyProvider());
} catch (Exception e) {
// handle initialization failure
e.printStackTrace();
}

Asynchronous​

To register a provider in a non-blocking manner, you can use the setProvider method as shown below:

OpenFeatureAPI.getInstance().setProvider(new MyProvider());

In some situations, it may be beneficial to register multiple providers in the same application. This is possible using domains, which is covered in more detail below.

Targeting​

Sometimes, the value of a flag must consider some dynamic criteria about the application or user, such as the user's location, IP, email address, or the server's location. In OpenFeature, we refer to this as targeting. If the flag management system you're using supports targeting, you can provide the input data using the evaluation context.

// set a value to the global context
OpenFeatureAPI api = OpenFeatureAPI.getInstance();
Map<String, Value> apiAttrs = new HashMap<>();
apiAttrs.put("region", new Value(System.getenv("AWS_REGION")));
EvaluationContext apiCtx = new ImmutableContext(apiAttrs);
api.setEvaluationContext(apiCtx);

// set a value to the client context
Map<String, Value> clientAttrs = new HashMap<>();
clientAttrs.put("region", new Value(System.getenv("AWS_REGION")));
EvaluationContext clientCtx = new ImmutableContext(clientAttrs);
Client client = api.getClient();
client.setEvaluationContext(clientCtx);

// set a value to the invocation context
Map<String, Value> requestAttrs = new HashMap<>();
requestAttrs.put("email", new Value(session.getAttribute("email")));
requestAttrs.put("product", new Value("productId"));
String targetingKey = session.getId();
EvaluationContext reqCtx = new ImmutableContext(targetingKey, requestAttrs);