Mina IoHandler interface definition

Abstract implementation of Mina filter chain: http://donald-draper.iteye.com/blog/2376335
In the above article, when IOService receives a message, the message is filtered by the filter on the filter chain, from the chain head to the chain tail , the process is as follows:
//Message reception, from the head of the chain to the tail of the chain - "Iohanlder (this process handler handles related events)
public void fireMessageReceived(IoSession session, Object message) {
        Entry head = this.head;
        callNextMessageReceived(head, session, message);
}
private void callNextMessageReceived(Entry entry, IoSession session,
            Object message) {
        try {
            entry.getFilter().messageReceived(entry.getNextFilter(), session,
                    message);
        } catch (Throwable e) {
            fireExceptionCaught(session, e);
        }
}

private static class TailFilter extends IoFilterAdapter {
   private static class TailFilter extends IoFilterAdapter {
       ...
        public void messageReceived(NextFilter nextFilter, IoSession session,
                Object message) throws Exception {
            try {
                session.getHandler().messageReceived(session, message);
            } finally {
	        //If the sending message object is ByteBuffer, release the buffer
                ByteBufferUtil.releaseIfPossible(message);
            }
        }
        ...
}
}

It can be seen from the above that after IoService receives the message, the filter (protocol decoder, etc.) in the message filtering chain filters it, and finally it is handed over to the session handler for processing, that is, IoHandler. Today, let's take a look at IoHandler.

/**
 * Handles all I/O events fired by MINA.
 *
 * @author The Apache MINA Project ([email protected])
 * @version $Rev$, $Date$
 *
 * @see IoHandlerAdapter
 */
public interface IoHandler {
    /**
     * Invoked from an I/O processor thread when a new connection has been created.
     * Because this method is supposed to be called from the same thread that
     * handles I/O of multiple sessions, please implement this method to perform
     * tasks that consumes minimal amount of time such as socket parameter
     * and user-defined session attribute initialization.
     When a new connection is created, an IO handler thread calls this method. Since this method can be called by the same thread,
     Handling multiple session IO, so when implementing this method, try to consume as little time as possible when performing tasks, such as doing some Socket parameter configuration,
     User-defined session attributes
     */
    void sessionCreated(IoSession session) throws Exception;

    /**
     * Invoked when a connection has been opened.  This method is invoked after
     * {@link #sessionCreated(IoSession)}.  The biggest difference from
     * {@link #sessionCreated(IoSession)} is that it's invoked from other thread
     * than an I/O processor thread once thread modesl is configured properly.
     This method is called when a connection is opened. This method is called after the session is created. Unlike the session creation method,
     Once the threading model is configured, session open is called by other threads, not IO handlers.
     */
    void sessionOpened(IoSession session) throws Exception;

    /**
     * Invoked when a connection is closed. Called when the connection is closed.
     */
    void sessionClosed(IoSession session) throws Exception;

    /**
     * Invoked with the related {@link IdleStatus} when a connection becomes idle.
     * This method is not invoked if the transport type is UDP; it's a known bug,
     * and will be fixed in 2.0.
     When the connection is idle, the sessionIdle method is called by the relevant idle state. If the transport interface is UDP, this method will not be called,
     This is a known bug, fixed in version 2.0
     */
    void sessionIdle(IoSession session, IdleStatus status) throws Exception;

    /**
     * Invoked when any exception is thrown by user {@link IoHandler}
     * implementation or by MINA.  If <code>cause</code> is instanceof
     * {@link IOException}, MINA will close the connection automatically.
     When the user's IO handler throws an exception, the method has a mina call. If the exception is IOException, mina will
     Automatically close the connection.
     */
    void exceptionCaught(IoSession session, Throwable cause) throws Exception;

    /**
     * Invoked when a message is received. When a message is received, call
     */
    void messageReceived(IoSession session, Object message) throws Exception;

    /**
     * Invoked when a message written by {@link IoSession#write(Object)} is
     * sent out.
     When the message is sent by the Io session write method, call
     */
    void messageSent(IoSession session, Object message) throws Exception;
}

// IoHandlerAdapter
/**
 * An abstract adapter class for {@link IoHandler}.  You can extend this
 * class and selectively override required event handler methods only.  All
 * methods do nothing by default.
 * IoHandlerAdapter is a simple implementation of IO handler. You can extend this class or override event handling methods that require attention,
 By default all methods do nothing.
 * @author The Apache Directory Project ([email protected])
 * @version $Rev$, $Date$
 */
public class IoHandlerAdapter implements IoHandler {
    public void sessionCreated(IoSession session) throws Exception {
        SessionUtil.initialize(session);
    }
    public void sessionOpened(IoSession session) throws Exception {
    }

    public void sessionClosed(IoSession session) throws Exception {
    }
    public void sessionIdle(IoSession session, IdleStatus status)
            throws Exception {
    }
    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        if (SessionLog.isWarnEnabled(session)) {
            SessionLog.warn(session, "EXCEPTION, please implement "
                    + getClass().getName()
                    + ".exceptionCaught() for proper handling:", cause);
        }
    }
    public void messageReceived(IoSession session, Object message)
            throws Exception {
    }
    public void messageSent(IoSession session, Object message) throws Exception {
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326484253&siteId=291194637