Class ConnectStrategy

  • Direct Known Subclasses:
    HttpConnectStrategy

    public abstract class ConnectStrategy
    extends java.lang.Object
    An abstraction of how EventSource should obtain an input stream.

    The default implementation is HttpConnectStrategy, which makes HTTP requests. To customize the HTTP behavior, you can use methods of HttpConnectStrategy:

    
         EventSource.Builder builder = new EventSource.Builder(
           ConnectStrategy.http(myStreamUri)
             .headers(myCustomHeaders)
             .connectTimeout(10, TimeUnit.SECONDS)
         );
     

    Or, if you want to consume an input stream from some other source, you can create your own subclass of ConnectStrategy.

    Instances of this class should be immutable and not contain any state that is specific to one active stream. The ConnectStrategy.Client that they produce is stateful and belongs to a single EventSource.

    Since:
    4.0.0
    • Constructor Detail

      • ConnectStrategy

        public ConnectStrategy()
    • Method Detail

      • createClient

        public abstract ConnectStrategy.Client createClient​(LDLogger logger)
        Creates a client instance.

        This is called once when an EventSource is created. The EventSource retains the returned Client and uses it to perform all subsequent connection attempts.

        Parameters:
        logger - the logger belonging to EventSource
        Returns:
        a ConnectStrategy.Client instance
      • http

        public static HttpConnectStrategy http​(java.net.URI uri)
        Returns the default HTTP implementation, specifying a stream URI.

        To specify custom HTTP behavior, call HttpConnectStrategy methods on the returned object to obtain a modified instance:

        
             EventSource.Builder builder = new EventSource.Builder(
               ConnectStrategy.http(myStreamUri)
                 .headers(myCustomHeaders)
                 .connectTimeout(10, TimeUnit.SECONDS)
             );
         
        Parameters:
        uri - the stream URI
        Returns:
        a configurable HttpConnectStrategy
        Throws:
        java.lang.IllegalArgumentException - if the argument is null, or if the scheme is not HTTP or HTTPS
        See Also:
        http(HttpUrl)
      • http

        public static HttpConnectStrategy http​(java.net.URL url)
        Returns the default HTTP implementation, specifying a stream URL.

        This is the same as http(URI), but uses the URL type.

        Parameters:
        url - the stream URL
        Returns:
        a configurable HttpConnectStrategy
        Throws:
        java.lang.IllegalArgumentException - if the argument is null, or if the scheme is not HTTP or HTTPS
        See Also:
        http(HttpUrl)
      • http

        public static HttpConnectStrategy http​(okhttp3.HttpUrl url)
        Returns the default HTTP implementation, specifying a stream URI.

        This is the same as http(URI), but uses the okhttp type HttpUrl.

        Parameters:
        url - the stream URL
        Returns:
        a configurable HttpConnectStrategy
        Throws:
        java.lang.IllegalArgumentException - if the argument is null, or if the scheme is not HTTP or HTTPS
        See Also:
        http(URI)