How to detect connection timeout on client side?

XuQing Tan missedone at gmail.com
Mon Sep 21 21:26:35 EDT 2009


Hi, folks,

I'm recently investigating some NIO framework, and I find that Netty is
awesome, I'd like to adopt it into our project.
But here's my first question: how to detect the conenction timeout on client
side?
As the secureChat example shown, client connects to host via code:

*ChannelFuture future = bootstrap.connect(new InetSocketAddress(host,
port));*

a *ChannelFuture* returned immediately, which tells the connection status,
e.g. canceled or done, *but no TIMEOUT*.

by digging some codes, I got this:

*org.jboss.netty.channel.socket.nio.NioClientSocketPipelineSink.Boss.processConnectTimeout(Set<SelectionKey>,
long)

        private void processConnectTimeout(Set<SelectionKey> keys, long
currentTimeNanos) {
            ConnectException cause = null;
            for (SelectionKey k: keys) {
                if (!k.isValid()) {
                    continue;
                }

                NioClientSocketChannel ch = (NioClientSocketChannel)
k.attachment();
                if (ch.connectDeadlineNanos > 0 &&
                        currentTimeNanos >= ch.connectDeadlineNanos) {

                    if (cause == null) {
                        cause = new ConnectException("connection timed
out");
                    }

                    ch.connectFuture.setFailure(cause);
                    fireExceptionCaught(ch, cause);
                    close(k);
                }
            }
        }*

so, from the view above, maybe we can test the timeout status int the *
ChannelFutureListener* via:

*new ChannelFutureListener() {

* *public void operationComplete(ChannelFuture future) {

**if (future.getCause() instanceof ConnectException) {
* *// deal with timeout
....
*
*}
*
*
}
*
*}*

is that correct? but a little weird, I think.



Thanks & Best Regards!

              ///
             (. .)
 --------ooO--(_)--Ooo--------
 |         Nick Tan          |
 -----------------------------
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.jboss.org/pipermail/netty-users/attachments/20090922/379d75ea/attachment-0001.html 


More information about the netty-users mailing list