Class DemuxingIoHandler
- java.lang.Object
-
- org.apache.mina.common.IoHandlerAdapter
-
- org.apache.mina.handler.demux.DemuxingIoHandler
-
- All Implemented Interfaces:
IoHandler
public class DemuxingIoHandler extends IoHandlerAdapter
AIoHandlerthat demuxesmessageReceivedevents to the appropriateMessageHandler.You can freely register and deregister
MessageHandlers usingaddMessageHandler(Class, MessageHandler)andremoveMessageHandler(Class).When
messageis received through a call tomessageReceived(IoSession, Object)the class of themessageobject will be used to find aMessageHandlerfor that particular message type. If noMessageHandlerinstance 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 (
Cxare classes whileIxare interfaces):C3 - I7 - I9 | | /\ | I8 I3 I4 | C2 - I5 - I6 | C1 - I1 - I2 - I4 | | | I3 ObjectWhenmessageis of typeC3this 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)andremoveMessageHandler(Class)clear this cache.
-
-
Constructor Summary
Constructors Constructor Description DemuxingIoHandler()Creates a new instance with no registeredMessageHandlers.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description <E> MessageHandler<? super E>addMessageHandler(java.lang.Class<E> type, MessageHandler<? super E> handler)Registers aMessageHandlerthat receives the messages of the specifiedtype.protected MessageHandler<java.lang.Object>findHandler(java.lang.Class type)<E> MessageHandler<? super E>getMessageHandler(java.lang.Class<E> type)Returns theMessageHandlerwhich is registered to process the specifiedtype.java.util.Map<java.lang.Class,MessageHandler>getMessageHandlerMap()Returns theMapwhich contains all messageType-MessageHandlerpairs registered to this handler.voidmessageReceived(IoSession session, java.lang.Object message)Forwards the received events into the appropriateMessageHandlerwhich is registered byaddMessageHandler(Class, MessageHandler).<E> MessageHandler<? super E>removeMessageHandler(java.lang.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 Detail
-
DemuxingIoHandler
public DemuxingIoHandler()
Creates a new instance with no registeredMessageHandlers.
-
-
Method Detail
-
addMessageHandler
public <E> MessageHandler<? super E> addMessageHandler(java.lang.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
public <E> MessageHandler<? super E> removeMessageHandler(java.lang.Class<E> type)
Deregisters aMessageHandlerthat receives the messages of the specifiedtype.- Returns:
- the removed handler if successfully removed. null otherwise.
-
getMessageHandler
public <E> MessageHandler<? super E> getMessageHandler(java.lang.Class<E> type)
Returns theMessageHandlerwhich is registered to process the specifiedtype.
-
getMessageHandlerMap
public java.util.Map<java.lang.Class,MessageHandler> getMessageHandlerMap()
Returns theMapwhich contains all messageType-MessageHandlerpairs registered to this handler.
-
messageReceived
public void messageReceived(IoSession session, java.lang.Object message) throws java.lang.Exception
Forwards the received events into the appropriateMessageHandlerwhich is registered byaddMessageHandler(Class, MessageHandler).- Specified by:
messageReceivedin interfaceIoHandler- Overrides:
messageReceivedin classIoHandlerAdapter- Throws:
java.lang.Exception
-
findHandler
protected MessageHandler<java.lang.Object> findHandler(java.lang.Class type)
-
-