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
AIoHandler
that demuxesmessageReceived
events to the appropriateMessageHandler
.You can freely register and deregister
MessageHandler
s usingaddMessageHandler(Class, MessageHandler)
andremoveMessageHandler(Class)
.When
message
is received through a call tomessageReceived(IoSession, Object)
the class of themessage
object will be used to find aMessageHandler
for that particular message type. If noMessageHandler
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 whileIx
are interfaces):C3 - I7 - I9 | | /\ | I8 I3 I4 | C2 - I5 - I6 | C1 - I1 - I2 - I4 | | | I3 Object
Whenmessage
is of typeC3
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)
andremoveMessageHandler(Class)
clear this cache.
-
-
Constructor Summary
Constructors Constructor Description DemuxingIoHandler()
Creates a new instance with no registeredMessageHandler
s.
-
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 aMessageHandler
that 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 theMessageHandler
which is registered to process the specifiedtype
.java.util.Map<java.lang.Class,MessageHandler>
getMessageHandlerMap()
Returns theMap
which contains all messageType-MessageHandler
pairs registered to this handler.void
messageReceived(IoSession session, java.lang.Object message)
Forwards the received events into the appropriateMessageHandler
which is registered byaddMessageHandler(Class, MessageHandler)
.<E> MessageHandler<? super E>
removeMessageHandler(java.lang.Class<E> type)
Deregisters aMessageHandler
that 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 registeredMessageHandler
s.
-
-
Method Detail
-
addMessageHandler
public <E> MessageHandler<? super E> addMessageHandler(java.lang.Class<E> type, MessageHandler<? super E> handler)
Registers aMessageHandler
that 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 aMessageHandler
that 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 theMessageHandler
which is registered to process the specifiedtype
.
-
getMessageHandlerMap
public java.util.Map<java.lang.Class,MessageHandler> getMessageHandlerMap()
Returns theMap
which contains all messageType-MessageHandler
pairs registered to this handler.
-
messageReceived
public void messageReceived(IoSession session, java.lang.Object message) throws java.lang.Exception
Forwards the received events into the appropriateMessageHandler
which is registered byaddMessageHandler(Class, MessageHandler)
.- Specified by:
messageReceived
in interfaceIoHandler
- Overrides:
messageReceived
in classIoHandlerAdapter
- Throws:
java.lang.Exception
-
findHandler
protected MessageHandler<java.lang.Object> findHandler(java.lang.Class type)
-
-