Class GrowthBookClient

java.lang.Object
growthbook.sdk.java.multiusermode.GrowthBookClient

public class GrowthBookClient extends Object
Multi-user GrowthBook SDK facade.

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 Details

    • GrowthBookClient

      public GrowthBookClient()
      Creates a client with default options.
    • GrowthBookClient

      public GrowthBookClient(Options opts)
      Creates a client with the supplied options.
      Parameters:
      opts - client options; null falls back to default options
  • Method Details

    • getDiagnostics

      public Diagnostics 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

      public void setGlobalAttributes(String attributes)
      Replaces global attributes used for future evaluations.
      Parameters:
      attributes - JSON string containing global attributes
    • setGlobalForceFeatures

      public void setGlobalForceFeatures(Map<String,Object> forceFeatures)
      Replaces globally forced feature values used for future evaluations.
      Parameters:
      forceFeatures - feature key to forced value map
    • setGlobalForceVariations

      public void setGlobalForceVariations(Map<String,Integer> forceVariations)
      Replaces globally forced variations used for future experiment evaluations.
      Parameters:
      forceVariations - experiment key to forced variation index map
    • refreshFeature

      @Deprecated public void refreshFeature()
      Fetches the latest feature definitions using the default refresh path.
    • refreshFeatures

      public void refreshFeatures()
      Refreshes feature definitions using the default refresh behavior.
    • refreshFeatures

      public void refreshFeatures(RefreshMode refreshMode)
      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

      public void refreshForRemoteEval(RequestBodyForRemoteEval requestBodyForRemoteEval)
      Refreshes feature definitions using remote evaluation payload.
      Parameters:
      requestBodyForRemoteEval - remote evaluation request payload
    • preloadRemoteEval

      public boolean preloadRemoteEval(UserContext userContext)
    • 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 key
      valueTypeClass - expected value class
      userContext - 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 shared EvaluationContext. 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 evaluate
      valueTypeClass - the expected value type (typically Object.class for mixed types)
      userContext - the user context, processed once
      Returns:
      a map from feature key to its FeatureResult
    • isOn

      public Boolean isOn(String featureKey, UserContext userContext)
      Checks whether a feature evaluates to on for a user.
      Parameters:
      featureKey - feature key
      userContext - user context
      Returns:
      true when the feature is on
    • isOff

      public Boolean isOff(String featureKey, UserContext userContext)
      Checks whether a feature evaluates to off for a user.
      Parameters:
      featureKey - feature key
      userContext - 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 key
      defaultValue - fallback value
      gsonDeserializableClass - expected value class
      userContext - user context
      Returns:
      evaluated feature value or default value
    • getFeature

      public <T> FeatureResult<T> getFeature(FeatureKey<T> featureKey, UserContext userContext)
      Evaluate a feature for a user using a type-safe FeatureKey instead of a raw string key.

      As with evalFeature(String, Class, UserContext), the returned result's FeatureResult.getValue() is the raw evaluated value (a boxed primitive, or a Map/List for object and array features); it is not deserialized into the key's value type. To obtain a deserialized instance of a complex type, use getFeatureValue(FeatureKey, Object, UserContext).

      Type Parameters:
      T - feature value type carried by the key
      Parameters:
      featureKey - typed feature key, e.g. Features.NEW_HOME
      userContext - user context
      Returns:
      the feature result
    • isOn

      public Boolean isOn(FeatureKey<?> featureKey, UserContext userContext)
      Checks whether the feature identified by the typed key evaluates to on for a user.
      Parameters:
      featureKey - typed feature key
      userContext - user context
      Returns:
      true when the feature is on
    • isOff

      public Boolean isOff(FeatureKey<?> featureKey, UserContext userContext)
      Checks whether the feature identified by the typed key evaluates to off for a user.
      Parameters:
      featureKey - typed feature key
      userContext - user context
      Returns:
      true when the feature is off
    • getFeatureValue

      public <T> T getFeatureValue(FeatureKey<T> featureKey, T defaultValue, UserContext userContext)
      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 key
      defaultValue - value to return when the feature is missing or invalid
      userContext - user context
      Returns:
      the found value or defaultValue
    • getBooleanFeature

      public Boolean getBooleanFeature(FeatureKey<Boolean> featureKey, UserContext userContext)
      Get a boolean feature value, defaulting to false when missing or falsy.
      Parameters:
      featureKey - typed boolean feature key
      userContext - 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 key
      defaultValue - value to return when the feature is missing or invalid
      userContext - 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 key
      defaultValue - value to return when the feature is missing or invalid
      userContext - 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 key
      defaultValue - value to return when the feature is missing or invalid
      userContext - 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 key
      defaultValue - value to return when the feature is missing or invalid
      userContext - 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 key
      defaultValue - value to return when the feature is missing or invalid
      userContext - user context
      Returns:
      the found value or defaultValue
    • run

      public <T> ExperimentResult<T> run(Experiment<T> experiment, UserContext userContext)
      Runs an experiment for a user and notifies experiment subscribers when assignment changes.
      Type Parameters:
      T - experiment value type
      Parameters:
      experiment - experiment to evaluate
      userContext - user context
      Returns:
      experiment evaluation result
    • subscribe

      public void subscribe(ExperimentRunCallback callback)
      Registers an experiment run callback.
      Parameters:
      callback - callback invoked when an experiment assignment changes
    • addFeatureRefreshListener

      public void addFeatureRefreshListener(FeatureRefreshListener listener)
      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

      public FeatureRefreshSubscription subscribeFeatureRefreshListener(FeatureRefreshListener listener)
      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

      public void removeFeatureRefreshListener(FeatureRefreshListener listener)
      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.