Package org.jboss.netty.channel
Class SimpleChannelHandler
- java.lang.Object
-
- org.jboss.netty.channel.SimpleChannelHandler
-
- All Implemented Interfaces:
ChannelDownstreamHandler
,ChannelHandler
,ChannelUpstreamHandler
- Direct Known Subclasses:
AbstractTrafficShapingHandler
,BufferedWriteHandler
,HttpContentEncoder
,IdleStateAwareChannelHandler
,SpdyHttpResponseStreamIdHandler
public class SimpleChannelHandler extends java.lang.Object implements ChannelUpstreamHandler, ChannelDownstreamHandler
AChannelHandler
which provides an individual handler method for each event type. This handler down-casts the received upstream or or downstream event into more meaningful sub-type event and calls an appropriate handler method with the down-cast event. For an upstream event, the names of the methods are identical to the upstream event names, as introduced in theChannelEvent
documentation. For a downstream event, the names of the methods starts with the name of the operation and ends with"Requested"
(e.g.writeRequested
.)Please use
SimpleChannelUpstreamHandler
orSimpleChannelDownstreamHandler
if you want to intercept only upstream or downstream events.Overriding the
handleUpstream
andhandleDownstream
methodYou can override the
handleUpstream
andhandleDownstream
method just like overriding an ordinary Java method. Please make sure to callsuper.handleUpstream()
orsuper.handleDownstream()
so that other handler methods are invoked properly:public class MyChannelHandler extends
SimpleChannelHandler
{@Override
public void handleUpstream(ChannelHandlerContext
ctx,ChannelEvent
e) throws Exception { // Log all channel state changes. if (e instanceofChannelStateEvent
) { logger.info("Channel state changed: " + e); } super.handleUpstream(ctx, e); }@Override
public void handleDownstream(ChannelHandlerContext
ctx,ChannelEvent
e) throws Exception { // Log all channel state changes. if (e instanceofMessageEvent
) { logger.info("Writing:: " + e); } super.handleDownstream(ctx, e); } }
-
-
Nested Class Summary
-
Nested classes/interfaces inherited from interface org.jboss.netty.channel.ChannelHandler
ChannelHandler.Sharable
-
-
Field Summary
Fields Modifier and Type Field Description private static InternalLogger
logger
-
Constructor Summary
Constructors Constructor Description SimpleChannelHandler()
-
Method Summary
-
-
-
Field Detail
-
logger
private static final InternalLogger logger
-
-
Method Detail
-
handleUpstream
public void handleUpstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
Handles the specified upstream event. Down-casts the received upstream event into more meaningful sub-type event and calls an appropriate handler method with the down-casted event.- Specified by:
handleUpstream
in interfaceChannelUpstreamHandler
- Parameters:
ctx
- the context object for this handlere
- the upstream event to process or intercept- Throws:
java.lang.Exception
-
messageReceived
public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Invoked when a message object (e.g:ChannelBuffer
) was received from a remote peer.- Throws:
java.lang.Exception
-
exceptionCaught
public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) throws java.lang.Exception
Invoked when an exception was raised by an I/O thread or aChannelHandler
.- Throws:
java.lang.Exception
-
channelOpen
public void channelOpen(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked when aChannel
is open, but not bound nor connected.- Throws:
java.lang.Exception
-
channelBound
public void channelBound(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked when aChannel
is open and bound to a local address, but not connected.- Throws:
java.lang.Exception
-
channelConnected
public void channelConnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked when aChannel
is open, bound to a local address, and connected to a remote address.- Throws:
java.lang.Exception
-
channelInterestChanged
public void channelInterestChanged(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked when aChannel
'sinterestOps
was changed.- Throws:
java.lang.Exception
-
channelDisconnected
public void channelDisconnected(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked when aChannel
was disconnected from its remote peer.- Throws:
java.lang.Exception
-
channelUnbound
public void channelUnbound(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked when aChannel
was unbound from the current local address.- Throws:
java.lang.Exception
-
channelClosed
public void channelClosed(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked when aChannel
was closed and all its related resources were released.- Throws:
java.lang.Exception
-
writeComplete
public void writeComplete(ChannelHandlerContext ctx, WriteCompletionEvent e) throws java.lang.Exception
Invoked when something was written into aChannel
.- Throws:
java.lang.Exception
-
childChannelOpen
public void childChannelOpen(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws java.lang.Exception
Invoked when a childChannel
was open. (e.g. a server channel accepted a connection)- Throws:
java.lang.Exception
-
childChannelClosed
public void childChannelClosed(ChannelHandlerContext ctx, ChildChannelStateEvent e) throws java.lang.Exception
Invoked when a childChannel
was closed. (e.g. the accepted connection was closed)- Throws:
java.lang.Exception
-
handleDownstream
public void handleDownstream(ChannelHandlerContext ctx, ChannelEvent e) throws java.lang.Exception
Handles the specified downstream event. Down-casts the received downstream event into more meaningful sub-type event and calls an appropriate handler method with the down-casted event.- Specified by:
handleDownstream
in interfaceChannelDownstreamHandler
- Parameters:
ctx
- the context object for this handlere
- the downstream event to process or intercept- Throws:
java.lang.Exception
-
writeRequested
public void writeRequested(ChannelHandlerContext ctx, MessageEvent e) throws java.lang.Exception
Invoked whenChannel.write(Object)
is called.- Throws:
java.lang.Exception
-
bindRequested
public void bindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked whenChannel.bind(SocketAddress)
was called.- Throws:
java.lang.Exception
-
connectRequested
public void connectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked whenChannel.connect(SocketAddress)
was called.- Throws:
java.lang.Exception
-
setInterestOpsRequested
public void setInterestOpsRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked whenChannel.setInterestOps(int)
was called.- Throws:
java.lang.Exception
-
disconnectRequested
public void disconnectRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked whenChannel.disconnect()
was called.- Throws:
java.lang.Exception
-
unbindRequested
public void unbindRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked whenChannel.unbind()
was called.- Throws:
java.lang.Exception
-
closeRequested
public void closeRequested(ChannelHandlerContext ctx, ChannelStateEvent e) throws java.lang.Exception
Invoked whenChannel.close()
was called.- Throws:
java.lang.Exception
-
-