@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 finalTimer
timer; private finalChannelHandler
timeoutHandler; public MyPipelineFactory(Timer
timer) { this.timer = timer; this.timeoutHandler = newReadTimeoutHandler
(timer, 30), // timer must be shared. } publicChannelPipeline
getPipeline() { // An example configuration that implements 30-second read timeout: returnChannels
.pipeline( timeoutHandler, new MyHandler()); } }ServerBootstrap
bootstrap = ...;Timer
timer = 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
ChannelHandler.Sharable
Constructor and Description |
---|
ReadTimeoutHandler(Timer timer,
int timeoutSeconds)
Creates a new instance.
|
ReadTimeoutHandler(Timer timer,
long timeout,
TimeUnit unit)
Creates a new instance.
|
Modifier and Type | Method and Description |
---|---|
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, writeComplete
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 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 ExternalResourceReleasable
public void beforeAdd(ChannelHandlerContext ctx) throws Exception
beforeAdd
in interface LifeCycleAwareChannelHandler
Exception
public void afterAdd(ChannelHandlerContext ctx) throws Exception
afterAdd
in interface LifeCycleAwareChannelHandler
Exception
public void beforeRemove(ChannelHandlerContext ctx) throws Exception
beforeRemove
in interface LifeCycleAwareChannelHandler
Exception
public void afterRemove(ChannelHandlerContext ctx) throws Exception
afterRemove
in interface LifeCycleAwareChannelHandler
Exception
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
SimpleChannelUpstreamHandler
Channel
is open, but not bound nor connected.
channelOpen
in class SimpleChannelUpstreamHandler
Exception
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws Exception
SimpleChannelUpstreamHandler
Channel
was closed and all its related resources
were released.channelClosed
in class SimpleChannelUpstreamHandler
Exception
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws Exception
SimpleChannelUpstreamHandler
ChannelBuffer
) was received
from a remote peer.messageReceived
in class SimpleChannelUpstreamHandler
Exception
protected void readTimedOut(ChannelHandlerContext ctx) throws Exception
Exception
Copyright © 2008-2014 The Netty Project. All Rights Reserved.