You are viewing a plain text version of this content. The canonical link for it is here.
Posted to httpclient-users@hc.apache.org by Gajo Csaba <cs...@cosylab.com> on 2010/03/17 14:00:04 UTC

How to manage timeouts?

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


Re: How to fail immediately on broken connection?

Posted by Gajo Csaba <cs...@cosylab.com>.
OK now for some reason the SessionRequestCallback object is working, and 
I was able to fix the problem.

Csaba


On 03/18/2010 11:58 AM, Gajo Csaba wrote:
> Hello,
>
> I was suing the asynchronous client example from [1] to write a simple 
> client. The problem is, when the server is not online, the connection 
> just halts and waits, due to the CountDownLatch. I thought that the 
> SessionRequestCallback object was supposed to deal with this? However, 
> when I try to connect, the completed() method immediately returns 
> success is true, and I'm left hanging. The failed() method is never 
> invoked. Why is that?
>
> Basically, what I want to do is, if the server is not available, to 
> just print out some message. How to do that?
>
> 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


How to fail immediately on broken connection?

Posted by Gajo Csaba <cs...@cosylab.com>.
Hello,

I was suing the asynchronous client example from [1] to write a simple 
client. The problem is, when the server is not online, the connection 
just halts and waits, due to the CountDownLatch. I thought that the 
SessionRequestCallback object was supposed to deal with this? However, 
when I try to connect, the completed() method immediately returns 
success is true, and I'm left hanging. The failed() method is never 
invoked. Why is that?

Basically, what I want to do is, if the server is not available, to just 
print out some message. How to do that?

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


Re: How to manage timeouts?

Posted by Oleg Kalnichevski <ol...@apache.org>.
On Thu, 2010-03-18 at 09:19 +0100, Gajo Csaba wrote:
> On 03/17/2010 10:04 PM, Oleg Kalnichevski wrote:
> > Gajo Csaba wrote:
> >> 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
> >>
> >
> > Please note, though, that in this case you simply reset the connection 
> > timeout and the connection effectively never times out. This begs the 
> > question why you need to set timeout at all
> >
> > Oleg
> 
> I think by default all sockets have some default timeout, no?
> 
> Csaba
> 
> 

No, not really. The default socket timeout value is 0 (no timeout)

Oleg


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


Re: How to manage timeouts?

Posted by Gajo Csaba <cs...@cosylab.com>.
On 03/17/2010 10:04 PM, Oleg Kalnichevski wrote:
> Gajo Csaba wrote:
>> 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
>>
>
> Please note, though, that in this case you simply reset the connection 
> timeout and the connection effectively never times out. This begs the 
> question why you need to set timeout at all
>
> Oleg

I think by default all sockets have some default timeout, no?

Csaba



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


Re: How to manage timeouts?

Posted by Oleg Kalnichevski <ol...@apache.org>.
Gajo Csaba wrote:
> 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
> 

Please note, though, that in this case you simply reset the connection 
timeout and the connection effectively never times out. This begs the 
question why you need to set timeout at all

Oleg

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


Re: How to manage timeouts?

Posted by Gajo Csaba <cs...@cosylab.com>.
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