ReadTimeoutHandler and WriteTimeoutHandler

Christian Migowski chrismfwrd at gmail.com
Fri Apr 17 09:55:10 EDT 2009


Hi,

my understanding (and a very brief glance at the code) of this
handlers is that they are only intended for established connections
(you can see in the code they are initialized in channelOpen), not for
creating connections.

They are pretty easy to use:
1. put them into your pipeline, eg.

	pipeline.addLast("idlehandler", new IdleStateHandler(timer,10,5,0));
	pipeline.addLast("readTimeout", new ReadTimeoutHandler(timer, 10,
TimeUnit.SECONDS));
	pipeline.addLast("writeTimeout", new WriteTimeoutHandler(timer, 5,
TimeUnit.SECONDS));

2. do whatever you need to do in your handler on the events they create, eg.

//for the *TimeoutHandlers
    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) {
	if (e.getCause() instanceof WriteTimeoutException) {
	    System.out.println(new
SimpleDateFormat("HH:mm:ss.SSS").format(new Date()) + "
writetimeout");
	} else if (e.getCause() instanceof ReadTimeoutException) {
	    System.out.println(ctx.getChannel());
	    System.out.println(new
SimpleDateFormat("HH:mm:ss.SSS").format(new Date()) + " readtimeout");
	} else {
	    e.getCause().printStackTrace();
	    Channel ch = e.getChannel();
	    ch.close().awaitUninterruptibly();
	    System.out.println("channel closed");
	}
    }

//for the IdleStateHandler
    public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent
e) throws Exception {
	if (e instanceof IdleStateEvent) {
	    System.out.println(new
SimpleDateFormat("HH:mm:ss.SSS").format(new Date()) + " idleness
event: " + e);
	    if (((IdleStateEvent) e).getState().equals(IdleState.READER_IDLE)) {
		writeTime(e.getChannel());
	    }
	}
	super.handleUpstream(ctx, e);
    }


I tried to create a handler which does what you want but given that
Netty channels are not reconnectable I didn't pursue this further
because something like this as a handler is only useful (imho) if it
is also capable of reconnecting broken connections.

something like this should to the trick (tcp example, also better
error checking should be done):

        do {
        ChannelFuture f = bootstrap.connect(new InetSocketAddress(host, port));
        f.awaitUninterruptibly();
        ch = f.getChannel();
        }while(!ch.isConnected());


hth,
christian!



On Fri, Apr 17, 2009 at 3:28 PM, hezjing <hezjing at gmail.com> wrote:
> Hi
> I want to develop a UDP client, when started, it will keep trying to connect
> to a server until the server is started, and accepted the connection.
> I guess the ReadTimeoutHandler and WriteTimeoutHandler can help on this (?)
> but these classes are lack of Javadoc.
> Any example of these timeout handlers work?
>
> Thank you!
>
> --
>
> Hez
>
> _______________________________________________
> netty-users mailing list
> netty-users at lists.jboss.org
> https://lists.jboss.org/mailman/listinfo/netty-users
>
>




More information about the netty-users mailing list