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?
Apparently this is a known issue with
RestEasy
that was fixed on version3.0.9.Final
.You can see the bug report here.
To fix it, upgrade
RestEasy
or use the deprecatedDefaultHttpClient
: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: