Class DemuxingIoHandler
- All Implemented Interfaces:
IoHandler
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<E> MessageHandler<? super E>addMessageHandler(Class<E> type, MessageHandler<? super E> handler) Registers aMessageHandlerthat receives the messages of the specifiedtype.protected MessageHandler<Object>findHandler(Class type) <E> MessageHandler<? super E>getMessageHandler(Class<E> type) Returns theMessageHandlerwhich is registered to process the specifiedtype.Returns theMapwhich contains all messageType-MessageHandlerpairs registered to this handler.voidmessageReceived(IoSession session, Object message) Forwards the received events into the appropriateMessageHandlerwhich is registered byaddMessageHandler(Class, MessageHandler).<E> MessageHandler<? super E>removeMessageHandler(Class<E> type) Deregisters aMessageHandlerthat receives the messages of the specifiedtype.Methods inherited from class org.apache.mina.common.IoHandlerAdapter
exceptionCaught, messageSent, sessionClosed, sessionCreated, sessionIdle, sessionOpened
-
Constructor Details
-
DemuxingIoHandler
public DemuxingIoHandler()Creates a new instance with no registeredMessageHandlers.
-
-
Method Details
-
addMessageHandler
public <E> MessageHandler<? super E> addMessageHandler(Class<E> type, MessageHandler<? super E> handler) Registers aMessageHandlerthat receives the messages of the specifiedtype.- Returns:
- the old handler if there is already a registered handler for the specified type. null otherwise.
-
removeMessageHandler
Deregisters aMessageHandlerthat receives the messages of the specifiedtype.- Returns:
- the removed handler if successfully removed. null otherwise.
-
getMessageHandler
Returns theMessageHandlerwhich is registered to process the specifiedtype. -
getMessageHandlerMap
Returns theMapwhich contains all messageType-MessageHandlerpairs registered to this handler. -
messageReceived
Forwards the received events into the appropriateMessageHandlerwhich is registered byaddMessageHandler(Class, MessageHandler).- Specified by:
messageReceivedin interfaceIoHandler- Overrides:
messageReceivedin classIoHandlerAdapter- Throws:
Exception
-
findHandler
-