org.jboss.netty.handler.timeout
Class ReadTimeoutHandler

java.lang.Object
  extended by org.jboss.netty.channel.SimpleChannelUpstreamHandler
      extended by org.jboss.netty.handler.timeout.ReadTimeoutHandler
All Implemented Interfaces:
ChannelHandler, ChannelUpstreamHandler, LifeCycleAwareChannelHandler, ExternalResourceReleasable

@ChannelHandler.Sharable
public class ReadTimeoutHandler
extends SimpleChannelUpstreamHandler
implements LifeCycleAwareChannelHandler, ExternalResourceReleasable

Raises a ReadTimeoutException when no data was read within a certain period of time.

 public class MyPipelineFactory implements ChannelPipelineFactory {

     private final Timer timer;
     private final ChannelHandler timeoutHandler;

     public MyPipelineFactory(Timer timer) {
         this.timer = timer;
         this.timeoutHandler = new ReadTimeoutHandler(timer, 30), // timer must be shared.
     }

     public ChannelPipeline getPipeline() {
         // An example configuration that implements 30-second read timeout:
         return Channels.pipeline(
             timeoutHandler,
             new MyHandler());
     }
 }

 ServerBootstrap bootstrap = ...;
 Timer timer = new HashedWheelTimer();
 ...
 bootstrap.setPipelineFactory(new MyPipelineFactory(timer));
 ...
 
The Timer which was specified when the ReadTimeoutHandler is created should be stopped manually by calling releaseExternalResources() or Timer.stop() when your application shuts down.

Version:
$Rev: 2222 $, $Date: 2010-03-24 14:07:27 +0900 (Wed, 24 Mar 2010) $
Author:
The Netty Project, Trustin Lee
See Also:
WriteTimeoutHandler, IdleStateHandler

Nested Class Summary
 
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
 
Constructor Summary
ReadTimeoutHandler(Timer timer, int timeoutSeconds)
          Creates a new instance.
ReadTimeoutHandler(Timer timer, long timeout, TimeUnit unit)
          Creates a new instance.
 
Method Summary
 void afterAdd(ChannelHandlerContext ctx)
           
 void afterRemove(ChannelHandlerContext ctx)
           
 void beforeAdd(ChannelHandlerContext ctx)
           
 void beforeRemove(ChannelHandlerContext ctx)
           
 void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel was closed and all its related resources were released.
 void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
          Invoked when a Channel is open, but not bound nor connected.
 void messageReceived(ChannelHandlerContext ctx, MessageEvent e)
          Invoked when a message object (e.g: ChannelBuffer) was received from a remote peer.
protected  void readTimedOut(ChannelHandlerContext ctx)
           
 void releaseExternalResources()
          Stops the Timer which was specified in the constructor of this handler.
 
Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream, writeComplete
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ReadTimeoutHandler

public ReadTimeoutHandler(Timer timer,
                          int timeoutSeconds)
Creates a new instance.

Parameters:
timer - the Timer that is used to trigger the scheduled event. The recommended Timer implementation is HashedWheelTimer.
timeoutSeconds - read timeout in seconds

ReadTimeoutHandler

public ReadTimeoutHandler(Timer timer,
                          long timeout,
                          TimeUnit unit)
Creates a new instance.

Parameters:
timer - the Timer that is used to trigger the scheduled event. The recommended Timer implementation is HashedWheelTimer.
timeout - read timeout
unit - the TimeUnit of timeout
Method Detail

releaseExternalResources

public void releaseExternalResources()
Stops the Timer which was specified in the constructor of this handler. You should not call this method if the Timer is in use by other objects.

Specified by:
releaseExternalResources in interface ExternalResourceReleasable

beforeAdd

public void beforeAdd(ChannelHandlerContext ctx)
               throws Exception
Specified by:
beforeAdd in interface LifeCycleAwareChannelHandler
Throws:
Exception

afterAdd

public void afterAdd(ChannelHandlerContext ctx)
              throws Exception
Specified by:
afterAdd in interface LifeCycleAwareChannelHandler
Throws:
Exception

beforeRemove

public void beforeRemove(ChannelHandlerContext ctx)
                  throws Exception
Specified by:
beforeRemove in interface LifeCycleAwareChannelHandler
Throws:
Exception

afterRemove

public void afterRemove(ChannelHandlerContext ctx)
                 throws Exception
Specified by:
afterRemove in interface LifeCycleAwareChannelHandler
Throws:
Exception

channelOpen

public void channelOpen(ChannelHandlerContext ctx,
                        ChannelStateEvent e)
                 throws Exception
Description copied from class: SimpleChannelUpstreamHandler
Invoked when a Channel is open, but not bound nor connected.
Be aware that this event is fired from within the Boss-Thread so you should not execute any heavy operation in there as it will block the dispatching to other workers!

Overrides:
channelOpen in class SimpleChannelUpstreamHandler
Throws:
Exception

channelClosed

public void channelClosed(ChannelHandlerContext ctx,
                          ChannelStateEvent e)
                   throws Exception
Description copied from class: SimpleChannelUpstreamHandler
Invoked when a Channel was closed and all its related resources were released.

Overrides:
channelClosed in class SimpleChannelUpstreamHandler
Throws:
Exception

messageReceived

public void messageReceived(ChannelHandlerContext ctx,
                            MessageEvent e)
                     throws Exception
Description copied from class: SimpleChannelUpstreamHandler
Invoked when a message object (e.g: ChannelBuffer) was received from a remote peer.

Overrides:
messageReceived in class SimpleChannelUpstreamHandler
Throws:
Exception

readTimedOut

protected void readTimedOut(ChannelHandlerContext ctx)
                     throws Exception
Throws:
Exception


Copyright © 2008-2011 JBoss, a division of Red Hat, Inc.. All Rights Reserved.