hc-httpclient-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Gajo Csaba <csaba.g...@cosylab.com>
Subject Re: How to manage timeouts?
Date Wed, 17 Mar 2010 13:59:35 GMT
OK I've found a solution which works. I'm not sure if this is the best, 
but so far it works.

I wrote a custom class which extends BufferingHttpClientHandler, and 
overrode its timeout() method.

public void timeout(final NHttpClientConnection conn) {
      HttpContext context = conn.getContext();
      conn.setSocketTimeout(5000);
      conn.requestOutput();
}

I basically took the source code from AsyncNHttpClientHandler and 
removed the bits that referred to the internal ClientConnState class...

Regards, Csaba



On 03/17/2010 02:00 PM, Gajo Csaba wrote:
> Hello,
>
> I'm trying to send a HTTP GET to a host which may take a long time to 
> reply (tens of minutes). The protocol is simple. If the connection 
> timeouts, I just send the same request again. Sooner or later it will 
> reply to me.
>
> What I'm having problems with is how to recreate a connection. I've 
> looked at the examples on the website and have spent hours on trying 
> to make it work, but in the end I think I'm stuck.
>
> This is what I have. It's based on the non-blocking HTTP client 
> example, found at [1].
>
> handler.setEventListener(new MyEventListener(ioReactor, requestCount));
>
> Thread t = new Thread(new Runnable() {
>    public void run() {
>         try {
>              ioReactor.execute(ioEventDispatch);
>         }
>    ....
> }
> t.start();
>
> // -------- here comes the relevant part ----------
> SessionRequest session = ioReactor.connect(new InetSocketAddress(host, 
> port), null, new HttpHost(host), new MySessionCallback());
> // --------------------------------------------------------------
>
> So it's completely the same as the example at [1]. I've implemented 
> the custom EventListener, and the custom SessionRequestCallback.
>
> What I want to do now is detect when the connection times out. First, 
> let's look at the event listener.
>
> public void connectionTimeout(final NHttpConnection conn) {
>    System.out.println("Connection timed out: " + conn);
> }
>
> Works as expected. Except, I can't get anything useful from the conn 
> object, such as host, port, URL etc. The headers are null.
> I would need to have the host, port and URL to be able to recreate the 
> connection object, and schedule it with the ioReactor.
>
> Now let's look at the session request callback object.
>
> public void completed(SessionRequest request) {
>    System.err.println("completed: " + request);
> }
>
> public void timeout(SessionRequest request) {
>    System.err.println("timeout: " + request);
> }
>
> As soon as the connection is open, the completed() method is executed. 
> The request is NOT completed! I haven't yet received a reply from the 
> server! So I'm not really sure what this method is doing, is it only 
> used until the request is dispatched to the server? Which I don't care 
> for.
> The timeout() method is never invoked.
>
> If I'm right about what this callback object is doing, then it is only 
> used until the request is sent to the server. In that case, what I 
> would need is a "response" callback object. Is there such a thing?
>
> ---------
>
> Maybe I'm doing things wrong. What I would basically like to do is:
>
> 1) ioReactor.connect(new InetSocketAddress(host, port), null, new 
> HttpHost(host), new MySessionCallback());
> 2) in MySessionCallback:
>    if (timeout) {
>       ioReactor.connect(conn.getSockAddress(), null, conn.getHost(), 
> this);
>    }
> 3) loop until completed successfully
>
> -------
>
> What should I do to make this work?
>
> Thanks, Csaba
>
>
> 1 - 
> http://svn.apache.org/repos/asf/httpcomponents/httpcore/branches/4.0.x/httpcore-nio/src/examples/org/apache/http/examples/nio/NHttpClient.java

>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
> For additional commands, e-mail: httpclient-users-help@hc.apache.org
>


---------------------------------------------------------------------
To unsubscribe, e-mail: httpclient-users-unsubscribe@hc.apache.org
For additional commands, e-mail: httpclient-users-help@hc.apache.org


Mime
View raw message