|
From: Borut B. <bor...@gm...> - 2013-10-11 10:38:20
|
Hello,
what is wrong with the code below? When using RestEasy Proxy Framework it
seems the client is not using the configuration for connection timeouts.
At line 54 there is a test url which returns response after 7 seconds, but
the call is not aborted as one might expect.
package si.najdi.httpclient;
import javax.ws.rs.ProcessingException;
import javax.ws.rs.core.Response;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.config.SocketConfig;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.jboss.resteasy.client.jaxrs.ResteasyClient;
import org.jboss.resteasy.client.jaxrs.ResteasyClientBuilder;
import org.jboss.resteasy.client.jaxrs.ResteasyWebTarget;
import org.jboss.resteasy.client.jaxrs.engines.ApacheHttpClient4Engine;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Main {
static Logger logger = LoggerFactory.getLogger(Main.class);
public static void main(String[] args) {
logger.info("Starting HttpClient test.");
SocketConfig socketConfig = SocketConfig.custom()
.setTcpNoDelay(true)
.setSoKeepAlive(true)
.setSoReuseAddress(true)
.build();
PoolingHttpClientConnectionManager connManager = new
PoolingHttpClientConnectionManager();
connManager.setMaxTotal(100);
connManager.setDefaultMaxPerRoute(100);
connManager.setDefaultSocketConfig(socketConfig);
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(2000)
.setConnectTimeout(100)
.setConnectionRequestTimeout(3000)
.setStaleConnectionCheckEnabled(true)
.build();
CloseableHttpClient httpClient = HttpClients.custom()
.setDefaultRequestConfig(defaultRequestConfig)
.setConnectionManager(connManager)
.build();
ApacheHttpClient4Engine apacheHttpClient4Engine = new
ApacheHttpClient4Engine(httpClient);
ResteasyClient client = new
ResteasyClientBuilder().httpEngine(apacheHttpClient4Engine).build();
//String host = "http://httpstat.us/400";
String host = "http://fake-response.appspot.com/?sleep=7";
ResteasyWebTarget newsWebTarget = client.target(host);
NewsClient newsClient = newsWebTarget.proxy(NewsClient.class);
boolean ok = false;
Response response = null;
try {
logger.info("Calling...");
response = newsClient.clickIncrement("666");
logger.info("...returning");
if (response.getStatus() == Response.Status.OK.getStatusCode()) {
String line = (String) response.readEntity(String.class);
logger.info("Response line: " + line);
} else {
String failMessage = response.getStatusInfo().getStatusCode() + " " +
response.getStatusInfo().getReasonPhrase();
logger.warn("Failed call. Reason: " + failMessage);
}
} catch (ProcessingException e) {
logger.warn("Exception incrementing click counter." + e);
} finally {
if (response != null) {
response.close();
}
}
}
}
Here is the client interface if it matters:
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Response;
public interface NewsClient {
@GET
//@Path("clickcounter/news/{newsId}")
Response clickIncrement(@PathParam("newsId") @DefaultValue("123")
String newsId);
}
The log output:
[12:05:46] INFO [si.najdi.httpclient.Main]: Starting HttpClient test.
[12:05:47] DEBUG [org.jboss.resteasy.plugins.providers.DocumentProvider]:
Unable to retrieve config: expandEntityReferences defaults to true
[12:05:47] DEBUG [org.jboss.resteasy.plugins.providers.DocumentProvider]:
Unable to retrieve config: expandEntityReferences defaults to true
[12:05:47] INFO [si.najdi.httpclient.Main]: Calling...
[12:05:54] INFO [si.najdi.httpclient.Main]: ...returning
[12:05:54] INFO [si.najdi.httpclient.Main]: Response line:
{"response":"This request has finsihed sleeping for 7 seconds"}
Best regards,
borut
|