public class HttpClientSingleton
extends java.lang.Object
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.Modifier and Type | Method and 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.
|
public static IRetryStrategy setRetryStrategy(IRetryStrategy strategy)
strategy
- the IRetryStrategy implementation to be set as the factorypublic static HttpClientSingleton getInstance()
public okhttp3.OkHttpClient getHttpClient()
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.public void setHttpClient(okhttp3.OkHttpClient client)
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.client
- the new OkHttpClient instance to use as a default client configurationpublic static void setupTLSProtocol(okhttp3.OkHttpClient.Builder builder)
builder
- the OkHttpClient
builder.public okhttp3.OkHttpClient createHttpClient()
OkHttpClient
instance with a new ServiceCookieJar
and a default configuration.OkHttpClient
instancepublic okhttp3.OkHttpClient configureClient(HttpConfigOptions options)
OkHttpClient
instance based on the passed-in options, replaces
the current instance with the newly-configured instance and returns the new instance.options
- the HttpConfigOptions
object for modifying the client instancepublic okhttp3.OkHttpClient configureClient(okhttp3.OkHttpClient client, HttpConfigOptions options)
OkHttpClient
instance based on the passed-in options,
and returns a new instance with the requested options applied.client
- the OkHttpClient
instance to configureoptions
- the HttpConfigOptions
instance for modifying the clientOkHttpClient
instance with the specified options appliedCopyright © 2022 IBM Cloud Developer Experience. All rights reserved.