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 a
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.- See Also:
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionprivate final class
private static final class
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) static final ReadTimeoutException
(package private) final long
(package private) final Timer
-
Constructor Summary
ConstructorsConstructorDescriptionReadTimeoutHandler
(Timer timer, int timeoutSeconds) Creates a new instance.ReadTimeoutHandler
(Timer timer, long timeout, TimeUnit unit) Creates a new instance. -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
void
void
void
Invoked when aChannel
was closed and all its related resources were released.void
Invoked when aChannel
is open, but not bound nor connected.private static void
private void
void
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.protected void
void
Stops theTimer
which was specified in the constructor of this handler.private static ReadTimeoutHandler.State
Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream, writeComplete
-
Field Details
-
EXCEPTION
-
timer
-
timeoutMillis
final long timeoutMillis
-
-
Constructor Details
-
ReadTimeoutHandler
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
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 Details
-
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
- Specified by:
beforeAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-
afterAdd
- Specified by:
afterAdd
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-
beforeRemove
- Specified by:
beforeRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-
afterRemove
- Specified by:
afterRemove
in interfaceLifeCycleAwareChannelHandler
- Throws:
Exception
-
channelOpen
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:
Exception
-
channelClosed
Description copied from class:SimpleChannelUpstreamHandler
Invoked when aChannel
was closed and all its related resources were released.- Overrides:
channelClosed
in classSimpleChannelUpstreamHandler
- Throws:
Exception
-
messageReceived
Description copied from class:SimpleChannelUpstreamHandler
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.- Overrides:
messageReceived
in classSimpleChannelUpstreamHandler
- Throws:
Exception
-
initialize
-
destroy
-
state
-
readTimedOut
- Throws:
Exception
-