Class DemuxingIoHandler
- java.lang.Object
-
- org.apache.mina.core.service.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 usingaddReceivedMessageHandler(Class, MessageHandler)
andremoveReceivedMessageHandler(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
addReceivedMessageHandler(Class, MessageHandler)
andremoveReceivedMessageHandler(Class)
clear this cache.- Author:
- Apache MINA Project
-
-
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 extends Throwable>
ExceptionHandler<? super E>addExceptionHandler(Class<E> type, ExceptionHandler<? super E> handler)
Registers aMessageHandler
that receives the messages of the specifiedtype
.<E> MessageHandler<? super E>
addReceivedMessageHandler(Class<E> type, MessageHandler<? super E> handler)
Registers aMessageHandler
that handles the received messages of the specifiedtype
.<E> MessageHandler<? super E>
addSentMessageHandler(Class<E> type, MessageHandler<? super E> handler)
Registers aMessageHandler
that handles the sent messages of the specifiedtype
.void
exceptionCaught(IoSession session, Throwable cause)
Invoked when any exception is thrown by user IoHandler implementation or by MINA.protected ExceptionHandler<Throwable>
findExceptionHandler(Class<? extends Throwable> type)
protected MessageHandler<Object>
findReceivedMessageHandler(Class<?> type)
protected MessageHandler<Object>
findSentMessageHandler(Class<?> type)
Map<Class<?>,ExceptionHandler<?>>
getExceptionHandlerMap()
<E> MessageHandler<? super E>
getMessageHandler(Class<E> type)
Map<Class<?>,MessageHandler<?>>
getReceivedMessageHandlerMap()
Map<Class<?>,MessageHandler<?>>
getSentMessageHandlerMap()
void
messageReceived(IoSession session, Object message)
Forwards the received events into the appropriateMessageHandler
which is registered byaddReceivedMessageHandler(Class, MessageHandler)
.void
messageSent(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 aMessageHandler
that receives the messages of the specifiedtype
.<E> MessageHandler<? super E>
removeReceivedMessageHandler(Class<E> type)
Deregisters aMessageHandler
that handles the received messages of the specifiedtype
.<E> MessageHandler<? super E>
removeSentMessageHandler(Class<E> type)
Deregisters aMessageHandler
that handles the sent messages of the specifiedtype
.-
Methods inherited from class org.apache.mina.core.service.IoHandlerAdapter
event, inputClosed, sessionClosed, sessionCreated, sessionIdle, sessionOpened
-
-
-
-
Constructor Detail
-
DemuxingIoHandler
public DemuxingIoHandler()
Creates a new instance with no registeredMessageHandler
s.
-
-
Method Detail
-
addReceivedMessageHandler
public <E> MessageHandler<? super E> addReceivedMessageHandler(Class<E> type, MessageHandler<? super E> handler)
Registers aMessageHandler
that 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
.null
otherwise.
-
removeReceivedMessageHandler
public <E> MessageHandler<? super E> removeReceivedMessageHandler(Class<E> type)
Deregisters aMessageHandler
that 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.
null
otherwise.
-
addSentMessageHandler
public <E> MessageHandler<? super E> addSentMessageHandler(Class<E> type, MessageHandler<? super E> handler)
Registers aMessageHandler
that 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
.null
otherwise.
-
removeSentMessageHandler
public <E> MessageHandler<? super E> removeSentMessageHandler(Class<E> type)
Deregisters aMessageHandler
that 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.
null
otherwise.
-
addExceptionHandler
public <E extends Throwable> ExceptionHandler<? super E> addExceptionHandler(Class<E> type, ExceptionHandler<? super E> handler)
Registers aMessageHandler
that 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
.null
otherwise.
-
removeExceptionHandler
public <E extends Throwable> ExceptionHandler<? super E> removeExceptionHandler(Class<E> type)
Deregisters aMessageHandler
that 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.
null
otherwise.
-
getMessageHandler
public <E> MessageHandler<? super E> getMessageHandler(Class<E> type)
- Type Parameters:
E
- The message handler's type- Parameters:
type
- The message's type- Returns:
- the
MessageHandler
which is registered to process the specifiedtype
.
-
getReceivedMessageHandlerMap
public Map<Class<?>,MessageHandler<?>> getReceivedMessageHandlerMap()
- Returns:
- the
Map
which contains all messageType-MessageHandler
pairs registered to this handler for received messages.
-
getSentMessageHandlerMap
public Map<Class<?>,MessageHandler<?>> getSentMessageHandlerMap()
- Returns:
- the
Map
which contains all messageType-MessageHandler
pairs registered to this handler for sent messages.
-
getExceptionHandlerMap
public Map<Class<?>,ExceptionHandler<?>> getExceptionHandlerMap()
- Returns:
- the
Map
which contains all messageType-MessageHandler
pairs registered to this handler.
-
messageReceived
public void messageReceived(IoSession session, Object message) throws Exception
Forwards the received events into the appropriateMessageHandler
which 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:
messageReceived
in interfaceIoHandler
- Overrides:
messageReceived
in 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
public void messageSent(IoSession session, Object message) throws Exception
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:
messageSent
in interfaceIoHandler
- Overrides:
messageSent
in 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
public void exceptionCaught(IoSession session, Throwable cause) throws Exception
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 userIoHandler
implementation or by MINA. Ifcause
is an instance ofIOException
, MINA will close the connection automatically.- Specified by:
exceptionCaught
in interfaceIoHandler
- Overrides:
exceptionCaught
in 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
protected MessageHandler<Object> findReceivedMessageHandler(Class<?> type)
-
findSentMessageHandler
protected MessageHandler<Object> findSentMessageHandler(Class<?> type)
-
findExceptionHandler
protected ExceptionHandler<Throwable> findExceptionHandler(Class<? extends Throwable> type)
-
-