Then initialize the Reseasy client with the Http client you created in 
the example.

On 10/22/2013 11:27 AM, Borut Bolčina wrote:
> Hi, this simple program which uses the same httpclient 4.3 works as
> expected. The timeout is respected. After 4 seconds you
> get java.net.SocketTimeoutException: Read timed out.
>
> So the bug is not with httpclient.
>
>
> import java.io.IOException;
>
> import org.apache.commons.lang.time.StopWatch;
> import org.apache.http.client.ClientProtocolException;
> import org.apache.http.client.config.RequestConfig;
> import org.apache.http.client.methods.CloseableHttpResponse;
> import org.apache.http.client.methods.HttpGet;
> 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.slf4j.Logger;
> import org.slf4j.LoggerFactory;
>
> public class Main2 {
> static Logger logger = LoggerFactory.getLogger(Main2.class);
>
> public static void main(String[] args) {
> logger.info <http://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(4000)
>     .setConnectTimeout(2000)
>     .setConnectionRequestTimeout(1000)
>     .setStaleConnectionCheckEnabled(true)
>     .build();
> CloseableHttpClient httpClient = HttpClients.custom()
>     .setDefaultRequestConfig(defaultRequestConfig)
>     .setConnectionManager(connManager)
>     .build();
> String host = "http://fake-response.appspot.com/?sleep=7";;
> HttpGet httpget = new HttpGet(host);
> CloseableHttpResponse response = null;
> StopWatch sw = new StopWatch();
> try {
> sw.start();
> response = httpClient.execute(httpget);
> System.out.println(response.getProtocolVersion());
> System.out.println(response.getStatusLine().getStatusCode());
> System.out.println(response.getStatusLine().getReasonPhrase());
> System.out.println(response.getStatusLine().toString());
> } catch (ClientProtocolException e) {
> e.printStackTrace();
> } catch (IOException e) {
> e.printStackTrace();
> } finally {
>     try {
> sw.stop();
> System.out.println(sw);
> response.close();
> } catch (IOException e) {
> e.printStackTrace();
> }
> }
> }
> }
>
> Best regards,
> borut
>
>
> 2013/10/11 Bill Burke <bbu...@redhat.com <mailto:bbu...@redhat.com>>
>
>     Might be an Apache issue.  Maybe try the timeout without Resteasy and
>     just an Apache call?
>
>     On 10/11/2013 6:37 AM, Borut Bolčina wrote:
>      > 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 <http://logger.info> <http://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 <http://logger.info> <http://logger.info>("Calling...");
>      > response = newsClient.clickIncrement("666");
>      > logger.info <http://logger.info>
>     <http://logger.info>("...returning");
>      >
>      > if (response.getStatus() == Response.Status.OK.getStatusCode()) {
>      > String line = (String) response.readEntity(String.class);
>      > logger.info <http://logger.info> <http://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
>      >
>      >
>      >
>      >
>     
> ------------------------------------------------------------------------------
>      > October Webinars: Code for Performance
>      > Free Intel webinars can help you accelerate application performance.
>      > Explore tips for MPI, OpenMP, advanced profiling, and more. Get
>     the most from
>      > the latest Intel processors and coprocessors. See abstracts and
>     register >
>      >
>     
> http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk
>      >
>      >
>      >
>      > _______________________________________________
>      > Resteasy-users mailing list
>      > Resteasy-users@lists.sourceforge.net
>     <mailto:Resteasy-users@lists.sourceforge.net>
>      > https://lists.sourceforge.net/lists/listinfo/resteasy-users
>      >
>
>     --
>     Bill Burke
>     JBoss, a division of Red Hat
>     http://bill.burkecentral.com
>
>     
> ------------------------------------------------------------------------------
>     October Webinars: Code for Performance
>     Free Intel webinars can help you accelerate application performance.
>     Explore tips for MPI, OpenMP, advanced profiling, and more. Get the
>     most from
>     the latest Intel processors and coprocessors. See abstracts and
>     register >
>     
> http://pubads.g.doubleclick.net/gampad/clk?id=60134071&iu=/4140/ostg.clktrk
>     _______________________________________________
>     Resteasy-users mailing list
>     Resteasy-users@lists.sourceforge.net
>     <mailto:Resteasy-users@lists.sourceforge.net>
>     https://lists.sourceforge.net/lists/listinfo/resteasy-users
>
>

-- 
Bill Burke
JBoss, a division of Red Hat
http://bill.burkecentral.com

------------------------------------------------------------------------------
October Webinars: Code for Performance
Free Intel webinars can help you accelerate application performance.
Explore tips for MPI, OpenMP, advanced profiling, and more. Get the most from 
the latest Intel processors and coprocessors. See abstracts and register >
http://pubads.g.doubleclick.net/gampad/clk?id=60135991&iu=/4140/ostg.clktrk
_______________________________________________
Resteasy-users mailing list
Resteasy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/resteasy-users

Reply via email to