The Use of java mina Framework and Some Understanding

Keywords: Session Java network Apache

@ (java technology notes)

The Use of java mina Framework and Some Understanding

1. What is Mina framework and why use mina?

The interpretation of the official website is as follows:
Apache Mina Server is a network communication application framework, that is to say, it is mainly a communication framework based on TCP/IP, UDP/IP protocol stack (of course, it can also provide JAVA object serialization services, virtual machine pipeline communication services, etc.). Mina can help us develop high-performance, highly scalable network communication applications quickly. Mina provides event-driven, asynchronous (M). Ina's asynchronous IO defaults to JAVA NIO as the underlying support) operation programming model.

My own understanding:
java socket is blocked, so we need to use NIO to develop efficient network communication applications, but the use of NIO is more complex. So mina can be simply understood as a Nio-based network framework.

2. Mina's usage and some understanding?

The following is a description of mina's service-side framework on mina's official website:

The mina server-side framework can be analyzed in the figure, which can be divided into three layers. I/O Acceptor serves as the receiver of the connection, IO Filter serves as the message filter, and I/O Handler serves as the processor of the message.

With this understanding, the construction code of Mina server can be simplified to the following five steps. MyServerHandler is a custom class inherited from mina's org. apache. mina. core. service. IoHandler Adapter class.
Another point is that in the mina framework, the connection state and interaction between the server and the client are carried out with the IoSession object. mina creates an IoSession object for each client connection and saves it in memory. Until the connection is disconnected.

try {
        // The first step is to build the socekt connection object NioSocketAcceptor
        NioSocketAcceptor acceptor=new NioSocketAcceptor();
        // The second step is to set up a message processor, which can be customized
        acceptor.setHandler(new MyServerHandler());
        //The third step is to set up a message filter, which can be customized
        acceptor.getFilterChain().addLast("codec", new ProtocolCodecFilter(new TextLineCodecFactory()));
        //The fourth step is to set up the related settings of the session of the connection object.
        acceptor.getSessionConfig().setIdleTime(IdleStatus.BOTH_IDLE, 3);
        //Step 5 Bind the server port and start the server
        acceptor.bind(new InetSocketAddress(9898));
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

Custom message processor:
Several methods commonly used by mina's message processor are as follows. The function and method name of the method are basically the same. Note that each message processor callback contains an IoSession object, and each client connection corresponds to an IoSession object.

public class MyServerHandler extends IoHandlerAdapter {

    public void exceptionCaught(IoSession session, Throwable cause)
            throws Exception {
        System.out.println("exceptionCaught");
    }

    public void messageReceived(IoSession session, Object message)
            throws Exception {
        String s=(String)message;
        System.out.println("messageReceived:"+s);
        session.write("server reply:"+ s);
    }

    public void messageSent(IoSession session, Object message) throws Exception {
        String s=(String)message;
        System.out.println("messageSent:"+s);
    }

    public void sessionClosed(IoSession session) throws Exception {
        System.out.println("sessionClosed");
    }

    public void sessionCreated(IoSession session) throws Exception {
        System.out.println("sessionCreated");
        System.out.println("session:"+session.toString());
    }

    public void sessionIdle(IoSession session, IdleStatus status)
            throws Exception {
        System.out.println("sessionIdle");
    }

    public void sessionOpened(IoSession session) throws Exception {
        System.out.println("sessionOpened");
    }

}

Posted by cesar_ser on Sat, 20 Apr 2019 03:45:34 -0700