IdleStateHandler appears to trigger continuously

B.L. Zeebub roger.varley at googlemail.com
Mon May 16 03:04:46 EDT 2011


Hi

Have I misunderstood or mis-configured the use of the idleStateHandler? I
thought it should trigger only when there has been neither a read or a write
for the defined interval. What I'm seeing is the idle state event is being
fired every interval regardless of whether or not there is traffic going
through my pipeline. (I see the message "IdleStateHandler invoked: "
approximately every 3000 ms) I see this in 3.2.3 and 3.2.4.

1) My start-up class.

public class NettyClientService implements Runnable {

	private static final Logger logger = Logger
	.getLogger(NettyClientService.class.getName());
	
	private Channel channel;

	private String host;

	private CommandExecutor executor;
	
	public NettyClientService(String host, CommandExecutor executor) {
		this.host = host;
		this.executor = executor;
	}
	
	public void run() {
		
		logger.info("Trying to connect to host " + host); 
				
		ClientBootstrap bootstrap = new ClientBootstrap(new
OioClientSocketChannelFactory(Executors
				.newCachedThreadPool()));
		
		// Set up the event pipeline factory.
		Timer timer = new HashedWheelTimer();
		bootstrap.setPipelineFactory(new ClientPipelineFactory(executor,timer));
		bootstrap.setOption("keepAlive", true);
		
		// Make a new connection.
		ChannelFuture future = bootstrap.connect(new InetSocketAddress(
				host, 5956));

		channel = future.awaitUninterruptibly().getChannel();
		if (!channel.isConnected()) {
			
			logger.error("Failed to connect to " + host);
			executor.close();
			bootstrap.releaseExternalResources();
			return;
			}
		
		logger.info("Connected to " + host);
	}

	public void shutdown() {
		logger.info("Channel Close has been called on ClientService.shutdown()");
		channel.close();
	}

	
}

2) My PipelineFactory

public class ClientPipelineFactory implements ChannelPipelineFactory {

	private CommandExecutor executor;
	private Timer timer;

	public ClientPipelineFactory(CommandExecutor executor, Timer timer) {
		this.executor = executor;
		this.timer = timer;
	}

	@Override
	public ChannelPipeline getPipeline() throws Exception {
		
		ChannelPipeline pipeline = Channels.pipeline();
		
		pipeline.addLast("ObjectEncoder", new ObjectEncoder());
		pipeline.addLast("ObjectDecoder", new ObjectDecoder());
		pipeline.addLast("IdleStateHandler", new IdleStateHandler(timer,0,0,3));
		pipeline.addLast("TimeOutHandler", new IdleStateTimeoutHandler());
		pipeline.addLast("ChannelHandler",new ChannelExecutor(executor));
		return pipeline;
	}

}

3) My timeout handler class

public class IdleStateTimeoutHandler extends IdleStateAwareChannelHandler {

    long lastinvocation;	
	
	@Override
    public void channelIdle(ChannelHandlerContext ctx, IdleStateEvent e) {
        if (e.getState() == IdleState.ALL_IDLE) {
        	//TODO remove
        	long now = System.currentTimeMillis();
        	System.out.println("IdleStateHandler invoked: " + (now -
lastinvocation));
        	lastinvocation = now;
            e.getChannel().write(new Heartbeat());
        }
    }
}

--
View this message in context: http://netty-forums-and-mailing-lists.685743.n2.nabble.com/IdleStateHandler-appears-to-trigger-continuously-tp6367688p6367688.html
Sent from the Netty User Group mailing list archive at Nabble.com.


More information about the netty-users mailing list