Today I will introduce the basic concepts and APIs of Apache Mina, including I/O services, I/O sessions, I/O filters, and I/O processors.
I. Introduction to Mina
Apache Mina is a Web application framework designed to help users simply develop high-performance and high-reliability Web applications. It provides an event-driven asynchronous API that is abstracted by Java NiO on different transports such as TCP/IP and UDP/IP.
Second, what is NIO?
Java NIO, non-clogging applications are usually used in the I/O reading and writing, we know that the performance bottleneck of the system operation is usually in the I/O read and write, including port and file operation, in the past, after opening an I/O channel, read () will be waiting on the port side read the byte content, if no content come in , read () is also silly and so on, which will affect our program to continue to do other things, then the improvement is to open a thread, let the thread to wait, but this is also very resource-intensive.
Iii. What is event driven?
Apache MINA provides an event-driven API. It abstracts various activities related to the network into events. Network applications only need to process events that are of interest to them. The event-driven API makes it easier to develop Web applications based on Apache MINA. The application does not need to consider the specifics associated with the underlying transport, but only the abstract I/O events. For example, when a service-side application is implemented, the I/O service generates an event if a new connection comes in sessionOpened
. If the application needs to perform certain actions when there is a connection open, simply sessionOpened
add the appropriate code to this event-handling method in the I/O processor.
Iv. Architecture of the Mina
Apache MINA-based network applications have three levels, namely I/O services, I/O filters, and I/O processors:
-
- I/O services: I/O services are used to perform actual I/O operations. Apache MINA has provided a range of I/O services that support different protocols, such as TCP/IP, UDP/IP, serial ports, and intra-VM pipelines. Developers can also implement their own I/O services.
- I/O filters: The I/O service is capable of transmitting byte streams, whereas upper-level applications require specific objects and data structures. The I/O filter is used to accomplish the conversion between the two. Another important function of the I/O filter is to process the input and output data to meet the crosscutting requirements. Multiple I/O filters are concatenated together to form an I/O filter chain.
- I/O processors: I/O processors are used to perform specific business logic. Performs a specific processing on the received message.
Creating a complete Apache MINA-based Web application requires building these three levels separately. Apache MINA has provided a number of implementations for I/O services and I/O filters, so these two levels can be used in most cases with an existing implementation. I/O processor because it is related to the specific business, generally we will go to implement this processor.
V. Mina self-brought I/O filter
-
- Log filter: Loggingfilter record all events and requests;
- Protocol encoding and decoding filter: Protocolcodecfilter the incoming byte stream into a Message object, or reverse operation;
- Compression Filter: Compressionfilter is responsible for compressing all the data;
- Condom Hierarchy Filter: Sslfilter provides SSL, TLS, STARTTLS support, users can do their own SSL encryption, in the following blog, I will talk about how to do SSL encryption.
VI. matters of caution
-
- From this, we can see that Mina only supports adding an I/O processor, and if multiple additions are added, the last one added is used by default;
- When adding filters, the order of processing is in accordance with the order of adding filters;
- Mina in the use of filters, as long as the need to add to the place, not necessarily the server, the client should be added. That is, the server, client programming when the server has this filter, the client can have or not.
- If the user needs to use SSL encryption, it is best to add sslfilter when adding a filter, otherwise the processed data cannot be encrypted.
The following blog I will tell Mina server, client development, welcome attention! My csdn Blog: http://blog.csdn.net/u010049692/article/details/38820321
Mina Introduction to the Mina development of "Andorid Development Framework Learning"