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 anIdleStateEventwhen aChannelhas not performed read, write, or both operation for a while.Supported idle states
Property Meaning readerIdleTimean IdleStateEventwhose state isIdleState.READER_IDLEwill be triggered when no read was performed for the specified period of time. Specify0to disable.writerIdleTimean IdleStateEventwhose state isIdleState.WRITER_IDLEwill be triggered when no write was performed for the specified period of time. Specify0to disable.allIdleTimean IdleStateEventwhose state isIdleState.ALL_IDLEwill be triggered when neither read nor write was performed for the specified period of time. Specify0to 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 implements
TheChannelPipelineFactory{ private finalTimertimer; private finalChannelHandleridleStateHandler; public MyPipelineFactory(Timertimer) { this.timer = timer; this.idleStateHandler = newIdleStateHandler(timer, 60, 30, 0), // timer must be shared. } publicChannelPipelinegetPipeline() { returnChannels.pipeline( idleStateHandler, new MyHandler()); } } // Handler should handle theIdleStateEventtriggered byIdleStateHandler. public class MyHandler extendsIdleStateAwareChannelHandler{@Overridepublic void channelIdle(ChannelHandlerContextctx,IdleStateEvente) { if (e.getState() ==IdleState.READER_IDLE) { e.getChannel().close(); } else if (e.getState() ==IdleState.WRITER_IDLE) { e.getChannel().write(new PingMessage()); } } }ServerBootstrapbootstrap = ...;Timertimer = newHashedWheelTimer(); ... bootstrap.setPipelineFactory(new MyPipelineFactory(timer)); ...Timerwhich was specified when theIdleStateHandleris created should be stopped manually by callingreleaseExternalResources()orTimer.stop()when your application shuts down.- See Also:
ReadTimeoutHandler,WriteTimeoutHandler
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classIdleStateHandler.AllIdleTimeoutTaskprivate classIdleStateHandler.ReaderIdleTimeoutTaskprivate static classIdleStateHandler.Stateprivate classIdleStateHandler.WriterIdleTimeoutTask-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description (package private) longallIdleTimeMillis(package private) longreaderIdleTimeMillis(package private) Timertimer(package private) longwriterIdleTimeMillis
-
Constructor Summary
Constructors Constructor Description IdleStateHandler(Timer timer, int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)Creates a new instance.IdleStateHandler(Timer timer, long readerIdleTime, long writerIdleTime, long allIdleTime, java.util.concurrent.TimeUnit unit)Creates a new instance.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voidafterAdd(ChannelHandlerContext ctx)voidafterRemove(ChannelHandlerContext ctx)voidbeforeAdd(ChannelHandlerContext ctx)voidbeforeRemove(ChannelHandlerContext ctx)voidchannelClosed(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelwas closed and all its related resources were released.protected voidchannelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis)voidchannelOpen(ChannelHandlerContext ctx, ChannelStateEvent e)Invoked when aChannelis open, but not bound nor connected.private static voiddestroy(ChannelHandlerContext ctx)private voidfireChannelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis)longgetAllIdleTimeInMillis()Return the allIdleTime that was given when instance this class in milliseconds.longgetReaderIdleTimeInMillis()Return the readerIdleTime that was given when instance this class in milliseconds.longgetWriterIdleTimeInMillis()Return the writerIdleTime that was given when instance this class in milliseconds.private voidinitialize(ChannelHandlerContext ctx)voidmessageReceived(ChannelHandlerContext ctx, MessageEvent e)Invoked when a message object (e.g:ChannelBuffer) was received from a remote peer.voidreleaseExternalResources()Stops theTimerwhich was specified in the constructor of this handler.private static IdleStateHandler.Statestate(ChannelHandlerContext ctx)voidwriteComplete(ChannelHandlerContext ctx, WriteCompletionEvent e)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 Detail
-
timer
final Timer timer
-
readerIdleTimeMillis
final long readerIdleTimeMillis
-
writerIdleTimeMillis
final long writerIdleTimeMillis
-
allIdleTimeMillis
final long allIdleTimeMillis
-
-
Constructor Detail
-
IdleStateHandler
public IdleStateHandler(Timer timer, int readerIdleTimeSeconds, int writerIdleTimeSeconds, int allIdleTimeSeconds)
Creates a new instance.- Parameters:
timer- theTimerthat is used to trigger the scheduled event. The recommendedTimerimplementation isHashedWheelTimer.readerIdleTimeSeconds- anIdleStateEventwhose state isIdleState.READER_IDLEwill be triggered when no read was performed for the specified period of time. Specify0to disable.writerIdleTimeSeconds- anIdleStateEventwhose state isIdleState.WRITER_IDLEwill be triggered when no write was performed for the specified period of time. Specify0to disable.allIdleTimeSeconds- anIdleStateEventwhose state isIdleState.ALL_IDLEwill be triggered when neither read nor write was performed for the specified period of time. Specify0to disable.
-
IdleStateHandler
public IdleStateHandler(Timer timer, long readerIdleTime, long writerIdleTime, long allIdleTime, java.util.concurrent.TimeUnit unit)
Creates a new instance.- Parameters:
timer- theTimerthat is used to trigger the scheduled event. The recommendedTimerimplementation isHashedWheelTimer.readerIdleTime- anIdleStateEventwhose state isIdleState.READER_IDLEwill be triggered when no read was performed for the specified period of time. Specify0to disable.writerIdleTime- anIdleStateEventwhose state isIdleState.WRITER_IDLEwill be triggered when no write was performed for the specified period of time. Specify0to disable.allIdleTime- anIdleStateEventwhose state isIdleState.ALL_IDLEwill be triggered when neither read nor write was performed for the specified period of time. Specify0to disable.unit- theTimeUnitofreaderIdleTime,writeIdleTime, andallIdleTime
-
-
Method Detail
-
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 theTimerwhich was specified in the constructor of this handler. You should not call this method if theTimeris in use by other objects.- Specified by:
releaseExternalResourcesin interfaceExternalResourceReleasable
-
beforeAdd
public void beforeAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeAddin interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
afterAdd
public void afterAdd(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterAddin interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
beforeRemove
public void beforeRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
beforeRemovein interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
afterRemove
public void afterRemove(ChannelHandlerContext ctx) throws java.lang.Exception
- Specified by:
afterRemovein interfaceLifeCycleAwareChannelHandler- Throws:
java.lang.Exception
-
channelOpen
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when aChannelis 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:
channelOpenin classSimpleChannelUpstreamHandler- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when aChannelwas closed and all its related resources were released.- Overrides:
channelClosedin classSimpleChannelUpstreamHandler- Throws:
java.lang.Exception
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when a message object (e.g:ChannelBuffer) was received from a remote peer.- Overrides:
messageReceivedin classSimpleChannelUpstreamHandler- Throws:
java.lang.Exception
-
writeComplete
public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws java.lang.Exception
Description copied from class:SimpleChannelUpstreamHandlerInvoked when something was written into aChannel.- Overrides:
writeCompletein classSimpleChannelUpstreamHandler- Throws:
java.lang.Exception
-
initialize
private void initialize(ChannelHandlerContext ctx)
-
destroy
private static void destroy(ChannelHandlerContext ctx)
-
state
private static IdleStateHandler.State state(ChannelHandlerContext ctx)
-
fireChannelIdle
private void fireChannelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis)
-
channelIdle
protected void channelIdle(ChannelHandlerContext ctx, IdleState state, long lastActivityTimeMillis) throws java.lang.Exception
- Throws:
java.lang.Exception
-
-