Apache Mina Introduction

1. MINA Framework Introduction
Pictured below are my summary, based on MINA understanding of the framework drawn diagram:
When customers first visit to MINA prepared using the procedure, IoAcceptor as a thread running, is responsible for receiving requests from clients. When a client requests a connection, create a Session, the Session and IoProcessor, SocketChannel and IOService linked. IoProcessor also serves as another thread is running, regularly check whether there is data on the arrival of clients, and client request processing, followed by calls in various IOService registered IoFilter, the last call IoHandler final of the logic processing, and then processed and returned to the results of Filter to the client.
2. IoSession
Session can be understood as a server and client-specific connection, the connection from the server address, port, as well as client address, port to decide. The client initiated the request, specify the server address and port, the client will be specified, or the network routing information automatically assigned an address automatically assigned a port. This address, port constitute a Session.
Session is a server-side connection of this abstract, MINA them a package defines the IoSession interface, used to represent the client and server connections on the server side to refer the client to achieve the client's operations, binding with the client-related information and objects. Session through the use of this concept, write a program when you can separate the server side is very convenient area which is currently dealing with a client's request, to maintain the client state information, can be achieved between the client communications.
IoSession some commonly used to provide the following methods:
(1) setAttribute (Object key, Object value) getAttribute (Object key)
Set / Get the user-defined properties.
The attributes and session linked to facilitate future use when dealing with user requests. For example if we ask the users log in before you can continue to operate, then the user after a successful landing, you can through the setAttribute () to set a property, when the user later to continue to request, through the getAttribute () to obtain the property to determine whether the user login.
(2) getRemoteAddress ()
Access to the remote client address.
(3) getId () getCreationTime () getLastIoTime () getConfig ()
Get Session of the Id, creation time, last IO time and configuration information.
(4) write (Object message)
The data sent to the client.
(5) close ()
Close Session.
Note: You can send the data in the Session, but Session did not provide methods to read data, read data through another set of mechanisms IoHandler the messageReceived () is implemented.

3. Event
MINA can be seen as event-driven. Usually in the network of communications, the whole process can be divided into several basic phases, such as establishing connections, data communications, close the connection.
Data communications in general, including sending and receiving of data, as in the communication process, you may want to send and receive data many times to carry out different business interactions.
Can not have been receiving and sending data, so there is Idle appeared in the MINA, if the set time, no data sent or received, it will trigger an Idle event.
For some reason, an error may occur, causing the system exception occurs, triggering exception.
Therefore, if from the perspective of the incident, then it will be in the MINA as by an established communication link (sessionCreated and sessionOpened), multiple data reception and transmission, a close connection events, and more than seven kinds of events such as Idle events composition process.
Session is an abstraction of the two sides communicate with each other, so the process is a series of communications with the Session-related events.
MINA now in TCP implementations, sessionCreated and sessionOpened no difference. Therefore, strictly speaking, there are 6 types of events.

4. IoHandler
MINA framework from the above diagram can be seen right from the client-side data processing is finally dealt with in the IoHandler. IoHandler package from the client-side handling of different events, if interested in an event, you can achieve the appropriate method, when the event occurs, IoHandler in the implementation of the method will be triggered. IoHandler a total of 7 methods corresponding to seven events:
(1) void exceptionCaught (IoSession session, Throwable cause)
There is triggered when an exception occurs.
(2) void messageReceived (IoSession session, Object message)
Are triggered when the news arrived, message on behalf of the received message.
(3) void messageSent (IoSession session, Object message)
Send the message is triggered from time to time, that is called IoSession.write () when triggered, message will be sent on behalf of the message.
(4) void sessionClosed (IoSession session)
When the connection is closed when you are triggered, that is triggered when the Session terminated.
(5) void sessionCreated (IoSession session)
When you create a new connection is triggered, that is, when starting a new Session when triggered.
(6) void sessionIdle (IoSession session, IdleStatus status)
Are triggered when the connection is idle. Use IoSessionConfig in setIdleTime (IdleStatus status, int idleTime) method can set the session of free time. If the Session idle longer than the set value, the method is triggered, you can session.getIdleCount (status) to obtain the sessionIdle be triggered more frequently.
(7) void sessionOpened (IoSession session)
When you open a connection is triggered. In the current implementation, it seems sessionOpened and sessionCreated no great difference, sessionCreated in the sessionOpened before being triggered.
IoHandler is an interface, in general there is no need to directly implement this interface for each method. MINA provides a IoHandlerAdapter class that implements the requirements of the IoHandler methods, but have not been doing any processing. When we want to write your own Handler, you can expand IoHandlerAdapter, override the event method can be our concern. For example, the general situation, we are more concerned about whether the data received this time, then we can cover messageReceived method, need not care about other methods.

5. IoFilter
IoFilter to the customer's request or send data to the client filter. And IoHandler like, Filter is based on events, by implementing IoFilter interface, you can right the communication process in the Session of the event for processing.
Filter is a kind of chain structure, with IoHandler different processing function of each Session of events, in addition to the object passed in session, but also was introduced to NextFilter object used to represent the next Filter. In general, in dealing with after the end, call the appropriate method under a filter for further processing. Filter can also be for a particular communication or data, without further processing, it can not invoke the corresponding method NextFilter.
Session In addition to the corresponding seven kinds of events, in the IoFilter also can be Filter's init, destroy, as well as add, remove equal-time daughter to handle it.
MINA provides a IoFilterAdapter class, we have to write your own Filter, you can expand IoFilterAdapter, do not directly implement IoFilter interface.
Apache MINA provides a LoggingFilter class is used to log the communication process.

No comments:

Post a Comment