[Andorid development framework learning]-mina Development 1

Source: Internet
Author: User

 

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.

 

1. Introduction to Mina

Apache Mina is a network application framework that helps users develop high-performance and high-reliability network applications. It provides an event-driven asynchronous API abstracted through Java NiO in different transmission such as TCP/IP and UDP/IP.

 

2. What is NiO?

Java NiO, a non-congested application, is usually used for I/O read/write. We know that the performance bottleneck of system operation is usually in I/O read/write, including port and file operations, in the past, after opening an I/O channel, read () will always wait for reading byte content on the port side. If no content comes in, read () is also silly, this will affect our program to continue to do other things, so the improvement is to open a thread and let the thread wait, but this is also quite resource-consuming.

 

3. What is event-driven?

Apache Mina provides event-driven APIs. It abstracts network-related activities into events. Network applications only need to process the events they are interested in. The event-driven API makes it easier to develop network applications based on Apache Mina. The application does not need to consider the details related to the underlying transmission, but only needs to process abstract I/O events. For example, when a server application is implemented, if a new connection comes in, the I/O service will generatesessionOpenedSuch an event. If the application needs to perform certain operations when a connection is enabled, you only need to handle this event in the I/O processor.sessionOpenedAdd the corresponding code.

 

Iv. Mina Architecture

  

 

Apache Mina-based network applications have three layers: I/O service, I/O filter, and I/O processor:

    • I/O service: I/O service is used to perform actual I/O operations. Apache Mina has provided a series of I/O services that support different protocols, such as TCP/IP, UDP/IP, serial port, and virtual machine internal pipelines. Developers can also implement their own I/O services.
    • I/O filter: the I/O service can transmit byte streams, while upper-layer applications need specific objects and data structures. The I/O filter is used to convert the two. Another important role of the I/O filter is to process the input and output data to meet the cross-cutting requirements. Multiple I/O filters are connected in tandem to form an I/O filter chain.
    • I/O processor: I/O processor is used to execute specific business logic. Connect to the received message for specific processing.

To create a complete network application based on Apache Mina, you need to build these three layers respectively. Apache Mina has already provided many implementations for the I/O service and I/O filter. Therefore, the two layers can be implemented in most cases. Because I/O processors are related to specific services, we generally implement this processor.

 

5. I/O filters provided by Mina

 

    • LOG filter: loggingfilter records all events and requests;
    • Protocol encoding and decoding filter: protocolcodecfilter converts incoming bytes into message objects or performs reverse operations;
    • Compression filter: compressionfilter compresses all data;
    • Secure Sockets Layer filter: sslfilter supports SSL, TLS, and starttls. You can perform SSL encryption on your own. In the following blog, I will explain how to implement SSL encryption.

 

 

Vi. Precautions

 

    • We can see that Mina only supports adding one I/O processor. If multiple processors are added, the last processor is used by default;
    • When adding a filter, the processing order is in the order of adding a filter;
    • When using a filter, Mina only needs to add the filter as needed, not necessarily for servers and clients. That is to say, when programming on the server and client, the server has this filter, and the client may or may not have this filter.
    • If you need to use SSL encryption, it is best to add the sslfilter first when adding a filter. Otherwise, the processed data cannot be encrypted.

 

The following blog will describe the development of Mina servers and clients!

 

[Andorid development framework learning]-mina Development 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.