OpenFeature provider for Java SDK
Read time: 3 minutes
Last edited: Aug 26, 2024
Overview
This topic documents how to get started with the LaunchDarkly OpenFeature provider for the Java SDK.
LaunchDarkly's OpenFeature providers are open source. In addition to this reference guide, we provide source, API reference documentation, and a sample application:
Resource | Location |
---|---|
OpenFeature Provider API documentation | Provider API docs |
GitHub repository | openfeature-java-server |
Sample application | Sample OpenFeature Java provider application |
Published module | Maven |
Get started
The LaunchDarkly OpenFeature provider for the Java SDK is intended for use in multi-user systems such as web servers and application. It is not intended for use in desktop and embedded systems applications.
Follow these instructions to start using the LaunchDarkly OpenFeature provider for the Java SDK in your application.
Version compatibility
The LaunchDarkly OpenFeature provider for the Java SDK is compatible with the OpenFeature Java SDK v1.x.
The provider is compatible with Java 11 and above.
Install the provider and dependencies
First, you need compatible versions of the LaunchDarkly Java SDK and the OpenFeature java-sdk:
implementation group: 'com.launchdarkly', name: 'launchdarkly-java-server-sdk', version: '[7.1.0, 8.0.0)'implementation 'dev.openfeature:sdk:[1.7.0,2.0.0)'
Then, add the LaunchDarkly provider for the Java SDK as a dependency in your application:
<dependency><groupId>com.launchdarkly</groupId><artifactId>launchdarkly-openfeature-serverprovider</artifactId><version>1.0.0</version></dependency>
Next, import the OpenFeature and LaunchDarkly namespaces in your application code:
import dev.openfeature.sdk.OpenFeatureAPI;import com.launchdarkly.sdk.server.LDClient;import com.launchdarkly.openfeature.serverprovider.Provider;
Initialize the provider
After you install and import the provider, create a single, shared instance of Provider
. Specify your SDK key here to authorize your application to connect to a particular environment within LaunchDarkly.
Here's how:
public class Main {public static void main(String[] args) {OpenFeatureAPI.getInstance().setProvider(new Provider("sdk-key-123abc"));Client client = OpenFeatureAPI.getInstance().getClient();}}
The LaunchDarkly Java provider uses an SDK key. Keys are specific to each project and environment. They are available from the Environments list for each project. To learn more about key types, read Keys.
Construct a context
A context is a generalized way of referring to the people, services, machines, or other resources that encounter feature flags in your product. The OpenFeature specification calls these evaluation contexts.
In the LaunchDarkly provider, contexts:
- always have a particular context kind. If you do not specify a kind, the provider treats the context as having a "user" kind. To specify a different kind, including a multi-context, you must include a
kind
attribute. - must have a targeting key. This is optional in the OpenFeature specification, but LaunchDarkly requires a key for evaluation. You can specify this using
targetingKey
, as in the OpenFeature specification, orkey
, which is the typical LaunchDarkly identifier for the targeting key.
Here are examples of a context:
EvaluationContext context = new ImmutableContext("user-key-123abc");
For additional examples, read OpenFeature specific considerations in the provider GitHub repository.
Evaluate a context
To evaluate feature flags for a context, use the OpenFeature Evaluation API. For example:
boolean value = client.getBooleanValue("flag-key-123abc", false, context);
Access the LaunchDarkly client
You may need access to the LDClient
from within the LaunchDarkly Java SDK if you are working on use cases not supported by OpenFeature, such as migration flags or sending custom events.
To access the LDClient
, use getLdClient()
:
LDClient ldClient = provider.getLdClient();