Apache MINA network communication framework

basic introduction:

Apache MINA 2 is a web application framework for developing high performance and highly scalable web applications. It provides an abstract event-driven asynchronous API that can use transmission methods such as TCP/IP, UDP/IP, serial ports, and pipes inside the virtual machine. Apache MINA 2 can be used as a good foundation for developing web applications.

Mina's API isolates the real network communication from our application, you only need to care what you want to send,

The received data and your business logic are enough.

The basic architecture of mina:

In the module chain in the figure, IoService is the entry point of the application, which is equivalent to IoAccepter in our previous code, and IoAccepter is an extension interface of IoService. The IoService interface can be used to add multiple IoFilters that conform to the chain of responsibility pattern and are called by the IoProcessor thread. On the basis of the ioService interface, IoAccepter also provides an interface for binding a communication port and unbinding it. ioHandler is the application logic processing class.

Main classes and interfaces:

(1.) IoService: This interface is responsible for the establishment of sockets on a thread, has its own Selector, monitors

Listen to see if a connection is established.

(2.) IoProcessor: This interface is responsible for checking whether data is read or written on the channel on another thread, that is

Say it also has its own Selector, which is a difference from when we use JAVA NIO coding,

Usually in JAVA NIO coding, we all use a Selector, that is, do not distinguish between IoService

Two functional interfaces with IoProcessor. In addition, IoProcessor is responsible for calling registration on IoService

filter and call IoHandler after the filter chain.

(3.) IoFilter: This interface defines a set of interceptors, which can include log output, blacklist filtering,

Data encoding (write direction) and decoding (read direction) functions, among which data encode and decode

It is the most important and the most important place to focus on when you use Mina.

(4.) IoHandler: This interface is responsible for writing business logic, that is, where data is received and sent.

(5.) IoSession: Session can be understood as a specific connection between the server and the client, which is determined by the server address, port and client address and port. When the client initiates a request, it specifies the server address and port, and the client also specifies or automatically specifies an address and automatically assigns a port according to the network routing information. This address and port pair constitute a Session. Session is the abstraction of this connection on the server side. MINA encapsulates it and defines the IoSession interface, which is used to represent the connection between the client and the server, and refers to the client on the server side to realize the operation and binding of the client. Information and objects related to the client. By using the concept of Session, when writing a program, it is very convenient to distinguish which client's request is currently being processed on the server side, maintain the status information of the client, and realize mutual communication between clients.

A picture is worth a thousand words, the core class diagram of MINA:

The server code is roughly as follows:

