Java SDK thread-safe HttpClient config

Getting IllegalStateException: Connection pool shut down when scaling up concurrent requests with the Java Platform SDK. The default builder creates an HttpClient that doesn’t seem to handle high concurrency well. Does PlatformClientFactory.createApiClient expose a way to inject a custom PoolingHttpClientConnectionManager? Or is there a specific method to set the max connections per route before initializing the client?

Yeah, the default connection pooling in the Java SDK is pretty conservative out of the box. It chokes the second you hit any real concurrency. You don’t need to rebuild the whole client, just swap the connection manager before you initialize.

The PlatformClientFactory doesn’t have a direct setter for the pool size, but you can hook into the underlying RestClient configuration. Here’s how I usually patch it. You need to create your own PoolingHttpClientConnectionManager and attach it to the CloseableHttpClient builder before passing it to the SDK.

import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.mypurecloud.sdk.v2.PlatformClientFactory;
import com.mypurecloud.sdk.v2.client.ApiClient;

// 1. Configure the pool
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
connManager.setMaxTotal(200); // Total connections across all routes
connManager.setDefaultMaxPerRoute(20); // Connections per specific host

// 2. Build the client with the custom manager
CloseableHttpClient customHttpClient = HttpClients.custom()
 .setConnectionManager(connManager)
 .build();

// 3. Initialize the API client using the custom HTTP client
// Note: The exact method signature depends on your SDK version, 
// but usually you pass the configured HttpClient to the ApiClient constructor or builder.
ApiClient apiClient = PlatformClientFactory.createApiClient()
 .withHttpClient(customHttpClient); // Pseudo-code for clarity, check your SDK version's builder pattern

// 4. ceed with OAuth as normal
apiClient.setAccessToken("your_token");

If you’re on a newer SDK version that abstracts the HttpClient entirely, check the PlatformClientFactory.createApiClient() return type. Sometimes they expose a setHttpClient method on the resulting ApiClient instance before you call setAccessToken.

Just keep an eye on the thread pool size in your application too. If you have 500 threads but only 20 max connections per route, they’re just going to queue up and timeout. It’s a common oversight. The connection pool needs to match the expected load.