@ChannelHandler.Sharable public class ReadTimeoutHandler extends SimpleChannelUpstreamHandler implements LifeCycleAwareChannelHandler, ExternalResourceReleasable
ReadTimeoutException when no data was read within a certain
period of time.
public class MyPipelineFactory implementsTheChannelPipelineFactory{ 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)); ...
Timer which was specified when the ReadTimeoutHandler is
created should be stopped manually by calling releaseExternalResources()
or Timer.stop() when your application shuts down.WriteTimeoutHandler,
IdleStateHandler| Modifier and Type | Class and Description |
|---|---|
private class |
ReadTimeoutHandler.ReadTimeoutTask |
private static class |
ReadTimeoutHandler.State |
ChannelHandler.Sharable| Modifier and Type | Field and Description |
|---|---|
(package private) static ReadTimeoutException |
EXCEPTION |
(package private) long |
timeoutMillis |
(package private) Timer |
timer |
| Constructor and Description |
|---|
ReadTimeoutHandler(Timer timer,
int timeoutSeconds)
Creates a new instance.
|
ReadTimeoutHandler(Timer timer,
long timeout,
java.util.concurrent.TimeUnit unit)
Creates a new instance.
|
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream, writeCompletestatic final ReadTimeoutException EXCEPTION
final Timer timer
final long timeoutMillis
public ReadTimeoutHandler(Timer timer, int timeoutSeconds)
timer - the Timer that is used to trigger the scheduled event.
The recommended Timer implementation is HashedWheelTimer.timeoutSeconds - read timeout in secondspublic ReadTimeoutHandler(Timer timer, long timeout, java.util.concurrent.TimeUnit unit)
timer - the Timer that is used to trigger the scheduled event.
The recommended Timer implementation is HashedWheelTimer.timeout - read timeoutunit - the TimeUnit of timeoutpublic void releaseExternalResources()
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.releaseExternalResources in interface ExternalResourceReleasablepublic void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
beforeAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
afterAdd in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
beforeRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
afterRemove in interface LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel is open, but not bound nor connected.
channelOpen in class SimpleChannelUpstreamHandlerjava.lang.Exceptionpublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel was closed and all its related resources
were released.channelClosed in class SimpleChannelUpstreamHandlerjava.lang.Exceptionpublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannelBuffer) was received
from a remote peer.messageReceived in class SimpleChannelUpstreamHandlerjava.lang.Exceptionprivate void initialize(ChannelHandlerContext ctx)
private static void destroy(ChannelHandlerContext ctx)
private static ReadTimeoutHandler.State state(ChannelHandlerContext ctx)
protected void readTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception