In this chapter, we will give you an understanding of what MINA is, what non-blocking IO is, and why we developed this framework on non-blocking IO. We will also show you a very simple example to MINA runs.
1. Non-blocking IO
The non-blocking IO API appeared in JAVA 1.4 version and has been used in tens of thousands of applications. The NIO API covers non-blocking IO Various blocking operations. The java.nio.* package contains the following important structures:
Buffers - Data Containers Chartsets - Containers translators for bytes and Unicode Channels - represents connections to entities capable of I/O operations Selectors - provide selectable, multiplexed non-blocking IO Regexps - provide provide some tools to manipulate regular expressions
In the MINA framework, we are most interested in Channels, _Selectors_ and Buffers. We hope that users Hide this part.
2.NIO vs BIO
It is important to understand the difference between these two APIs. BIO, which is blocking IO, relies on the socket to adopt blocking mode. When you When reading, writing or any other operation on the socket, the caller will block until the operation is completed.
But in some actual situations, it expects the callee to notify the caller when the request is completed, and the caller Other operations can be completed at the same time. This is NIO. When you have many connected sockets, NIO is a better solution. You don't need to create a specific thread for each connection, you can just use some Threads do the same work. If you want to get more information about NIO, there are a lot of information and books on the Internet.
3.Why MINA?
Write some Web applications are often seen as a burden, a low-level development. It is an area that developers do not often come into contact with, or they have studied it in school but have forgotten it, or the complexity of the network layer is hidden by higher-level abstractions, and you do not understand the underlying things.
When it comes to asynchronous IO, an additional complexity appears: time.
The biggest difference between BIO and NIO is that in BIO, when you send a request, you have to wait until the request is responded to. On the server side, this means that every time a connection comes, a thread is created. So you have to deal with the complexity of multiple connections. In NIO, you have to Solving the synchronization problem in a non-blocking system, it means that when some event occurs, you have to be involved. In NIO, you don't need to wait for your previous call, you send a request and when the request is completed Will let you know.
4.The need of a framework
Given these differences, and the complexity of these problems, the best solution is to hide these complexities, which That's what MINA does. But MINA can do more. It provides some common IO when communicating through TCP, UDP or other mechanisms. If we only consider TCP and UDP, MINA can differentiate between these two protocols and allow you to focus on other parts of your application.
MINA can not only solve TCP and UDP problems, it is also very useful for serial communication and ARP.
Finally, MINA is a network framework customized to solve server-client communication problems, write a highly scalable server that adapts to needs, and allows memory performance to be reflected. These are What MINA is good at is always making it easier for you to develop servers.
5.First Steps
Below we will show how easy it is to use MINA and run a very simple example. First, go here to download the latest version of MINA. Unless you have good reasons not to use the latest version, I hope you can use the latest version for development. http://mina.apache.org/mina-project/downloads.html. After the download is complete, unzip the downloaded file. The directory will contain some subdirectories, among which disk contains the jar files of the MINA library, docs contains the API documentation, and lib contains the jar files required by MINA.
6. Run the first MINA program
First, if you are in the terminal, you need to add the following package to the classpath, modify /etc/profile, and make it take effect. If you use an integrated environment, just build your classpath.
The above is the content of Apache Mina study notes (1) - getting started. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!