public class DemuxingIoHandler extends IoHandlerAdapter
IoHandler
that demuxes messageReceived
events
to the appropriate MessageHandler
.
You can freely register and deregister MessageHandler
s using
addReceivedMessageHandler(Class, MessageHandler)
and
removeReceivedMessageHandler(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 ObjectWhen
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
addReceivedMessageHandler(Class, MessageHandler)
and
removeReceivedMessageHandler(Class)
clear this cache.
构造器和说明 |
---|
DemuxingIoHandler()
Creates a new instance with no registered
MessageHandler s. |
限定符和类型 | 方法和说明 |
---|---|
<E extends java.lang.Throwable> |
addExceptionHandler(java.lang.Class<E> type,
ExceptionHandler<? super E> handler)
Registers a
MessageHandler that receives the messages of
the specified type . |
<E> MessageHandler<? super E> |
addReceivedMessageHandler(java.lang.Class<E> type,
MessageHandler<? super E> handler)
Registers a
MessageHandler that handles the received messages of
the specified type . |
<E> MessageHandler<? super E> |
addSentMessageHandler(java.lang.Class<E> type,
MessageHandler<? super E> handler)
Registers a
MessageHandler that handles the sent messages of the
specified type . |
void |
exceptionCaught(IoSession session,
java.lang.Throwable cause)
Invoked when any exception is thrown by user IoHandler implementation
or by MINA.
|
protected ExceptionHandler<java.lang.Throwable> |
findExceptionHandler(java.lang.Class<? extends java.lang.Throwable> type) |
protected MessageHandler<java.lang.Object> |
findReceivedMessageHandler(java.lang.Class<?> type) |
protected MessageHandler<java.lang.Object> |
findSentMessageHandler(java.lang.Class<?> type) |
java.util.Map<java.lang.Class<?>,ExceptionHandler<?>> |
getExceptionHandlerMap() |
<E> MessageHandler<? super E> |
getMessageHandler(java.lang.Class<E> type) |
java.util.Map<java.lang.Class<?>,MessageHandler<?>> |
getReceivedMessageHandlerMap() |
java.util.Map<java.lang.Class<?>,MessageHandler<?>> |
getSentMessageHandlerMap() |
void |
messageReceived(IoSession session,
java.lang.Object message)
Forwards the received events into the appropriate
MessageHandler
which is registered by addReceivedMessageHandler(Class, MessageHandler) . |
void |
messageSent(IoSession session,
java.lang.Object message)
Invoked when a message written by IoSession.write(Object) is sent out.
|
<E extends java.lang.Throwable> |
removeExceptionHandler(java.lang.Class<E> type)
Deregisters a
MessageHandler that receives the messages of
the specified type . |
<E> MessageHandler<? super E> |
removeReceivedMessageHandler(java.lang.Class<E> type)
Deregisters a
MessageHandler that handles the received messages
of the specified type . |
<E> MessageHandler<? super E> |
removeSentMessageHandler(java.lang.Class<E> type)
Deregisters a
MessageHandler that handles the sent messages of
the specified type . |
inputClosed, sessionClosed, sessionCreated, sessionIdle, sessionOpened
public DemuxingIoHandler()
MessageHandler
s.public <E> MessageHandler<? super E> addReceivedMessageHandler(java.lang.Class<E> type, MessageHandler<? super E> handler)
MessageHandler
that handles the received messages of
the specified type
.E
- The message handler's typetype
- The message's typehandler
- The message handlerpublic <E> MessageHandler<? super E> removeReceivedMessageHandler(java.lang.Class<E> type)
MessageHandler
that handles the received messages
of the specified type
.E
- The message handler's typetype
- The message's typepublic <E> MessageHandler<? super E> addSentMessageHandler(java.lang.Class<E> type, MessageHandler<? super E> handler)
MessageHandler
that handles the sent messages of the
specified type
.E
- The message handler's typetype
- The message's typehandler
- The message handlerpublic <E> MessageHandler<? super E> removeSentMessageHandler(java.lang.Class<E> type)
MessageHandler
that handles the sent messages of
the specified type
.E
- The message handler's typetype
- The message's typepublic <E extends java.lang.Throwable> ExceptionHandler<? super E> addExceptionHandler(java.lang.Class<E> type, ExceptionHandler<? super E> handler)
MessageHandler
that receives the messages of
the specified type
.E
- The message handler's typetype
- The message's typehandler
- The Exception handlerpublic <E extends java.lang.Throwable> ExceptionHandler<? super E> removeExceptionHandler(java.lang.Class<E> type)
MessageHandler
that receives the messages of
the specified type
.E
- The Exception Handler's typetype
- The message's typepublic <E> MessageHandler<? super E> getMessageHandler(java.lang.Class<E> type)
E
- The message handler's typetype
- The message's typeMessageHandler
which is registered to process
the specified type
.public java.util.Map<java.lang.Class<?>,MessageHandler<?>> getReceivedMessageHandlerMap()
Map
which contains all messageType-MessageHandler
pairs registered to this handler for received messages.public java.util.Map<java.lang.Class<?>,MessageHandler<?>> getSentMessageHandlerMap()
Map
which contains all messageType-MessageHandler
pairs registered to this handler for sent messages.public java.util.Map<java.lang.Class<?>,ExceptionHandler<?>> getExceptionHandlerMap()
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 addReceivedMessageHandler(Class, MessageHandler)
.
Warning ! If you are to overload this method, be aware that you
_must_ call the messageHandler in your own method, otherwise it won't
be called.
Invoked when a message is received.messageReceived
在接口中 IoHandler
messageReceived
在类中 IoHandlerAdapter
session
- The session that is receiving a messagemessage
- The received messagejava.lang.Exception
- If we get an exception while processing the received messagepublic void messageSent(IoSession session, java.lang.Object message) throws java.lang.Exception
IoSession.write(Object)
is
sent out.messageSent
在接口中 IoHandler
messageSent
在类中 IoHandlerAdapter
session
- The session that has sent a full messagemessage
- The sent messagejava.lang.Exception
- If we get an exception while processing the sent messagepublic void exceptionCaught(IoSession session, java.lang.Throwable cause) throws java.lang.Exception
IoHandler
implementation or by MINA. If cause
is an instance of
IOException
, MINA will close the connection automatically.exceptionCaught
在接口中 IoHandler
exceptionCaught
在类中 IoHandlerAdapter
session
- The session for which we have got an exceptioncause
- The exception that has been caughtjava.lang.Exception
- If we get an exception while processing the caught exceptionprotected MessageHandler<java.lang.Object> findReceivedMessageHandler(java.lang.Class<?> type)
protected MessageHandler<java.lang.Object> findSentMessageHandler(java.lang.Class<?> type)
protected ExceptionHandler<java.lang.Throwable> findExceptionHandler(java.lang.Class<? extends java.lang.Throwable> type)