Package org.jboss.netty.handler.timeout
Class ReadTimeoutHandler
- java.lang.Object
-
- org.jboss.netty.channel.SimpleChannelUpstreamHandler
-
- org.jboss.netty.handler.timeout.ReadTimeoutHandler
-
- All Implemented Interfaces:
ChannelHandler,ChannelUpstreamHandler,LifeCycleAwareChannelHandler,ExternalResourceReleasable
@Sharable public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler implements LifeCycleAwareChannelHandler, ExternalResourceReleasable
Raises aReadTimeoutExceptionwhen no data was read within a certain period of time.public class MyPipelineFactory implements
TheChannelPipelineFactory{ private finalTimertimer; private finalChannelHandlertimeoutHandler; public MyPipelineFactory(Timertimer) { this.timer = timer; this.timeoutHandler = newReadTimeoutHandler(timer, 30), // timer must be shared. } publicChannelPipelinegetPipeline() { // An example configuration that implements 30-second read timeout: returnChannels.pipeline( timeoutHandler, new MyHandler()); } }ServerBootstrapbootstrap = ...;Timertimer = newHashedWheelTimer(); ... bootstrap.setPipelineFactory(new MyPipelineFactory(timer)); ...Timerwhich was specified when theReadTimeoutHandleris created should be stopped manually by callingreleaseExternalResources()orTimer.stop()when your application shuts down.- See Also:
WriteTimeoutHandler,IdleStateHandler
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Constructor Summary
Constructors Constructor Description ReadTimeoutHandler(Timer timer, int timeoutSeconds)Creates a new instance.ReadTimeoutHandler(Timer timer, long timeout, TimeUnit unit)Creates a new instance.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterAdd(ChannelHandlerContext ctx)voidafterRemove(ChannelHandlerContext ctx)voidbeforeAdd(ChannelHandlerContext ctx)voidbeforeRemove(ChannelHandlerContext ctx)voidchannelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelwas closed and all its related resources were released.voidchannelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelis open, but not bound nor connected.voidmessageReceived(ChannelHandlerContext ctx, MessageEvent e)Invoked when a message object (e.g:ChannelBuffer) was received from a remote peer.protected voidreadTimedOut(ChannelHandlerContext ctx)voidreleaseExternalResources()Stops theTimerwhich 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
-
-
-
-
Constructor Detail
-
ReadTimeoutHandler
public ReadTimeoutHandler(Timer timer, int timeoutSeconds)
Creates a new instance.- Parameters:
timer- theTimerthat is used to trigger the scheduled event. The recommendedTimerimplementation isHashedWheelTimer.timeoutSeconds- read timeout in seconds
-
-
Method Detail
-
releaseExternalResources
public void releaseExternalResources()
Stops theTimerwhich was specified in the constructor of this handler. You should not call this method if theTimeris in use by other objects.- Specified by:
releaseExternalResourcesin interfaceExternalResourceReleasable
-
beforeAdd
public void beforeAdd(ChannelHandlerContext ctx) throws Exception
- Specified by:
beforeAddin interfaceLifeCycleAwareChannelHandler- Throws:
Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws Exception
- Specified by:
afterAddin interfaceLifeCycleAwareChannelHandler- Throws:
Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws Exception
- Specified by:
beforeRemovein interfaceLifeCycleAwareChannelHandler- Throws:
Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws Exception
- Specified by:
afterRemovein interfaceLifeCycleAwareChannelHandler- Throws:
Exception
-
channelOpen
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when aChannelis open, but not bound nor connected.
Be aware that this event is fired from within the I/O thread. You should never execute any heavy operation in there as it will block the dispatching to other workers!- Overrides:
channelOpenin classSimpleChannelUpstreamHandler- Throws:
Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when aChannelwas closed and all its related resources were released.- Overrides:
channelClosedin classSimpleChannelUpstreamHandler- Throws:
Exception
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when a message object (e.g:ChannelBuffer) was received from a remote peer.- Overrides:
messageReceivedin classSimpleChannelUpstreamHandler- Throws:
Exception
-
readTimedOut
protected void readTimedOut(ChannelHandlerContext ctx) throws Exception
- Throws:
Exception
-
-