Search code examples
jax-rscxfjax-ws

Jaxrs ability to set a global timeout (connect + read)


In Jaxrs (WebClient for instance) we can set a connect timeout and a read timeout.

    ClientConfiguration c = WebClient.getConfig(client);
    HTTPConduit http = c.getHttpConduit();
    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    httpClientPolicy.setConnectionTimeout(timeout);
    httpClientPolicy.setReceiveTimeout(timeout);
    httpClientPolicy.setAllowChunking(false);
    http.setClient(httpClientPolicy);

I would like to set a timeout that includes both, I don't really care how much time is spent in connecting or in receiving, my requirement is to get a response in X seconds or just discard the search.


Solution

  • There is no way with CXF to set a maximum timeout for a request that consider both connection and receive durations. The maximum timeout for a request will be:

    maximum_timeout = connection_timeout + receive_timeout
    

    See this similar question for Apache HTTP client. The workaround could be to set a timer in a separate Thread to abort the connection when the desired maximum timeout expires