Class GBFeaturesRepository

java.lang.Object
growthbook.sdk.java.repository.GBFeaturesRepository
All Implemented Interfaces:
IGBFeaturesRepository

public class GBFeaturesRepository extends Object implements IGBFeaturesRepository
This class can be created with its `builder()` or constructor. It will fetch the features from the endpoint provided. Initialize with initialize() Get the features JSON with getFeaturesJson(). You would provide the features JSON when creating the GBContext
  • Field Details

  • Constructor Details

  • Method Details

    • getFeatureSnapshot

      public FeatureSnapshot getFeatureSnapshot()
      The current features payload as one immutable snapshot. Prefer this over the individual getters when consuming more than one part of the payload.
      Returns:
      the snapshot from the most recent successful refresh
    • getSavedGroupsJson

      @Nullable public String getSavedGroupsJson()
      Allows you to get the saved groups JSON from the provided getFeaturesEndpoint(). You must call initialize() before calling this method or your saved groups would not have loaded.
      Returns:
      saved groups JSON string
    • getFeaturesJson

      public String getFeaturesJson()
      Allows you to get the features JSON from the provided getFeaturesEndpoint(). You must call initialize() before calling this method or your features would not have loaded.
      Specified by:
      getFeaturesJson in interface IGBFeaturesRepository
      Returns:
      feature data JSON in a type of String. Handle refresh strategy
    • getParsedFeatures

      public Map<String,Feature<?>> getParsedFeatures()
      Keys are unique identifiers for the features and the values are Feature objects. Feature definitions - To be pulled from API / Cache
      Returns:
      parsed feature definitions
    • getParsedSavedGroups

      public com.google.gson.JsonObject getParsedSavedGroups()
    • setCacheManager

      public void setCacheManager(GbCacheManager cacheManager)
    • getEncryptionKey

      @Deprecated @Nullable public String getEncryptionKey()
      Deprecated.
    • getLastSuccessfulFetchAtMillis

      public long getLastSuccessfulFetchAtMillis()
    • getLastRefreshError

      @Nullable public Throwable getLastRefreshError()
      Description copied from interface: IGBFeaturesRepository
      Returns the most recent feature refresh error, if any. Used for diagnostics only.
      Specified by:
      getLastRefreshError in interface IGBFeaturesRepository
      Returns:
      the last refresh Throwable, or null when no refresh has failed
    • getLastRefreshErrorAtMillis

      public long getLastRefreshErrorAtMillis()
      Description copied from interface: IGBFeaturesRepository
      Returns the epoch-millis timestamp of the most recent feature refresh error. Used for diagnostics only.
      Specified by:
      getLastRefreshErrorAtMillis in interface IGBFeaturesRepository
      Returns:
      the timestamp in milliseconds, or 0 when no refresh has failed
    • hasFeatureData

      public boolean hasFeatureData()
    • isCacheDisabled

      public boolean isCacheDisabled()
    • getCacheLastUpdatedMillis

      @Nullable public Long getCacheLastUpdatedMillis()
    • isSseAllowed

      public boolean isSseAllowed()
    • isSseConnected

      public boolean isSseConnected()
    • getSseRetryAttempts

      public int getSseRetryAttempts()
    • getRefreshSuccessCount

      public long getRefreshSuccessCount()
      Specified by:
      getRefreshSuccessCount in interface IGBFeaturesRepository
    • getRefreshFailureCount

      public long getRefreshFailureCount()
      Specified by:
      getRefreshFailureCount in interface IGBFeaturesRepository
    • getRefreshConsecutiveFailureCount

      public long getRefreshConsecutiveFailureCount()
      Specified by:
      getRefreshConsecutiveFailureCount in interface IGBFeaturesRepository
    • getLastRefreshFailureAtMillis

      public long getLastRefreshFailureAtMillis()
      Specified by:
      getLastRefreshFailureAtMillis in interface IGBFeaturesRepository
    • getLastRefreshLoadedFromCache

      @Nullable public Boolean getLastRefreshLoadedFromCache()
      Specified by:
      getLastRefreshLoadedFromCache in interface IGBFeaturesRepository
    • onFeaturesRefresh

      public void onFeaturesRefresh(FeatureRefreshCallback callback)
      Subscribe to feature refresh events This callback is called when the features are successfully refreshed or there is an error when refreshing. This is called even if the features have not changed.
      Specified by:
      onFeaturesRefresh in interface IGBFeaturesRepository
      Parameters:
      callback - This callback will be called when features are refreshed
    • clearCallbacks

      public void clearCallbacks()
      Description copied from interface: IGBFeaturesRepository
      Clears the feature refresh callbacks
      Specified by:
      clearCallbacks in interface IGBFeaturesRepository
    • initialize

      public void initialize() throws FeatureFetchException
      Specified by:
      initialize in interface IGBFeaturesRepository
      Throws:
      FeatureFetchException
    • initialize

      public void initialize(Boolean retryOnFailure) throws FeatureFetchException
      Specified by:
      initialize in interface IGBFeaturesRepository
      Throws:
      FeatureFetchException
    • fetchFeatures

      public void fetchFeatures() throws FeatureFetchException
      Performs a network request to fetch the features from the GrowthBook API with the provided endpoint. If an encryptionKey is provided, it is assumed the features endpoint is using encrypted features. This method will attempt to decrypt the encrypted features with the provided encryptionKey.
      Throws:
      FeatureFetchException
    • refreshFeatures

      public void refreshFeatures() throws FeatureFetchException
      Throws:
      FeatureFetchException
    • refreshFeatures

      public void refreshFeatures(RefreshMode refreshMode) throws FeatureFetchException
      Throws:
      FeatureFetchException
    • requestFeatureRefresh

      public void requestFeatureRefresh(RefreshMode refreshMode)
    • getActiveFeatureCount

      public int getActiveFeatureCount()
    • shutdown

      public void shutdown()
    • fetchForRemoteEval

      public void fetchForRemoteEval(RequestBodyForRemoteEval requestBodyForRemoteEval) throws FeatureFetchException
      Throws:
      FeatureFetchException
    • builder

    • getFeaturesEndpoint

      public String getFeaturesEndpoint()
      Endpoint for GET request
    • getEventsEndpoint

      public String getEventsEndpoint()
      Endpoint for SSE request
    • getRefreshStrategy

      public FeatureRefreshStrategy getRefreshStrategy()
      Strategy for building url
    • getDecryptionKey

      @Nullable public String getDecryptionKey()
      The key used to decrypt encrypted features from the API
    • getSwrTtlSeconds

      public Integer getSwrTtlSeconds()
      The standard cache TTL to use (60 seconds)
    • getBackgroundFetchInterval

      @Nullable public Duration getBackgroundFetchInterval()
      Optional minimum interval between non-forced feature refreshes.
    • getRetryPolicy

      public FeatureFetchRetryPolicy getRetryPolicy()
      Bounded retry policy for feature fetch requests.
    • getExpiresAt

      public Long getExpiresAt()
      Seconds after that cache is expired
    • getInitialized

      public Boolean getInitialized()
      Flag to know whether GBFeatureRepository is initialized
    • getCacheManager

      public GbCacheManager getCacheManager()
      CachingManger allows to cache features data to file
    • getRequestBodyForRemoteEval

      @Nullable public RequestBodyForRemoteEval getRequestBodyForRemoteEval()
      Request body for that be sent with POST request for remote eval feature
    • getRemoteEvalEndPoint

      public String getRemoteEvalEndPoint()
      Endpoint for POST request