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 aReadTimeoutException
when no data was read within a certain period of time.public class MyPipelineFactory implements
TheChannelPipelineFactory
{ 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 theReadTimeoutHandler
is created should be stopped manually by callingreleaseExternalResources()
orTimer.stop()
when your application shuts down.- See Also:
WriteTimeoutHandler
,IdleStateHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private class
ReadTimeoutHandler.ReadTimeoutTask
private static class
ReadTimeoutHandler.State
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description (package private) static ReadTimeoutException
EXCEPTION
(package private) long
timeoutMillis
(package private) Timer
timer
-
Constructor Summary
Constructors Constructor Description ReadTimeoutHandler(Timer timer, int timeoutSeconds)
Creates a new instance.ReadTimeoutHandler(Timer timer, long timeout, java.util.concurrent.TimeUnit unit)
Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method 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 aChannel
was closed and all its related resources were released.void
channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)
Invoked when aChannel
is open, but not bound nor connected.private static void
destroy(ChannelHandlerContext ctx)
private void
initialize(ChannelHandlerContext ctx)
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 theTimer
which was specified in the constructor of this handler.private static ReadTimeoutHandler.State
state(ChannelHandlerContext ctx)
-
Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream, writeComplete
-
-
-
-
Field Detail
-
EXCEPTION
static final ReadTimeoutException EXCEPTION
-
timer
final Timer timer
-
timeoutMillis
final long timeoutMillis
-
-
Constructor Detail
-
ReadTimeoutHandler
public ReadTimeoutHandler(Timer timer, int timeoutSeconds)
Creates a new instance.- Parameters:
timer
- theTimer
that is used to trigger the scheduled event. The recommendedTimer
implementation isHashedWheelTimer
.timeoutSeconds
- read timeout in seconds
-
ReadTimeoutHandler
public ReadTimeoutHandler(Timer timer, long timeout, java.util.concurrent.TimeUnit unit)
Creates a new instance.- Parameters:
timer
- theTimer
that is used to trigger the scheduled event. The recommendedTimer
implementation isHashedWheelTimer
.timeout
- read timeoutunit
- theTimeUnit
oftimeout
-
-
Method Detail
-
releaseExternalResources
public void releaseExternalResources()
Stops theTimer
which was specified in the constructor of this handler. You should not call this method if theTimer
is in use by other objects.- Specified by:
releaseExternalResources
in interfaceExternalResourceReleasable
-
beforeAdd
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
java.lang.Exception
-
channelOpen
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandler
Invoked when aChannel
is 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:
channelOpen
in classSimpleChannelUpstreamHandler
- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandler
Invoked when aChannel
was closed and all its related resources were released.- Overrides:
channelClosed
in classSimpleChannelUpstreamHandler
- Throws:
java.lang.Exception
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandler
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.- Overrides:
messageReceived
in classSimpleChannelUpstreamHandler
- Throws:
java.lang.Exception
-
initialize
private void initialize(ChannelHandlerContext ctx)
-
destroy
private static void destroy(ChannelHandlerContext ctx)
-
state
private static ReadTimeoutHandler.State state(ChannelHandlerContext ctx)
-
readTimedOut
protected void readTimedOut(ChannelHandlerContext ctx) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-