Package org.jboss.netty.handler.timeout
Class IdleStateHandler
java.lang.Object
org.jboss.netty.channel.SimpleChannelUpstreamHandler
org.jboss.netty.handler.timeout.IdleStateHandler
- All Implemented Interfaces:
ChannelHandler
,ChannelUpstreamHandler
,LifeCycleAwareChannelHandler
,ExternalResourceReleasable
@Sharable
public class IdleStateHandler
extends SimpleChannelUpstreamHandler
implements LifeCycleAwareChannelHandler, ExternalResourceReleasable
Triggers an
IdleStateEvent
when a Channel
has not performed
read, write, or both operation for a while.
Supported idle states
Property | Meaning |
---|---|
readerIdleTime |
an IdleStateEvent whose state is IdleState.READER_IDLE
will be triggered when no read was performed for the specified period of
time. Specify 0 to disable. |
writerIdleTime |
an IdleStateEvent whose state is IdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of
time. Specify 0 to disable. |
allIdleTime |
an IdleStateEvent whose state is IdleState.ALL_IDLE
will be triggered when neither read nor write was performed for the
specified period of time. Specify 0 to disable. |
// An example that sends a ping message when there is no outbound traffic // for 30 seconds. The connection is closed when there is no inbound traffic // for 60 seconds. public class MyPipelineFactory implementsTheChannelPipelineFactory
{ private finalTimer
timer; private finalChannelHandler
idleStateHandler; public MyPipelineFactory(Timer
timer) { this.timer = timer; this.idleStateHandler = newIdleStateHandler
(timer, 60, 30, 0), // timer must be shared. } publicChannelPipeline
getPipeline() { returnChannels
.pipeline( idleStateHandler, new MyHandler()); } } // Handler should handle theIdleStateEvent
triggered byIdleStateHandler
. public class MyHandler extendsIdleStateAwareChannelHandler
{@Override
public void channelIdle(ChannelHandlerContext
ctx,IdleStateEvent
e) { if (e.getState() ==IdleState
.READER_IDLE) { e.getChannel().close(); } else if (e.getState() ==IdleState
.WRITER_IDLE) { e.getChannel().write(new PingMessage()); } } }ServerBootstrap
bootstrap = ...;Timer
timer = newHashedWheelTimer
(); ... bootstrap.setPipelineFactory(new MyPipelineFactory(timer)); ...
Timer
which was specified when the IdleStateHandler
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 final class
private static final class
private final class
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
Field Summary
FieldsModifier and TypeFieldDescription(package private) final long
(package private) final long
(package private) final Timer
(package private) final long
-
Constructor Summary
ConstructorsConstructorDescriptionIdleStateHandler
(Timer timer, int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) Creates a new instance.IdleStateHandler
(Timer timer, long readerIdleTime, long writerIdleTime, long allIdleTime, 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.protected void
channelIdle
(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) void
Invoked when aChannel
is open, but not bound nor connected.private static void
private void
fireChannelIdle
(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) long
Return the allIdleTime that was given when instance this class in milliseconds.long
Return the readerIdleTime that was given when instance this class in milliseconds.long
Return the writerIdleTime that was given when instance this class in milliseconds.private void
void
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.void
Stops theTimer
which was specified in the constructor of this handler.private static IdleStateHandler.State
void
Invoked when something was written into aChannel
.Methods inherited from class org.jboss.netty.channel.SimpleChannelUpstreamHandler
channelBound, channelConnected, channelDisconnected, channelInterestChanged, channelUnbound, childChannelClosed, childChannelOpen, exceptionCaught, handleUpstream
-
Field Details
-
timer
-
readerIdleTimeMillis
final long readerIdleTimeMillis -
writerIdleTimeMillis
final long writerIdleTimeMillis -
allIdleTimeMillis
final long allIdleTimeMillis
-
-
Constructor Details
-
IdleStateHandler
public IdleStateHandler(Timer timer, int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds) Creates a new instance.- Parameters:
timer
- theTimer
that is used to trigger the scheduled event. The recommendedTimer
implementation isHashedWheelTimer
.readerIdleTimeSeconds
- anIdleStateEvent
whose state isIdleState.READER_IDLE
will be triggered when no read was performed for the specified period of time. Specify0
to disable.writerIdleTimeSeconds
- anIdleStateEvent
whose state isIdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of time. Specify0
to disable.allIdleTimeSeconds
- anIdleStateEvent
whose state isIdleState.ALL_IDLE
will be triggered when neither read nor write was performed for the specified period of time. Specify0
to disable.
-
IdleStateHandler
public IdleStateHandler(Timer timer, long readerIdleTime, long writerIdleTime, long allIdleTime, TimeUnit unit) Creates a new instance.- Parameters:
timer
- theTimer
that is used to trigger the scheduled event. The recommendedTimer
implementation isHashedWheelTimer
.readerIdleTime
- anIdleStateEvent
whose state isIdleState.READER_IDLE
will be triggered when no read was performed for the specified period of time. Specify0
to disable.writerIdleTime
- anIdleStateEvent
whose state isIdleState.WRITER_IDLE
will be triggered when no write was performed for the specified period of time. Specify0
to disable.allIdleTime
- anIdleStateEvent
whose state isIdleState.ALL_IDLE
will be triggered when neither read nor write was performed for the specified period of time. Specify0
to disable.unit
- theTimeUnit
ofreaderIdleTime
,writeIdleTime
, andallIdleTime
-
-
Method Details
-
getReaderIdleTimeInMillis
public long getReaderIdleTimeInMillis()Return the readerIdleTime that was given when instance this class in milliseconds. -
getWriterIdleTimeInMillis
public long getWriterIdleTimeInMillis()Return the writerIdleTime that was given when instance this class in milliseconds. -
getAllIdleTimeInMillis
public long getAllIdleTimeInMillis()Return the allIdleTime that was given when instance this class in milliseconds. -
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
-
writeComplete
Description copied from class:SimpleChannelUpstreamHandler
Invoked when something was written into aChannel
.- Overrides:
writeComplete
in classSimpleChannelUpstreamHandler
- Throws:
Exception
-
initialize
-
destroy
-
state
-
fireChannelIdle
private void fireChannelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) -
channelIdle
protected void channelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) throws Exception - Throws:
Exception
-