Specifying Accept Timeouts on Server Sockets

Sai Pullabhotla sai.pullabhotla at jmethods.com
Fri Aug 13 11:57:57 EDT 2010


I think I got it to work now. Just to make sure I'm doing it the
correct way, below is the example code. If it should have been done
differently, please let me know.

import java.net.InetSocketAddress;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import org.jboss.netty.bootstrap.ServerBootstrap;
import org.jboss.netty.channel.Channel;
import org.jboss.netty.channel.ChannelHandlerContext;
import org.jboss.netty.channel.ChannelStateEvent;
import org.jboss.netty.channel.ChildChannelStateEvent;
import org.jboss.netty.channel.MessageEvent;
import org.jboss.netty.channel.socket.ServerSocketChannelFactory;
import org.jboss.netty.channel.socket.nio.NioServerSocketChannelFactory;
import org.jboss.netty.handler.timeout.IdleState;
import org.jboss.netty.handler.timeout.IdleStateAwareChannelHandler;
import org.jboss.netty.handler.timeout.IdleStateEvent;
import org.jboss.netty.handler.timeout.IdleStateHandler;
import org.jboss.netty.util.HashedWheelTimer;
import org.jboss.netty.util.Timer;

public class NettyAcceptTimeoutTest {

	public static void main(String[] args) {
		ExecutorService service = Executors.newCachedThreadPool();
		ServerSocketChannelFactory factory = new NioServerSocketChannelFactory(
			service, service);
		ServerBootstrap bootstrap = new ServerBootstrap(factory);
		HashedWheelTimer idleTimer = new HashedWheelTimer();
		//Here is where we handle accept timeouts
		bootstrap.setParentHandler(new ParentHandler(idleTimer, 0, 0, 10));
		//Here is where we handle read/write timeouts on child channels
		bootstrap.getPipeline().addLast("idleHandler",
			new IdleStateHandler(idleTimer, 60, 30, 30));
		bootstrap.getPipeline().addLast("handler", new ChildHandler());
		bootstrap.bind(new InetSocketAddress(3000));
	}

	private static class ParentHandler extends IdleStateHandler {

		public ParentHandler(Timer timer, int readerIdleTimeSeconds,
			int writerIdleTimeSeconds, int allIdleTimeSeconds) {
			super(timer, readerIdleTimeSeconds, writerIdleTimeSeconds,
				allIdleTimeSeconds);
		}

		@Override
		protected void channelIdle(ChannelHandlerContext ctx, IdleState state,
			long lastActivityTimeMillis) throws Exception {
			super.channelIdle(ctx, state, lastActivityTimeMillis);
			System.out.println("Acceptor Idle, unbinding " + state);
			ctx.getChannel().close();
		}

		@Override
		public void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e)
			throws Exception {
			System.out.println("Channel bound");
		}

		@Override
		public void channelUnbound(ChannelHandlerContext ctx,
			ChannelStateEvent e) throws Exception {
			System.out.println("Channel unbound");
		}

		@Override
		public void childChannelOpen(ChannelHandlerContext ctx,
			ChildChannelStateEvent e) throws Exception {
			System.out.println("Child channel opened");
		}

		@Override
		public void childChannelClosed(ChannelHandlerContext ctx,
			ChildChannelStateEvent e) throws Exception {
			System.out.println("Child channel closed");
		}

	}

	private static class ChildHandler extends IdleStateAwareChannelHandler {

		@Override
		public void channelConnected(ChannelHandlerContext ctx,
			ChannelStateEvent e) throws Exception {
			System.out.println("Child Channel connected");
		}

		@Override
		public void channelDisconnected(ChannelHandlerContext ctx,
			ChannelStateEvent e) throws Exception {
			System.out.println("Child Channel Disconnected");
		}

		@Override
		public void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
			throws Exception {
			System.out.println("Message Received");
			e.getChannel().write(e.getMessage());
		}

		@Override
		public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e)
			throws Exception {
			System.out.println("Child Channel Idle, closing connection");
			e.getChannel().close();
		}
	}

}

On Fri, Aug 13, 2010 at 10:01 AM, Sai Pullabhotla
<sai.pullabhotla at jmethods.com> wrote:
> Well, that's what I thought, but it did not work. It looks like the
> IdleStateHandlers are working only for read and write timeouts, but
> not for accept timeouts.
>
> Correct me if I'm wrong, but I think the accept timeouts should be
> handled by the parent handler of a Bootstrap, not by the channel
> pipeline that the child channels use. Is this correct? Could some one
> give me some hints on how to get this working?
>
> 2010/8/13 Marc-André Laverdière <marcandre.laverdiere at gmail.com>:
>> You could always have a timeout handler.
>>
>> Marc-André LAVERDIÈRE
>> "Perseverance must finish its work so that you may be mature and
>> complete, not lacking anything." -James 1:4
>> mlaverd.theunixplace.com/blog
>>
>>  /"\
>>  \ /    ASCII Ribbon Campaign
>>   X      against HTML e-mail
>>  / \
>>
>>
>>
>> 2010/8/12 Sai Pullabhotla <sai.pullabhotla at jmethods.com>:
>>> Hello,
>>>
>>> Is there a way to specify accept timeouts on server sockets? For
>>> example, an FTP server would typically listen on a random port in
>>> response to a PASV command from the client. If for some reason, the
>>> client does not open the connection to this passive port in a
>>> specified amount of time, I would like to shutdown the listener and
>>> free up the port/resources. Java's plain ServerSockets make use of the
>>> SO_TIMEOUT methods to achieve this. But, I did not find any equivalent
>>> in Netty. Can some one tell me if this is supported and steer me in
>>> the right direction?
>>>
>>> Thanks in advance for your help.
>>> Sai.
>>> _______________________________________________
>>> netty-users mailing list
>>> netty-users at lists.jboss.org
>>> https://lists.jboss.org/mailman/listinfo/netty-users
>>>
>>
>> _______________________________________________
>> 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