Class GrowthBookClient
The client owns one feature repository per instance in local mode. In remote-eval mode, feature evaluation is fetched through the remote eval service and cached per user context.
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a client with default options.GrowthBookClient(Options opts) Creates a client with the supplied options. -
Method Summary
Modifier and TypeMethodDescriptionvoidAdds a listener that is notified after a feature refresh succeeds or fails.<T> FeatureResult<T>evalFeature(String key, Class<T> valueTypeClass, UserContext userContext) Evaluates a feature for a user.<ValueType>
Map<String,FeatureResult<ValueType>> evalFeatures(List<String> featureKeys, Class<ValueType> valueTypeClass, UserContext userContext) Evaluates a batch of features for the same user using a single sharedEvaluationContext.getBooleanFeature(FeatureKey<Boolean> featureKey, UserContext userContext) Get a boolean feature value, defaulting tofalsewhen missing or falsy.getBooleanFeature(FeatureKey<Boolean> featureKey, Boolean defaultValue, UserContext userContext) Get a boolean feature value.Returns a read-only snapshot of the current SDK state.getDoubleFeature(FeatureKey<Double> featureKey, Double defaultValue, UserContext userContext) Get a double feature value.<T> FeatureResult<T>getFeature(FeatureKey<T> featureKey, UserContext userContext) Evaluate a feature for a user using a type-safeFeatureKeyinstead of a raw string key.<T> TgetFeatureValue(FeatureKey<T> featureKey, T defaultValue, UserContext userContext) Get a feature value using a typed key, inferring the deserialization class from the key.<T> TgetFeatureValue(String featureKey, T defaultValue, Class<T> gsonDeserializableClass, UserContext userContext) Evaluates a feature and returns its value, falling back to the supplied default on missing or invalid values.getFloatFeature(FeatureKey<Float> featureKey, Float defaultValue, UserContext userContext) Get a float feature value.getIntegerFeature(FeatureKey<Integer> featureKey, Integer defaultValue, UserContext userContext) Get an integer feature value.getStringFeature(FeatureKey<String> featureKey, String defaultValue, UserContext userContext) Get a string feature value.booleanInitializes the feature repository or remote-eval service.isOff(FeatureKey<?> featureKey, UserContext userContext) Checks whether the feature identified by the typed key evaluates to off for a user.isOff(String featureKey, UserContext userContext) Checks whether a feature evaluates to off for a user.isOn(FeatureKey<?> featureKey, UserContext userContext) Checks whether the feature identified by the typed key evaluates to on for a user.isOn(String featureKey, UserContext userContext) Checks whether a feature evaluates to on for a user.booleanpreloadRemoteEval(UserContext userContext) voidDeprecated.voidRefreshes feature definitions using the default refresh behavior.voidrefreshFeatures(RefreshMode refreshMode) Refreshes feature definitions using the provided refresh mode.voidrefreshForRemoteEval(RequestBodyForRemoteEval requestBodyForRemoteEval) Refreshes feature definitions using remote evaluation payload.voidRemoves a previously registered feature refresh listener.<T> ExperimentResult<T>run(Experiment<T> experiment, UserContext userContext) Runs an experiment for a user and notifies experiment subscribers when assignment changes.voidsetGlobalAttributes(String attributes) Replaces global attributes used for future evaluations.voidsetGlobalForceFeatures(Map<String, Object> forceFeatures) Replaces globally forced feature values used for future evaluations.voidsetGlobalForceVariations(Map<String, Integer> forceVariations) Replaces globally forced variations used for future experiment evaluations.voidshutdown()Stops repository background work and releases repository resources.voidsubscribe(ExperimentRunCallback callback) Registers an experiment run callback.Adds a listener and returns an idempotent handle that removes it.
-
Constructor Details
-
GrowthBookClient
public GrowthBookClient()Creates a client with default options. -
GrowthBookClient
Creates a client with the supplied options.- Parameters:
opts- client options; null falls back to default options
-
-
Method Details
-
getDiagnostics
Returns a read-only snapshot of the current SDK state.- Returns:
- diagnostics snapshot
-
initialize
public boolean initialize()Initializes the feature repository or remote-eval service.- Returns:
- true when the client is ready for evaluation
-
setGlobalAttributes
Replaces global attributes used for future evaluations.- Parameters:
attributes- JSON string containing global attributes
-
setGlobalForceFeatures
Replaces globally forced feature values used for future evaluations.- Parameters:
forceFeatures- feature key to forced value map
-
setGlobalForceVariations
Replaces globally forced variations used for future experiment evaluations.- Parameters:
forceVariations- experiment key to forced variation index map
-
refreshFeature
Deprecated.Fetches the latest feature definitions using the default refresh path. -
refreshFeatures
public void refreshFeatures()Refreshes feature definitions using the default refresh behavior. -
refreshFeatures
Refreshes feature definitions using the provided refresh mode.Blocks until the refresh completes, so an evaluation made right after this call sees the refreshed definitions. Use
GBFeaturesRepository.requestFeatureRefresh(RefreshMode)directly when a fire-and-forget refresh is wanted instead.In remote-eval mode this invalidates the remote-eval response cache. The next evaluation fetches a fresh remote response for that user context.
- Parameters:
refreshMode- refresh behavior to use
-
refreshForRemoteEval
Refreshes feature definitions using remote evaluation payload.- Parameters:
requestBodyForRemoteEval- remote evaluation request payload
-
preloadRemoteEval
-
evalFeature
public <T> FeatureResult<T> evalFeature(String key, Class<T> valueTypeClass, UserContext userContext) Evaluates a feature for a user.- Type Parameters:
T- feature value type- Parameters:
key- feature keyvalueTypeClass- expected value classuserContext- user context- Returns:
- feature evaluation result
-
evalFeatures
public <ValueType> Map<String,FeatureResult<ValueType>> evalFeatures(List<String> featureKeys, Class<ValueType> valueTypeClass, UserContext userContext) Evaluates a batch of features for the same user using a single sharedEvaluationContext. Attribute merging, global-context references and any sticky-bucket preload happen once for the whole batch instead of once per feature, so memory stays O(size(context)) rather than O(features × size(context)).- Type Parameters:
ValueType- the result value type- Parameters:
featureKeys- the feature keys to evaluatevalueTypeClass- the expected value type (typicallyObject.classfor mixed types)userContext- the user context, processed once- Returns:
- a map from feature key to its
FeatureResult
-
isOn
Checks whether a feature evaluates to on for a user.- Parameters:
featureKey- feature keyuserContext- user context- Returns:
- true when the feature is on
-
isOff
Checks whether a feature evaluates to off for a user.- Parameters:
featureKey- feature keyuserContext- user context- Returns:
- true when the feature is off
-
getFeatureValue
public <T> T getFeatureValue(String featureKey, T defaultValue, Class<T> gsonDeserializableClass, UserContext userContext) Evaluates a feature and returns its value, falling back to the supplied default on missing or invalid values.- Type Parameters:
T- feature value type- Parameters:
featureKey- feature keydefaultValue- fallback valuegsonDeserializableClass- expected value classuserContext- user context- Returns:
- evaluated feature value or default value
-
getFeature
Evaluate a feature for a user using a type-safeFeatureKeyinstead of a raw string key.As with
evalFeature(String, Class, UserContext), the returned result'sFeatureResult.getValue()is the raw evaluated value (a boxed primitive, or aMap/Listfor object and array features); it is not deserialized into the key's value type. To obtain a deserialized instance of a complex type, usegetFeatureValue(FeatureKey, Object, UserContext).- Type Parameters:
T- feature value type carried by the key- Parameters:
featureKey- typed feature key, e.g.Features.NEW_HOMEuserContext- user context- Returns:
- the feature result
-
isOn
Checks whether the feature identified by the typed key evaluates to on for a user.- Parameters:
featureKey- typed feature keyuserContext- user context- Returns:
- true when the feature is on
-
isOff
Checks whether the feature identified by the typed key evaluates to off for a user.- Parameters:
featureKey- typed feature keyuserContext- user context- Returns:
- true when the feature is off
-
getFeatureValue
Get a feature value using a typed key, inferring the deserialization class from the key.- Type Parameters:
T- feature value type carried by the key- Parameters:
featureKey- typed feature keydefaultValue- value to return when the feature is missing or invaliduserContext- user context- Returns:
- the found value or defaultValue
-
getBooleanFeature
Get a boolean feature value, defaulting tofalsewhen missing or falsy.- Parameters:
featureKey- typed boolean feature keyuserContext- user context- Returns:
- the found value or
false
-
getBooleanFeature
public Boolean getBooleanFeature(FeatureKey<Boolean> featureKey, Boolean defaultValue, UserContext userContext) Get a boolean feature value.- Parameters:
featureKey- typed boolean feature keydefaultValue- value to return when the feature is missing or invaliduserContext- user context- Returns:
- the found value or defaultValue
-
getStringFeature
public String getStringFeature(FeatureKey<String> featureKey, String defaultValue, UserContext userContext) Get a string feature value.- Parameters:
featureKey- typed string feature keydefaultValue- value to return when the feature is missing or invaliduserContext- user context- Returns:
- the found value or defaultValue
-
getIntegerFeature
public Integer getIntegerFeature(FeatureKey<Integer> featureKey, Integer defaultValue, UserContext userContext) Get an integer feature value.- Parameters:
featureKey- typed integer feature keydefaultValue- value to return when the feature is missing or invaliduserContext- user context- Returns:
- the found value or defaultValue
-
getDoubleFeature
public Double getDoubleFeature(FeatureKey<Double> featureKey, Double defaultValue, UserContext userContext) Get a double feature value.- Parameters:
featureKey- typed double feature keydefaultValue- value to return when the feature is missing or invaliduserContext- user context- Returns:
- the found value or defaultValue
-
getFloatFeature
public Float getFloatFeature(FeatureKey<Float> featureKey, Float defaultValue, UserContext userContext) Get a float feature value.- Parameters:
featureKey- typed float feature keydefaultValue- value to return when the feature is missing or invaliduserContext- user context- Returns:
- the found value or defaultValue
-
run
Runs an experiment for a user and notifies experiment subscribers when assignment changes.- Type Parameters:
T- experiment value type- Parameters:
experiment- experiment to evaluateuserContext- user context- Returns:
- experiment evaluation result
-
subscribe
Registers an experiment run callback.- Parameters:
callback- callback invoked when an experiment assignment changes
-
addFeatureRefreshListener
Adds a listener that is notified after a feature refresh succeeds or fails.Listeners never run on the thread that performed the refresh: they are dispatched on
Options.getFeatureRefreshListenerExecutor(), or on a dedicated daemon thread when none is configured. A listener may therefore block without stalling polling or SSE updates. If that executor rejects the task the event is dropped with a warning rather than run inline, so configuring a rejecting executor gives back-pressure, not blocking.- Parameters:
listener- listener to add
-
subscribeFeatureRefreshListener
Adds a listener and returns an idempotent handle that removes it.Dispatch and threading are as described on
addFeatureRefreshListener(growthbook.sdk.java.listener.FeatureRefreshListener).- Parameters:
listener- listener to add- Returns:
- subscription handle
-
removeFeatureRefreshListener
Removes a previously registered feature refresh listener.- Parameters:
listener- listener to remove
-
shutdown
public void shutdown()Stops repository background work and releases repository resources.Synchronized so concurrent shutdown calls cannot interleave teardown of the repository, the remote-eval coordinator, the listener executor, and the plugin registry.
-
refreshFeatures()orrefreshFeatures(RefreshMode).