Java code

  1. //Initialize Acceptor - you can not specify the number of threads, the default in MINA2 is the number of CPUs + 2
  2. // is your main thread of work
  3. NioSocketAcceptor acceptor = new NioSocketAcceptor(5);
  4. //create thread pool
  5. java.util.concurrent.Executor threadPool = newFixedThreadPool(1500);
  6. //Add filter (Filter) to Acceptor
  7. acceptor.getFilterChain().addLast(“exector”, new ExecutorFilter(threadPool));
  8. // codec
  9. acceptor.getFilterChain().addLast(“codec”,new ProtocolCodecFilter(new WebDecoder(),new XmlEncoder()));
  10. //log
  11. LoggingFilter filter = new LoggingFilter();
  12. filter.setExceptionCaughtLogLevel(LogLevel.DEBUG);
  13. filter.setMessageReceivedLogLevel(LogLevel.DEBUG);
  14. filter.setMessageSentLogLevel(LogLevel.DEBUG);
  15. filter.setSessionClosedLogLevel(LogLevel.DEBUG);
  16. filter.setSessionCreatedLogLevel(LogLevel.DEBUG);
  17. filter.setSessionIdleLogLevel(LogLevel.DEBUG);
  18. filter.setSessionOpenedLogLevel(LogLevel.DEBUG);
  19. acceptor.getFilterChain().addLast(“logger”, filter);
  20. / / Set the port that the main service listens on can be reused
  21. acceptor.setReuseAddress(true);
  22. //Set the port for each non-main listening connection to be reused
  23. acceptor.getSessionConfig().setReuseAddress(true);
  24. //In MINA2, when starting a server, the length of the initialization buffer should be set. If this value is not set, the system defaults to 2048. When the message sent by the client exceeds the set value,
  25. //MINA2's mechanism is segmented, and the characters are put into the buffer to read, so when reading the message, you need to judge how many times. The advantage of this is that it can save communication traffic.
  26. //Set the size of the input buffer
  27. acceptor.getSessionConfig().setReceiveBufferSize(1024);
  28. //Set the size of the output buffer
  29. acceptor.getSessionConfig (). setSendBufferSize (10240);
  30. //Set it to non-delayed sending, if it is true, it will not be assembled into a large package to send, and the received thing will be sent immediately
  31. acceptor.getSessionConfig().setTcpNoDelay(true);
  32. //Set the maximum value of the listening queue of the main service listening port to 100. If there are already 100 connections, new connections will be rejected by the server
  33. acceptor.setBacklog(100);
  34. acceptor.setDefaultLocalAddress(new InetSocketAddress(port));
  35. //Add Handler to Acceptor
  36. acceptor.setHandler(new YourHandler());
  37. acceptor.bind();

 

        //Initialize Acceptor - you can not specify the number of threads, the default in MINA2 is the number of CPUs + 2  
        // is your main thread of work   
        NioSocketAcceptor acceptor = new NioSocketAcceptor(5);
        //create thread pool
        java.util.concurrent.Executor threadPool = newFixedThreadPool(1500);
        //Add filter (Filter) to Acceptor         
        acceptor.getFilterChain().addLast("exector", new ExecutorFilter(threadPool));
        // codec
        acceptor.getFilterChain().addLast("codec",new ProtocolCodecFilter(new WebDecoder(),new XmlEncoder()));
        //log      
        LoggingFilter filter = new LoggingFilter();
        filter.setExceptionCaughtLogLevel(LogLevel.DEBUG);
        filter.setMessageReceivedLogLevel(LogLevel.DEBUG);
        filter.setMessageSentLogLevel(LogLevel.DEBUG);
        filter.setSessionClosedLogLevel(LogLevel.DEBUG);
        filter.setSessionCreatedLogLevel(LogLevel.DEBUG);
        filter.setSessionIdleLogLevel(LogLevel.DEBUG);
        filter.setSessionOpenedLogLevel(LogLevel.DEBUG);
        acceptor.getFilterChain().addLast("logger", filter);
        / / Set the port that the main service listens on can be reused
        acceptor.setReuseAddress(true);
        //Set the port for each non-main listening connection to be reused        
        acceptor.getSessionConfig().setReuseAddress(true);
        //In MINA2, when starting a server, the length of the initialization buffer should be set. If this value is not set, the system defaults to 2048. When the message sent by the client exceeds the set value,
        //MINA2's mechanism is segmented, and the characters are put into the buffer to read, so when reading the message, you need to judge how many times. The advantage of this is that it can save communication traffic.
        //Set the size of the input buffer
        acceptor.getSessionConfig().setReceiveBufferSize(1024);
        //Set the size of the output buffer
        acceptor.getSessionConfig (). setSendBufferSize (10240);
        //Set it to non-delayed sending, if it is true, it will not be assembled into a large package to send, and the received thing will be sent immediately         
        acceptor.getSessionConfig().setTcpNoDelay(true);
        //Set the maximum value of the listening queue of the main service listening port to 100. If there are already 100 connections, new connections will be rejected by the server         
        acceptor.setBacklog(100);
        acceptor.setDefaultLocalAddress(new InetSocketAddress(port));
        //Add Handler to Acceptor         
        acceptor.setHandler(new YourHandler());
        acceptor.bind();

The client code is roughly as follows:

The initialization of the client and the server are actually the same, that is, the initialization class is different, and the client is the sender.

Java code

  1. SocketConnector connector = new NioSocketConnector();
  2. connector.getFilterChain().addLast(“codec”, new ProtocolCodecFilter(new XmlCodecFactory(Charset.forName(charsetName), null, sertType)));
  3. //Specify the thread pool
  4. connector.getFilterChain().addLast(“executor”, new ExecutorFilter());
  5. //Specify the business processing class
  6. connector.setHandler(this);

 

        SocketConnector connector = new NioSocketConnector();
        connector.getFilterChain().addLast("codec", new ProtocolCodecFilter(new XmlCodecFactory(Charset.forName(charsetName), null, sertType)));
        //Specify the thread pool
        connector.getFilterChain().addLast("executor", new ExecutorFilter());
        //Specify the business processing class
        connector.setHandler(this);

