9

I'm calling a webservice using RestEasy Client. One requirement is to abort/timeout the call if it runs for more that 5 seconds. How would I achieve this with RestEasy Client? I have only seen server side timeout, i.e. the Rest Easy websevice will timeout the request if it's not fulfilled within a certain time.

n002213f
  • 7,805
  • 13
  • 69
  • 105

5 Answers5

11

A RESTEasy client typically uses Apache HttpClient to handle the network conversation.

You can override the HttpClient properties with your own custom timeout parameters:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, connectionTimeoutMillis);
HttpConnectionParams.setSoTimeout(params, socketTimeoutMillis);

The first param allows you to specify timeout establishing the initial connection and the second allows you to specify the maximum period of time in which a socket will wait while no data is sent.

You can use the modified HttpClient to build your ClientExecutor:

ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);

Which can be used in turn to build a ClientRequest object. Or you can inject it into a RestClientProxyFactoryBean if you are using a Spring configuration for RESTEasy.

It's not exactly the same as an absolute 5 second timeout, but depending on what you are trying to accomplish, tweaking these two properties will usually fill the bill.

Carter Page
  • 2,645
  • 3
  • 18
  • 14
3

If you prefer the builder pattern here is how you do it:

 Client client = new ResteasyClientBuilder()
            .establishConnectionTimeout(5, TimeUnit.SECONDS)
            .socketTimeout(5, TimeUnit.SECONDS)
            .build();

taken from here: http://blog.eisele.net/2014/12/setting-timeout-for-jax-rs-20-resteasy-client.html

Malik Atalla
  • 193
  • 8
2

The answer by Carter Page is correct for Apache HttpClient version >= 4.0.

For earlier versions of HttpClient (e.g. 3.1) the code is slightly different:

HttpClient httpClient = new HttpClient();
HttpConnectionParams params = httpClient.getHttpConnectionManager().getParams();
params.setConnectionTimeout(connectionTimeoutMillis);
params.setSoTimeout(socketTimeoutMillis);

ClientExecutor executor = new ApacheHttpClientExecutor(httpClient);
MyService service = ProxyFactory.create(MyService.class, URL, executor);
shonky linux user
  • 6,131
  • 4
  • 46
  • 73
0

If you are using resteasy client framework with spring integration (documentation), the following is the way to set timeout values:

<bean id="httpClient" class="org.apache.http.impl.client.DefaultHttpClient">
    <property name="params" ref="httpParams"/>
</bean>

<bean id="httpParams" class="org.apache.http.params.BasicHttpParams"/>

<bean id="httpConnectionParams" class="org.apache.http.params.HttpConnectionParamBean">
    <constructor-arg ref="httpParams"/>
    <property name="connectionTimeout" value="10000"/>
    <property name="soTimeout" value="30000"/>
</bean>
0

Given that both establishConnectionTimeout and socketTimeout are deprecated.

With this explanation on jboss v7.3 by redhat website :

The following ClientBuilder specification-compliant methods replace certain deprecated RESTEasy methods:

  • The connectTimeout method replaces the establishConnectionTimeout method.

    • The connectTimeout method determines how long the client must wait when making a new server connection.
  • The readTimeout method replaces the socketTimeout method.

    • The readTimeout method determines how long the client must wait for a response from the server.

This worked for me with RestEASY 3.12.1.Final:

    private Client clientBuilder() {
        return new ResteasyClientBuilder()
            .connectTimeout(2, TimeUnit.SECONDS)
            .readTimeout(10, TimeUnit.SECONDS)
            .build();
    }
g.momo
  • 536
  • 2
  • 7