@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.ChannelHandler.Sharable| 构造器和说明 |
|---|
ReadTimeoutHandler(Timer timer,
int timeoutSeconds)
Creates a new instance.
|
ReadTimeoutHandler(Timer timer,
long timeout,
java.util.concurrent.TimeUnit unit)
Creates a new instance.
|
| 限定符和类型 | 方法和说明 |
|---|---|
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. |
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream, writeCompletepublic 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()
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
beforeAdd 在接口中 LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
afterAdd 在接口中 LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
beforeRemove 在接口中 LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
afterRemove 在接口中 LifeCycleAwareChannelHandlerjava.lang.Exceptionpublic void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel is open, but not bound nor connected.
channelOpen 在类中 SimpleChannelUpstreamHandlerjava.lang.Exceptionpublic void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannel was closed and all its related resources
were released.channelClosed 在类中 SimpleChannelUpstreamHandlerjava.lang.Exceptionpublic void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
SimpleChannelUpstreamHandlerChannelBuffer) was received
from a remote peer.messageReceived 在类中 SimpleChannelUpstreamHandlerjava.lang.Exceptionprotected void readTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
java.lang.Exception