Class DemuxingIoHandler
- All Implemented Interfaces:
IoHandler
IoHandler that demuxes messageReceived events
to the appropriate MessageHandler.
You can freely register and deregister MessageHandlers 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
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
addReceivedMessageHandler(Class, MessageHandler) and
removeReceivedMessageHandler(Class) clear this cache.
-
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final Map<Class<?>, ExceptionHandler<?>> private final Map<Class<?>, ExceptionHandler<?>> private final Map<Class<?>, MessageHandler<?>> private final Map<Class<?>, MessageHandler<?>> private final Map<Class<?>, MessageHandler<?>> private final Map<Class<?>, MessageHandler<?>> -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<E extends Throwable>
ExceptionHandler<? super E> addExceptionHandler(Class<E> type, ExceptionHandler<? super E> handler) Registers aMessageHandlerthat receives the messages of the specifiedtype.<E> MessageHandler<? super E> addReceivedMessageHandler(Class<E> type, MessageHandler<? super E> handler) Registers aMessageHandlerthat handles the received messages of the specifiedtype.<E> MessageHandler<? super E> addSentMessageHandler(Class<E> type, MessageHandler<? super E> handler) Registers aMessageHandlerthat handles the sent messages of the specifiedtype.voidexceptionCaught(IoSession session, Throwable cause) Invoked when any exception is thrown by user IoHandler implementation or by MINA.private ExceptionHandler<Throwable> findExceptionHandler(Class<?> type, Set<Class<?>> triedClasses) protected ExceptionHandler<Throwable> findExceptionHandler(Class<? extends Throwable> type) private Objectprotected MessageHandler<Object> findReceivedMessageHandler(Class<?> type) private MessageHandler<Object> findReceivedMessageHandler(Class<?> type, Set<Class<?>> triedClasses) protected MessageHandler<Object> findSentMessageHandler(Class<?> type) private MessageHandler<Object> findSentMessageHandler(Class<?> type, Set<Class<?>> triedClasses) Map<Class<?>, ExceptionHandler<?>> <E> MessageHandler<? super E> getMessageHandler(Class<E> type) Map<Class<?>, MessageHandler<?>> Map<Class<?>, MessageHandler<?>> voidmessageReceived(IoSession session, Object message) Forwards the received events into the appropriateMessageHandlerwhich is registered byaddReceivedMessageHandler(Class, MessageHandler).voidmessageSent(IoSession session, Object message) Invoked when a message written by IoSession.write(Object) is sent out.<E extends Throwable>
ExceptionHandler<? super E> removeExceptionHandler(Class<E> type) Deregisters aMessageHandlerthat receives the messages of the specifiedtype.<E> MessageHandler<? super E> removeReceivedMessageHandler(Class<E> type) Deregisters aMessageHandlerthat handles the received messages of the specifiedtype.<E> MessageHandler<? super E> removeSentMessageHandler(Class<E> type) Deregisters aMessageHandlerthat handles the sent messages of the specifiedtype.Methods inherited from class IoHandlerAdapter
event, inputClosed, sessionClosed, sessionCreated, sessionIdle, sessionOpened
-
Field Details
-
receivedMessageHandlerCache
-
receivedMessageHandlers
-
sentMessageHandlerCache
-
sentMessageHandlers
-
exceptionHandlerCache
-
exceptionHandlers
-
-
Constructor Details
-
DemuxingIoHandler
public DemuxingIoHandler()Creates a new instance with no registeredMessageHandlers.
-
-
Method Details
-
addReceivedMessageHandler
public <E> MessageHandler<? super E> addReceivedMessageHandler(Class<E> type, MessageHandler<? super E> handler) Registers aMessageHandlerthat handles the received messages of the specifiedtype.- Type Parameters:
E- The message handler's type- Parameters:
type- The message's typehandler- The message handler- Returns:
- the old handler if there is already a registered handler for
the specified
type.nullotherwise.
-
removeReceivedMessageHandler
Deregisters aMessageHandlerthat handles the received messages of the specifiedtype.- Type Parameters:
E- The message handler's type- Parameters:
type- The message's type- Returns:
- the removed handler if successfully removed.
nullotherwise.
-
addSentMessageHandler
public <E> MessageHandler<? super E> addSentMessageHandler(Class<E> type, MessageHandler<? super E> handler) Registers aMessageHandlerthat handles the sent messages of the specifiedtype.- Type Parameters:
E- The message handler's type- Parameters:
type- The message's typehandler- The message handler- Returns:
- the old handler if there is already a registered handler for
the specified
type.nullotherwise.
-
removeSentMessageHandler
Deregisters aMessageHandlerthat handles the sent messages of the specifiedtype.- Type Parameters:
E- The message handler's type- Parameters:
type- The message's type- Returns:
- the removed handler if successfully removed.
nullotherwise.
-
addExceptionHandler
public <E extends Throwable> ExceptionHandler<? super E> addExceptionHandler(Class<E> type, ExceptionHandler<? super E> handler) Registers aMessageHandlerthat receives the messages of the specifiedtype.- Type Parameters:
E- The message handler's type- Parameters:
type- The message's typehandler- The Exception handler- Returns:
- the old handler if there is already a registered handler for
the specified
type.nullotherwise.
-
removeExceptionHandler
Deregisters aMessageHandlerthat receives the messages of the specifiedtype.- Type Parameters:
E- The Exception Handler's type- Parameters:
type- The message's type- Returns:
- the removed handler if successfully removed.
nullotherwise.
-
getMessageHandler
- Type Parameters:
E- The message handler's type- Parameters:
type- The message's type- Returns:
- the
MessageHandlerwhich is registered to process the specifiedtype.
-
getReceivedMessageHandlerMap
- Returns:
- the
Mapwhich contains all messageType-MessageHandlerpairs registered to this handler for received messages.
-
getSentMessageHandlerMap
- Returns:
- the
Mapwhich contains all messageType-MessageHandlerpairs registered to this handler for sent messages.
-
getExceptionHandlerMap
- Returns:
- the
Mapwhich contains all messageType-MessageHandlerpairs registered to this handler.
-
messageReceived
Forwards the received events into the appropriateMessageHandlerwhich is registered byaddReceivedMessageHandler(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.- Specified by:
messageReceivedin interfaceIoHandler- Overrides:
messageReceivedin classIoHandlerAdapter- Parameters:
session- The session that is receiving a messagemessage- The received message- Throws:
Exception- If we get an exception while processing the received message
-
messageSent
Invoked when a message written by IoSession.write(Object) is sent out. 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 written byIoSession.write(Object)is sent out.- Specified by:
messageSentin interfaceIoHandler- Overrides:
messageSentin classIoHandlerAdapter- Parameters:
session- The session that has sent a full messagemessage- The sent message- Throws:
Exception- If we get an exception while processing the sent message
-
exceptionCaught
Invoked when any exception is thrown by user IoHandler implementation or by MINA. If cause is an instance of IOException, MINA will close the connection automatically. 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 any exception is thrown by userIoHandlerimplementation or by MINA. Ifcauseis an instance ofIOException, MINA will close the connection automatically.- Specified by:
exceptionCaughtin interfaceIoHandler- Overrides:
exceptionCaughtin classIoHandlerAdapter- Parameters:
session- The session for which we have got an exceptioncause- The exception that has been caught- Throws:
Exception- If we get an exception while processing the caught exception
-
findReceivedMessageHandler
-
findSentMessageHandler
-
findExceptionHandler
-
findReceivedMessageHandler
private MessageHandler<Object> findReceivedMessageHandler(Class<?> type, Set<Class<?>> triedClasses) -
findSentMessageHandler
-
findExceptionHandler
-
findHandler
-