Class HttpClientSingleton


  • public class HttpClientSingleton
    extends java.lang.Object
    This class encapsulate the OkHttpClient instance in a singleton pattern. OkHttp performs best when you create a single OkHttpClient instance and reuse it for all of your HTTP calls. This is because each client holds its own connection pool and thread pools. Reusing connections and threads reduces latency and saves memory. Conversely, creating a client for each request wastes resources on idle pools.
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      okhttp3.OkHttpClient configureClient​(HttpConfigOptions options)
      Configures the current OkHttpClient instance based on the passed-in options, replaces the current instance with the newly-configured instance and returns the new instance.
      okhttp3.OkHttpClient configureClient​(okhttp3.OkHttpClient client, HttpConfigOptions options)
      Configures the specified OkHttpClient instance based on the passed-in options, and returns a new instance with the requested options applied.
      okhttp3.OkHttpClient createHttpClient()
      Creates a new OkHttpClient instance with a new ServiceCookieJar and a default configuration.
      okhttp3.OkHttpClient getHttpClient()
      Returns the current OkHttpClient instance held by this singleton.
      static HttpClientSingleton getInstance()
      Gets the single instance of HttpClientSingleton.
      void setHttpClient​(okhttp3.OkHttpClient client)
      Sets the current OkHttpClient instance held by this singleton.
      static IRetryStrategy setRetryStrategy​(IRetryStrategy strategy)
      Sets the factory to be used to construct retry interceptor instances.
      static void setupTLSProtocol​(okhttp3.OkHttpClient.Builder builder)
      Specifically enable all TLS protocols.
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Method Detail

      • setRetryStrategy

        public static IRetryStrategy setRetryStrategy​(IRetryStrategy strategy)
        Sets the factory to be used to construct retry interceptor instances.
        Parameters:
        strategy - the IRetryStrategy implementation to be set as the factory
        Returns:
        the previous factory
      • getInstance

        public static HttpClientSingleton getInstance()
        Gets the single instance of HttpClientSingleton.
        Returns:
        single instance of HttpClientSingleton
      • getHttpClient

        public okhttp3.OkHttpClient getHttpClient()
        Returns the current OkHttpClient instance held by this singleton. This is the client instance that is used as a default configuration from which other client instances are built.
        Returns:
        the current OkHttpClient instance
      • setHttpClient

        public void setHttpClient​(okhttp3.OkHttpClient client)
        Sets the current OkHttpClient instance held by this singleton. This is the client instance that is used as a default configuration from which other client instances are built.
        Parameters:
        client - the new OkHttpClient instance to use as a default client configuration
      • setupTLSProtocol

        public static void setupTLSProtocol​(okhttp3.OkHttpClient.Builder builder)
        Specifically enable all TLS protocols. See: https://github.com/watson-developer-cloud/java-sdk/issues/610
        Parameters:
        builder - the OkHttpClient builder.
      • createHttpClient

        public okhttp3.OkHttpClient createHttpClient()
        Creates a new OkHttpClient instance with a new ServiceCookieJar and a default configuration.
        Returns:
        the new OkHttpClient instance
      • configureClient

        public okhttp3.OkHttpClient configureClient​(HttpConfigOptions options)
        Configures the current OkHttpClient instance based on the passed-in options, replaces the current instance with the newly-configured instance and returns the new instance.
        Parameters:
        options - the HttpConfigOptions object for modifying the client instance
        Returns:
        the new client instance
      • configureClient

        public okhttp3.OkHttpClient configureClient​(okhttp3.OkHttpClient client,
                                                    HttpConfigOptions options)
        Configures the specified OkHttpClient instance based on the passed-in options, and returns a new instance with the requested options applied.
        Parameters:
        client - the OkHttpClient instance to configure
        options - the HttpConfigOptions instance for modifying the client
        Returns:
        a new OkHttpClient instance with the specified options applied