Interface DataSourceStatusProvider


  • public interface DataSourceStatusProvider
    An interface for querying the status of the SDK's data source. The data source is the component that receives updates to feature flag data; normally this is a streaming connection, but it could be polling or file data depending on your configuration.

    An implementation of this interface is returned by LDClientInterface.getDataSourceStatusProvider(). Application code never needs to implement this interface.

    Since:
    5.0.0
    • Method Detail

      • getStatus

        DataSourceStatusProvider.Status getStatus()
        Returns the current status of the data source.

        All of the built-in data source implementations are guaranteed to update this status whenever they successfully initialize, encounter an error, or recover after an error.

        For a custom data source implementation, it is the responsibility of the data source to push status updates to the SDK; if it does not do so, the status will always be reported as DataSourceStatusProvider.State.INITIALIZING.

        Returns:
        the latest status; will never be null
      • addStatusListener

        void addStatusListener​(DataSourceStatusProvider.StatusListener listener)
        Subscribes for notifications of status changes.

        The listener will be notified whenever any property of the status has changed. See DataSourceStatusProvider.Status for an explanation of the meaning of each property and what could cause it to change.

        Notifications will be dispatched on a worker thread. It is the listener's responsibility to return as soon as possible so as not to block subsequent notifications.

        Parameters:
        listener - the listener to add
      • removeStatusListener

        void removeStatusListener​(DataSourceStatusProvider.StatusListener listener)
        Unsubscribes from notifications of status changes.
        Parameters:
        listener - the listener to remove; if no such listener was added, this does nothing
      • waitFor

        boolean waitFor​(DataSourceStatusProvider.State desiredState,
                        java.time.Duration timeout)
                 throws java.lang.InterruptedException
        A synchronous method for waiting for a desired connection state.

        If the current state is already desiredState when this method is called, it immediately returns. Otherwise, it blocks until 1. the state has become desiredState, 2. the state has become DataSourceStatusProvider.State.OFF (since that is a permanent condition), 3. the specified timeout elapses, or 4. the current thread is deliberately interrupted with Thread.interrupt().

        A scenario in which this might be useful is if you want to create the LDClient without waiting for it to initialize, and then wait for initialization at a later time or on a different thread:

        
             // create the client but do not wait
             LDConfig config = new LDConfig.Builder().startWait(Duration.ZERO).build();
             client = new LDClient(sdkKey, config);
             
             // later, possibly on another thread:
             boolean inited = client.getDataSourceStatusProvider().waitFor(
                 DataSourceStatusProvider.State.VALID, Duration.ofSeconds(10));
             if (!inited) {
                 // do whatever is appropriate if initialization has timed out
             }       
         
        Parameters:
        desiredState - the desired connection state (normally this would be DataSourceStatusProvider.State.VALID)
        timeout - the maximum amount of time to wait-- or Duration.ZERO to block indefinitely (unless the thread is explicitly interrupted)
        Returns:
        true if the connection is now in the desired state; false if it timed out, or if the state changed to DataSourceStatusProvider.State.OFF and that was not the desired state
        Throws:
        java.lang.InterruptedException - if Thread.interrupt() was called on this thread while blocked