Short Version: Is it possible to configure a custom retry handler when using the ApacheConnectorProvider + PoolingHttpClientConnectionManager? If so, how?
Long Version: I'm using Jersey 2.25.1 and I've recently switched from using the default HTTP connection mechanism to the Apache PoolingHttpClientConnectionManager to improve the scaling ability of my automated test framework.
It is generally working, but as I scale, I get the occasional 'connect timeout'. I believe that the Apache HTTPClient has the ability to accept a custom retry handler, but I don't know how to configure a custom handler with Jersey. I can't find any examples in my many search attempts.
Something similar was asked 2 years ago Here but has no answers.
Here is my initialization code for the Jersey client:
SSLContext ctx = initSSLTrustFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.getSocketFactory())
.register("https", new SSLConnectionSocketFactory(ctx, NoopHostnameVerifier.INSTANCE))
.build();
ClientConfig cfg = new ClientConfig();
PoolingHttpClientConnectionManager connMan = new PoolingHttpClientConnectionManager(registry);
connMan.setMaxTotal(200);
connMan.setDefaultMaxPerRoute(50);
if (timeout != -1) {
SocketConfig sc = SocketConfig.custom()
.setSoTimeout(timeout)
.setSoReuseAddress(true)
.setTcpNoDelay(true)
.setSoReuseAddress(true)
.build();
connMan.setDefaultSocketConfig(sc);
cfg.property(ApacheClientProperties.REQUEST_CONFIG, RequestConfig
.custom()
.setConnectTimeout(timeout)
.setConnectionRequestTimeout(timeout)
.setSocketTimeout(timeout)
.build()
);
}
cfg.property(ApacheClientProperties.CONNECTION_MANAGER, connMan);
ApacheConnectorProvider acp = new ApacheConnectorProvider();
cfg.connectorProvider(acp);
ClientBuilder builder = JerseyClientBuilder.newBuilder();
client = builder
.hostnameVerifier((String hostname, SSLSession session) -> true)
.withConfig(cfg)
.register(JacksonFeature.class)
.register(MultiPartWriter.class)
.build();
There is an example HERE that shows one way to set it on the actual Apache HTTPClient, but I don't see a way to do the equivalent in Jersey but I'm still trying to understand how to customize Jersey.
I would have thought it done by doing something in the ClientConfig instance similar to:
cfg.property(ApacheClientProperties.RETRY_HANDLER, new HttpRequestRetryHandler() {
public boolean retryRequest(
IOException exception,
int executionCount,
HttpContext context) {
...
}
}
);
But a retry handler client property setting does not exist.
Thanks for any pointers!