Cannot set timeout on Resteasy Client on JBoss

2019-07-13 03:18发布

问题:

I am trying to create a rest client on a JBoss EAP server, and I need to make sure that a timeout will end the connection if it gets stuck.

My code looks like this:

int timeout = 5000;
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).setConnectTimeout(timeout).build();
HttpClientBuilder builder = HttpClientBuilder.create();
HttpClient httpClient = builder.setDefaultRequestConfig(defaultRequestConfig).build();
// Previous try
// HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig).build();

ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);        

ClientRequest cr = new ClientRequest("http://www.someserviceurl.com/", executor);
JSONObject jData = new JSONObject();

jData.put("param1", param1);
jData.put("param2", param2);

cr.accept("application/json");
cr.body("application/json", jData.toString());

ClientResponse<String> cres = cr.post(String.class);

Currently this code does not get a timeout, but just gets stuck forever. What am I doing wrong?

回答1:

Offical answer from Jboss Support is to Use a config builder and apply it implementing the method loadHttpMethod in class ApacheHttpClient4Executor. Here's an example of a client with a few configuration:

import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ProxyFactory;
import org.jboss.resteasy.client.core.executors.ApacheHttpClient4Executor;

// ......

RequestConfig.Builder requestBuilder = RequestConfig.custom();
requestBuilder.setConnectTimeout(100);
requestBuilder.setSocketTimeout(100);
requestBuilder.setConnectionRequestTimeout(100);
final RequestConfig config = requestBuilder.build();

HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
httpClientBuilder.setDefaultRequestConfig(config);

CloseableHttpClient client = httpClientBuilder.build();

ApacheHttpClient4Executor executor = new ApacheHttpClient4Executor(
        client) {
    @Override
    public void loadHttpMethod(ClientRequest request,
            HttpRequestBase httpMethod) throws Exception {
        httpMethod.setConfig(config);
        super.loadHttpMethod(request, httpMethod);
    }
};

// use it in your ClientRequest or Proxy interface
final FooService fooService = ProxyFactory.create(FooService.class, host, executor);


回答2:

Apparently this is a known issue with RestEasy that was fixed on version 3.0.9.Final.

You can see the bug report here.

To fix it, upgrade RestEasy or use the deprecated DefaultHttpClient:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params, timeout);
HttpConnectionParams.setSoTimeout(params, timeout);
ClientExecutor executor = new ApacheHttpClient4Executor(httpClient);

ClientRequest cr = new ClientRequest("http://www.someserviceurl.com/", executor);