users@jersey.java.net

[Jersey] Re: Jersey 2 per request timeout

From: Marek Potociar <marek.potociar_at_oracle.com>
Date: Fri, 17 Jun 2016 11:47:44 +0200

Changing the property is going to update the configuration as defined in the references JAX-RS apidocs.
As for Jersey client though, this change will trigger a creation of a new client runtime, because the client runtime in Jersey is immutable. So you are not going to save a lot by this change in terms of client reuse.

Marek

> On 15 Jun 2016, at 19:28, Rallavagu <rallavagu_at_gmail.com> wrote:
>
> As per the javadoc
>
> https://jersey.java.net/apidocs/latest/jersey/javax/ws/rs/core/Configurable.html#property(java.lang.String,%20java.lang.Object)
>
> "Set the new configuration property, if already set, the existing value of the property will be updated. Setting a null value into a property effectively removes the property from the property bag."
>
> Sounds like following line would overwrite the existing value. But could someone confirm if this can help with re-using the client?
>
> jerseyClient.property(ClientProperties.CONNECT_TIMEOUT, connectionTimeout);
>
> On 6/15/16 10:07 AM, Richard Sand wrote:
>> I have separate jerseyClient and httpClient objects but they both use
>> the same connection manager so I believe it will be the same connection
>> pool. Instead of setting those timeouts on the jerseyClient, set the
>> timeouts on the apache clientConfig and requestConfig objects as I did
>> in the code snippet and then those get passed into jersey.
>>
>> Best regards,
>>
>> Richard
>>
>>> Rallavagu <mailto:rallavagu_at_gmail.com>
>>> June 15, 2016 at 12:51 PM
>>> 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.
>>>
>>>
>>>
>>> Richard Sand <mailto:rsand_at_idfconnect.com>
>>> June 15, 2016 at 12:43 PM
>>> 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
>>>
>>>
>>