users@jersey.java.net

[Jersey] Re: Jersey 2 per request timeout

From: Rallavagu <rallavagu_at_gmail.com>
Date: Wed, 15 Jun 2016 09:51:23 -0700

Thanks Richard for the response. I have currently have something similar
to what you have posted. My application is spring based and I have
created spring bean for Client and PoolingHttpClientManager. But, would
like to set the timeout per request. Currently, I am doing this but
don't think it works.

jerseyClient.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout);
         jerseyClient.property(ClientProperties.READ_TIMEOUT,
readTimeout);

         Invocation.Builder builder =
jerseyClient.target(targetUrl).request(MediaType.APPLICATION_JSON);

Also, one thing I have noticed from your code is that you are trying to
use custom HttpClient. Is it working? I ask this because a
CloseableHttpClient is instantiated and used by ApacheConnector.


On 6/15/16 9:43 AM, Richard Sand wrote:
> 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
>