users@jersey.java.net

[Jersey] Re: Jersey 2 per request timeout

From: Richard Sand <rsand_at_idfconnect.com>
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 =
RequestConfig.custom();
         HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();

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

         // Configure Pooling Connection Manager
         // TODO (67) implement monitor threads for eviction and
keepalive as per
https://hc.apache.org/httpcomponents-client-4.5.x/tutorial/html/connmgmt.html
         cm.setDefaultMaxPerRoute(pc.getMaxConnections());
         cm.setMaxTotal(pc.getMaxConnections());

         httpClientBuilder.setConnectionManager(cm);

         // Start to configure RequestConfiguration
         if (pc.getConnectionTimeout() > -1) {
             logger.info("Setting connection timeout to {} ms",
pc.getConnectionTimeout());
             requestConfigBuilder
                     .setConnectTimeout(pc.getConnectionTimeout()) //
connection timeout
                     
.setConnectionRequestTimeout(pc.getConnectionTimeout()); // timeout
waiting for connection from pool
         }
         if (pc.getSocketTimeout() > -1) {
             logger.info("Setting socket read timeout to {} ms",
pc.getSocketTimeout());
             requestConfigBuilder.setSocketTimeout(pc.getSocketTimeout());
         }
         // 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) {
             logger.info("Setting proxy URL to {}", proxy);
             requestConfigBuilder.setProxy(proxy);
             // DefaultProxyRoutePlanner routePlanner = new
DefaultProxyRoutePlanner(proxy);
             // httpClientBuilder.setRoutePlanner(routePlanner);
         } else
             logger.debug("Not using a proxy");

         // Build the final HTTP client
         requestConfig = requestConfigBuilder.build();
         httpclient =
httpClientBuilder.setDefaultRequestConfig(requestConfig).build();

         // 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
ApacheConnectorProvider())
                 .register(JSonGatewayMessageBodyProvider.class)
                 .property(ApacheClientProperties.CONNECTION_MANAGER, cm)
                 .property(ApacheClientProperties.REQUEST_CONFIG,
requestConfig);
         jerseyClient = ClientBuilder.newClient(cc);



-Richard

> Rallavagu <mailto:rallavagu_at_gmail.com>
> 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