Implement Feature Flags (Java)
The Java feature flags implementation is currently in Beta.
Overview
This developer guide will assist you in configuring your server-side Java platform for Feature Flags using the Mixpanel Java SDK. Feature Flags allow you to control the rollout of your features, conduct A/B testing, and manage application behavior without deploying new code.
Prerequisites
Before implementing Feature Flags, ensure:
- You are on an Enterprise subscription plan and have the appropriate version of the SDK installed (minimum supported version is
1.6.0-flags). If not, please follow this doc to install the SDK. - You have your Project Token from your Mixpanel Project Settings
Flag Evaluation Modes
There are two modes available for using the Java SDK for feature flagging: Local Evaluation and Remote Evaluation.
For local evaluation, the SDK will poll Mixpanel servers for feature flag configurations. Assignment of user contexts to variants will be done locally within the SDK. This mode is recommended for low latency since there is no network call made at assignment time.
For remote evaluation, the SDK will make a network call to Mixpanel servers at assignment time. This mode is recommended for use cases where you want to leverage Mixpanel cohorts for user targeting or sticky variants for persistent variant assignments.
Local Evaluation
Targeting by Mixpanel cohorts and sticky variants are not supported in Local Evaluation mode.
-
The SDK is configured with a
LocalFlagsConfigobject that specifies parameters:apiHost- If your project is in the EU/IN region, this should be set to route toapi-eu.mixpanel.com/api-in.mixpanel.comrespectively.enablePolling- This should be set totrueto enable local evaluation.pollingIntervalSeconds- This is the interval in seconds at which the SDK will poll Mixpanel servers for feature flag configurations.
-
The SDK will continue to poll for the lifetime of the SDK instance or until stopped.
import com.mixpanel.mixpanelapi.featureflags.MixpanelFlagsClient;
import com.mixpanel.mixpanelapi.featureflags.config.LocalFlagsConfig;
import java.util.HashMap;
import java.util.Map;
// Configure local flags
LocalFlagsConfig localConfig = LocalFlagsConfig.builder()
.projectToken("YOUR_PROJECT_TOKEN")
.apiHost("https://api.mixpanel.com")
.enablePolling(true)
.pollingIntervalSeconds(60)
.requestTimeoutSeconds(10)
.build();
// Initialize the flags client
MixpanelFlagsClient flagsClient = new MixpanelFlagsClient(localConfig);
// Start polling for flag definitions
flagsClient.getLocalFlags().startPollingForDefinitions();
// This should be the 'key' of the feature flag from Mixpanel's UX.
String flagKey = "sample-flag";
// This is the fallback variant to return if the user context is not in a rollout group for the flag.
String fallbackVariant = "control";
// Current user context for evaluation.
// At minimum, this needs to include the user's distinct_id.
// If any of your feature flags use a Variant Assignment Key other than 'distinct_id', this should also include those keys for evaluation. For example, 'company_id' below
// If any of your feature flags use Runtime targeting, this should also include 'custom_properties' for evaluation
Map<String, Object> userContext = new HashMap<>();
userContext.put("distinct_id", "1234");
userContext.put("company_id", "X");
Map<String, Object> customProperties = new HashMap<>();
customProperties.put("platform", "java");
userContext.put("custom_properties", customProperties);
// Get variant value for the flag
String variantValue = flagsClient.getLocalFlags().getVariantValue(flagKey, fallbackVariant, userContext);
// Check if flag is enabled (boolean check)
boolean isEnabled = flagsClient.getLocalFlags().isEnabled(flagKey, userContext);Remote Evaluation
- The SDK is configured with a
RemoteFlagsConfigobject to use remote evaluation.
import com.mixpanel.mixpanelapi.featureflags.MixpanelFlagsClient;
import com.mixpanel.mixpanelapi.featureflags.config.RemoteFlagsConfig;
import java.util.HashMap;
import java.util.Map;
// Configure remote flags
RemoteFlagsConfig remoteConfig = RemoteFlagsConfig.builder()
.projectToken("YOUR_PROJECT_TOKEN")
.apiHost("https://api.mixpanel.com")
.requestTimeoutSeconds(5)
.build();
// Initialize the flags client
MixpanelFlagsClient flagsClient = new MixpanelFlagsClient(remoteConfig);
// Context for evaluation
Map<String, Object> userContext = new HashMap<>();
userContext.put("distinct_id", "user-123");
Map<String, Object> customProperties = new HashMap<>();
customProperties.put("platform", "java");
userContext.put("custom_properties", customProperties);
// get_variant_value usage is the same as for local evaluation, but will make a network call to Mixpanel servers at assignment time.
String variantValue = flagsClient.getRemoteFlags().getVariantValue(flagKey, fallbackVariant, userContext);
System.out.println("Variant value: " + variantValue);
// Check if flag is enabled (boolean check)
boolean isEnabled = flagsClient.getRemoteFlags().isEnabled(flagKey, userContext);Configuration Options
LocalFlagsConfig Builder Options
projectToken(String)- Your Mixpanel project token (required)apiHost(String)- API host URL (default: “https://api.mixpanel.com”)enablePolling(boolean)- Whether to enable automatic polling (default: true)pollingIntervalSeconds(int)- Polling interval in seconds (default: 60)requestTimeoutSeconds(int)- Request timeout in seconds (default: 10)
RemoteFlagsConfig Builder Options
projectToken(String)- Your Mixpanel project token (required)apiHost(String)- API host URL (default: “https://api.mixpanel.com”)requestTimeoutSeconds(int)- Request timeout in seconds (default: 5)
Best Practices
Context Management
- Always include
distinct_idin your user context - Include any custom properties used in your flag targeting rules
- Use consistent data types for context values across your application
Resource Management
- Initialize the
MixpanelFlagsClientonce and reuse it throughout your application - For local evaluation, remember to start polling with
startPollingForDefinitions() - Consider implementing proper shutdown procedures to stop polling when your application shuts down (call
MixpanelAPI.close())
Was this page useful?