Using the Java SDK
Providing User Data
Many calls to the AppFlags client require passing an AppFlagsUser
, which is needed to evaluate Targeting Rules for Feature Flags.
The AppFlagsUser
object contains a key
, which is a unique identifier for the user.
AppFlagsUser user = AppFlagsUser.builder()
.key("USER_KEY")
.build();
If you are not evaluating feature flags for a particular user, you can pass the SYSTEM
user instead:
AppFlagsUser user = AppFlagsUser.SYSTEM;
Getting the value for a Feature Flag
To retrieve the active value for a particular Feature Flag, use the relevant getVariation
function.
For example for a boolean
flag:
boolean booleanValue = appFlagsClient.getBooleanVariation("flag_key", user, false);
Default values
The third parameter is a default value. This value will be returned if the flag does not exist or if the AppFlags client is unable to connect to the AppFlags servers. You can set the default value to null
if you choose. For example:
// boolean default
boolean booleanValue = appFlagsClient.getBooleanVariation("flag_key", user, false);
// null default
Boolean booleanOrNull = appFlagsClient.getBooleanVariation("flag_key", user, null);
Each variation type
There are getVariation
functions for boolean
, number
, and string
Feature Flags:
boolean booleanFlag = appFlagsClient.getBooleanVariation("boolean_flag", user, false);
double numberFlag = appFlagsClient.getNumberVariation("number_flag", user, 0.0);
String stringFlag = appFlagsClient.getStringVariation("string_flag", user, "defaultValue");
Get Notified When Feature Flags Update
The AppFlags client will automatically update whenever your Feature Flags change.
To get notified when Feature Flags update, your application can be notified with a ConfigurationChangedHandler
:
appFlagsClient.addConfigurationChangedHandler(() -> {
// Feature flags have been updated
});
Example Repository
For a full example of using AppFlags in a Java application, see https://github.com/AppFlags/example-java.