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.ObjectThis class encapsulate theOkHttpClientinstance 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.OkHttpClientconfigureClient(HttpConfigOptions options)Configures the currentOkHttpClientinstance based on the passed-in options, replaces the current instance with the newly-configured instance and returns the new instance.okhttp3.OkHttpClientconfigureClient(okhttp3.OkHttpClient client, HttpConfigOptions options)Configures the specifiedOkHttpClientinstance based on the passed-in options, and returns a new instance with the requested options applied.okhttp3.OkHttpClientcreateHttpClient()Creates a newOkHttpClientinstance with a newServiceCookieJarand a default configuration.okhttp3.OkHttpClientgetHttpClient()Returns the currentOkHttpClientinstance held by this singleton.static HttpClientSingletongetInstance()Gets the single instance of HttpClientSingleton.voidsetHttpClient(okhttp3.OkHttpClient client)Sets the currentOkHttpClientinstance held by this singleton.static IRetryStrategysetRetryStrategy(IRetryStrategy strategy)Sets the factory to be used to construct retry interceptor instances.static voidsetupTLSProtocol(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 currentOkHttpClientinstance 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 currentOkHttpClientinstance 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- theOkHttpClientbuilder.
-
createHttpClient
public okhttp3.OkHttpClient createHttpClient()
Creates a newOkHttpClientinstance with a newServiceCookieJarand a default configuration.- Returns:
- the new
OkHttpClientinstance
-
configureClient
public okhttp3.OkHttpClient configureClient(HttpConfigOptions options)
Configures the currentOkHttpClientinstance based on the passed-in options, replaces the current instance with the newly-configured instance and returns the new instance.- Parameters:
options- theHttpConfigOptionsobject for modifying the client instance- Returns:
- the new client instance
-
configureClient
public okhttp3.OkHttpClient configureClient(okhttp3.OkHttpClient client, HttpConfigOptions options)Configures the specifiedOkHttpClientinstance based on the passed-in options, and returns a new instance with the requested options applied.- Parameters:
client- theOkHttpClientinstance to configureoptions- theHttpConfigOptionsinstance for modifying the client- Returns:
- a new
OkHttpClientinstance with the specified options applied
-
-