Some event methods are defined in IoHandler, such as messageReceived, sessionOpend, sessionCreated, exceptionCaught, etc. The user only needs to implement the corresponding processing logic inside the method.

Heartbeat mechanism:

The advantage of mina's own heartbeat mechanism is that it has additional processing, so that heartbeat messages will not be transmitted to the business layer, and are completed at the bottom layer.

Event model:

MINA can be seen as event-driven. Usually in network communication, the whole process can be divided into several basic stages, such as establishing connection, data communication, closing connection.

Data communication generally includes the sending and receiving of data, because during the communication process, data may be sent and received multiple times to perform different business interactions.

It is impossible to receive and send data all the time, so Idle appears. In MINA, if no data is sent or received within a set time, an Idle event is triggered.

Appendix: Understanding of the AND Agreement, excerpted from ppt

The http protocol
corresponds to the application layer
tcp protocol
, the transport layer
ip protocol
corresponds to the network layer, and
the three are essentially not comparable. Moreover, HTTP protocol is based on TCP connection.
TCP/IP is a transport layer protocol, which mainly solves how data is transmitted in the network; while HTTP is an application layer protocol, which mainly solves how to package data.
When we transmit data, we can only use the transport layer (TCP/IP), but in that case, because there is no application layer, the data content cannot be identified. If you want to make the transmitted data meaningful, you must use the application layer protocol. There are many layer protocols, such as HTTP, FTP, TELNET, etc. You can also define application layer protocols yourself. WEB uses HTTP as the transport layer protocol to encapsulate HTTP text information, and then uses TCP/IP as the transport layer protocol to send it to the network.

Socket is an encapsulation of the TCP/IP protocol. Socket itself is not a protocol, but a calling interface (API). Through Socket, we can use the TCP/IP protocol.

It is not difficult to understand why some internal system calls use socket instead of http.

In this system of the web itself, HTTP has already encapsulated the message information. Various JEE WEB frameworks can directly obtain the information in the message, and the socket method can easily define the content of the message, the encryption method, and so on.

URL: application layer

SOCKET
: network transport layer

Socket (socket)

It is a remote inter-process communication programming interface based on the network transport layer. The operating system provides a socket including two parts: host name and port number. The port number is an integer between 0 and 65535. Usually port numbers less than 1024 are uniformly assigned to specific network services, such as ftp service, 21; http service, 80; SMTP service, 25; POP3 service, 110; telnet service, 23, etc.

Socket is the cornerstone of communication and the basic operation unit of network communication supporting TCP/IP protocol. It is an abstract representation of endpoints in the network communication process, and contains five kinds of information necessary for network communication: the protocol used for the connection, the IP address of the local host, the protocol port of the local process, the IP address of the remote host, and the protocol of the remote process. port.

When the application layer communicates data through the transport layer, TCP encounters the problem of providing concurrent services for multiple application processes at the same time. Multiple TCP connections or multiple application processes may need to
transfer data over the same TCP protocol port. In order to distinguish different application processes and connections, many computer operating systems provide socket (Socket) interfaces for application programs to interact with the TCP/IP protocol.

Since the Socket connection is usually a TCP connection, once the Socket connection is established, the two communicating parties can start sending data content to each other until the connection between the two parties is disconnected. However, in practical network applications, the communication between the client and the server often needs to pass through multiple intermediate nodes, such as routers, gateways, firewalls, etc. Most firewalls will close the connection that is inactive for a long time by default, resulting in the
disconnection of the socket connection. connection, so you need to tell the network by polling that the connection is active.
The HTTP connection uses the "request-response" method, which not only needs to establish a connection when requesting, but also requires the client to send a request to the server before the server can reply to data.

In many cases, the server side needs to actively push data to the client side to keep the real-time and synchronization of the client and server data. At this time, if the two parties establish a Socket connection, the server can directly transmit the data to the client; if the two parties establish an HTTP connection, the server needs to wait until the client sends a request before sending the data back to the client. Therefore, The client regularly sends connection requests to the server, which not only keeps it online, but also "asks" the server if there is new data, and if so, transmits the data to the client.

Guess you like

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