[Jersey] Re: Jersey 2 per request timeout

From: Richard Sand <>
Date: Wed, 15 Jun 2016 12:43:08 -0400

Hi Rallavagu,

The Jersey client can be initialized with the underlying Apache client.
I've been trying to build the optimal configuration method. Note that in
my code I need both the HttpClient and the JerseyClient so thats part of
why I didn't do everything fluent, because I need some of those builder
classes. Also note that I use the same value for connectTimeout and
connectionRequestTimeout, but you could certainly split those out to be
two separate parameters.

Anyway here's my code for using the latest Apache client (4.5.2) with
Jersey (2.22). Hope this helps!

         HttpHost proxy = null;
         RequestConfig.Builder requestConfigBuilder =
         HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

         // Create our SSL handler for the gw URL - if necessary
         if (sslCtx != null) {
   "Creating custom ConnectionSocketFactory for SSL");
             Registry<ConnectionSocketFactory> socketFactoryRegistry =
RegistryBuilder.<ConnectionSocketFactory> create()
                     .register("https", new
             cm = new
         } else
             cm = new PoolingHttpClientConnectionManager();

         // Configure Pooling Connection Manager
         // TODO (67) implement monitor threads for eviction and
keepalive as per


         // Start to configure RequestConfiguration
         if (pc.getConnectionTimeout() > -1) {
   "Setting connection timeout to {} ms",
                     .setConnectTimeout(pc.getConnectionTimeout()) //
connection timeout
.setConnectionRequestTimeout(pc.getConnectionTimeout()); // timeout
waiting for connection from pool
         if (pc.getSocketTimeout() > -1) {
   "Setting socket read timeout to {} ms",
         // Configure the proxy if specified
         if (pc.isProxyEnable()) {
             try {
                 new URL(pc.getProxyScheme() + "://" + pc.getProxyHost()
+ ":" + pc.getProxyPort());
                 proxy = new HttpHost(pc.getProxyHost(),
pc.getProxyPort(), pc.getProxyScheme());
             } catch (MalformedURLException me) {
                 logger.error("Invalid parameters were provided for the
proxy, no proxy will be used: {}", me.toString());
         if (proxy != null) {
   "Setting proxy URL to {}", proxy);
             // DefaultProxyRoutePlanner routePlanner = new
             // httpClientBuilder.setRoutePlanner(routePlanner);
         } else
             logger.debug("Not using a proxy");

         // Build the final HTTP client
         requestConfig =;
         httpclient =

         // Configure the Jersey client connection handler with the
Apache client
         // All Apache custom configuration is maintained within the
connection manager and requestconfig
         // These must be conveyed to the Jersey client
         ClientConfig cc = new ClientConfig().connectorProvider(new
                 .property(ApacheClientProperties.CONNECTION_MANAGER, cm)
         jerseyClient = ClientBuilder.newClient(cc);


> Rallavagu <>
> June 15, 2016 at 12:08 PM
> Jersey version 2.22
> Is is possible to set timeout (both read and socket) per request
> without having to create a Client for each request? I am currently
> using a shared Client.
> Thanks