public class DemuxingIoHandler extends IoHandlerAdapter
IoHandler that demuxes messageReceived events
to the appropriate MessageHandler.
You can freely register and deregister MessageHandlers using
addMessageHandler(Class, MessageHandler) and
removeMessageHandler(Class).
When message is received through a call to
messageReceived(IoSession, Object) the class of the
message object will be used to find a MessageHandler for
that particular message type. If no MessageHandler instance can be
found for the immediate class (i.e. message.getClass()) the
interfaces implemented by the immediate class will be searched in depth-first
order. If no match can be found for any of the interfaces the search will be
repeated recursively for the superclass of the immediate class
(i.e. message.getClass().getSuperclass()).
Consider the following type hierarchy (Cx are classes while
Ix are interfaces):
C3 - I7 - I9
| | /\
| I8 I3 I4
|
C2 - I5 - I6
|
C1 - I1 - I2 - I4
| |
| I3
Object
When message is of type C3 this hierarchy will be
searched in the following order:
C3, I7, I8, I9, I3, I4, C2, I5, I6, C1, I1, I2, I3, I4, Object.
For efficiency searches will be cached. Calls to
addMessageHandler(Class, MessageHandler) and
removeMessageHandler(Class) clear this cache.
| Constructor and Description |
|---|
DemuxingIoHandler()
Creates a new instance with no registered
MessageHandlers. |
| Modifier and Type | Method and Description |
|---|---|
<E> MessageHandler<? super E> |
addMessageHandler(java.lang.Class<E> type,
MessageHandler<? super E> handler)
Registers a
MessageHandler that receives the messages of
the specified type. |
protected MessageHandler<java.lang.Object> |
findHandler(java.lang.Class type) |
<E> MessageHandler<? super E> |
getMessageHandler(java.lang.Class<E> type)
Returns the
MessageHandler which is registered to process
the specified type. |
java.util.Map<java.lang.Class,MessageHandler> |
getMessageHandlerMap()
Returns the
Map which contains all messageType-MessageHandler
pairs registered to this handler. |
void |
messageReceived(IoSession session,
java.lang.Object message)
Forwards the received events into the appropriate
MessageHandler
which is registered by addMessageHandler(Class, MessageHandler). |
<E> MessageHandler<? super E> |
removeMessageHandler(java.lang.Class<E> type)
Deregisters a
MessageHandler that receives the messages of
the specified type. |
exceptionCaught, messageSent, sessionClosed, sessionCreated, sessionIdle, sessionOpenedpublic DemuxingIoHandler()
MessageHandlers.public <E> MessageHandler<? super E> addMessageHandler(java.lang.Class<E> type, MessageHandler<? super E> handler)
MessageHandler that receives the messages of
the specified type.public <E> MessageHandler<? super E> removeMessageHandler(java.lang.Class<E> type)
MessageHandler that receives the messages of
the specified type.public <E> MessageHandler<? super E> getMessageHandler(java.lang.Class<E> type)
MessageHandler which is registered to process
the specified type.public java.util.Map<java.lang.Class,MessageHandler> getMessageHandlerMap()
Map which contains all messageType-MessageHandler
pairs registered to this handler.public void messageReceived(IoSession session, java.lang.Object message) throws java.lang.Exception
MessageHandler
which is registered by addMessageHandler(Class, MessageHandler).messageReceived in interface IoHandlermessageReceived in class IoHandlerAdapterjava.lang.Exceptionprotected MessageHandler<java.lang.Object> findHandler(java.lang.Class type)