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 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);