Package com.ibm.cloud.sdk.core.http
Class HttpClientSingleton
- java.lang.Object
-
- com.ibm.cloud.sdk.core.http.HttpClientSingleton
-
public class HttpClientSingleton extends java.lang.Object
This class encapsulate theOkHttpClient
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 currentOkHttpClient
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 specifiedOkHttpClient
instance based on the passed-in options, and returns a new instance with the requested options applied.okhttp3.OkHttpClient
createHttpClient()
Creates a newOkHttpClient
instance with a newServiceCookieJar
and a default configuration.okhttp3.OkHttpClient
getHttpClient()
Returns the currentOkHttpClient
instance held by this singleton.static HttpClientSingleton
getInstance()
Gets the single instance of HttpClientSingleton.void
setHttpClient(okhttp3.OkHttpClient client)
Sets the currentOkHttpClient
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.
-
-
-
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 currentOkHttpClient
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 currentOkHttpClient
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
- theOkHttpClient
builder.
-
createHttpClient
public okhttp3.OkHttpClient createHttpClient()
Creates a newOkHttpClient
instance with a newServiceCookieJar
and a default configuration.- Returns:
- the new
OkHttpClient
instance
-
configureClient
public okhttp3.OkHttpClient configureClient(HttpConfigOptions options)
Configures the currentOkHttpClient
instance based on the passed-in options, replaces the current instance with the newly-configured instance and returns the new instance.- Parameters:
options
- theHttpConfigOptions
object for modifying the client instance- Returns:
- the new client instance
-
configureClient
public okhttp3.OkHttpClient configureClient(okhttp3.OkHttpClient client, HttpConfigOptions options)
Configures the specifiedOkHttpClient
instance based on the passed-in options, and returns a new instance with the requested options applied.- Parameters:
client
- theOkHttpClient
instance to configureoptions
- theHttpConfigOptions
instance for modifying the client- Returns:
- a new
OkHttpClient
instance with the specified options applied
-
-