Original address: Mina User Guide Getting Started
1.1.1.
NiO Review 1.1.1.1.
NiO Review
The NIO API was introduced in Java 1.4 and has been adopted by most application systems. The NIO API contains non-blocking non-blocking application operations.
The following key structures are included in the java.nio.* package:
- L Buffers-Data container
- L chartsets-bytes and Unicode translation containers
- L Channels-Represents the IO operational capability of the connection and entity
- L Selectors-Multiple selectable non-blocking (non-blocking) IO selectors available
- L REGEXPS-provides regular regular expression tools
In the Mina framework we are most interested in channels_,_selectors_ and buffers, although we want to hide these elements from the user. This user guide will therefore be dedicated to building internal components on top of everything.
1.1.1.2.
nio vs. Bio
It is important to understand the difference between NiO and bio. Bio is the blocking mode that initiates a simple socket connection: When the data is read or written, whatever is done during the socket call, the called method processing is blocked until the call processing is complete.
In some cases, it is important to be able to invoke a processing operation, otherwise you must wait for the operation to complete. If the call is complete in order to do some processing, it will continue to wait, which means time.
this is where NIO needs to provide a better way to solve a large number of socket connections: You don't have to create a thread for each connection, and you just need to create a small number of threads to do the same thing.
If you want to learn more about the information that contains NIO, there are many articles and a few books on the web to solve this problem.
1.1.2.
Why is Mina 1.1.2.1.
Why is Mina
Writing some Web applications is often seen as a burden and a low level of development. It is not a field that is frequently learned and understood by developers, and because it has been forgotten for too long in school, or because the complexity and concealment of the network layer is obscured by a higher level, you have never studied it in depth.
With the introduction of asynchronous Io, other levels of complexity also appear, such as: time.
The big difference between bio and NiO is on bio. You send a request, and then wait until the response is obtained. On the server side, it means that the thread will be associated with any incoming connections, so you won't be dealing with complex multi-link connections. For NiO, on the other hand, you have to deal with the asynchronous feature's nonblocking system, which means that your application will be called when the event is triggered. In NiO, you do not call and wait for a return result, but instead use the command to tell whether the result is complete.
1.1.2.2.
need a framework
Thinking about these differences, in fact most applications expect that the network layer is blocking mode, the best solution is to write a framework to simulate (mimic) blocking mode, which is Mina.
But Mina does more, it provides basic IO communication versions such as TCP, UDP, or whatever the underlying principle. If we only consider TCP and UDP,TCP is a connection protocol when another is non-connected (UDP). Mina does a different implementation and can make you only need to focus on two aspects: and request encoding and application protocol coding, decoding.
Mina not only handles TCP and UDP, it also provides the upper layer for serial communication, such as rsc232,vmppipe,apr.
Finally, at least Mina is a network communication framework on the client and server side. The key to writing a server is to make it a scalable system that adapts to the needs of the service side, which is the benefit of Mina in terms of performance and memory usage, and it is easy to extend the server side.
1.1.2.3.
when to use Mina
This is an interesting question. Mina is not expected to be the most appropriate option under any circumstances. Here are a few elements to use Mina, which are listed below:
- Ease of use when you have no special performance requirements, Mina may be a good choice because it allows you to easily extend the server or the client without having to deal with various parameters and use cases when writing the same application when dealing with bio or NIO. You can write dozens of lines of code to define your server, and you may be able to implement some trap drops.
- With a large number of connected users using bio connectivity, NIO is significantly faster. The difference is in the 30% bio. This is true for up to thousands of connected users, but to some extent, the bio method just stops expanding: you are not able to handle millions of connected users using a thread per user! But NiO can. Now, another aspect is that Mina's time code may be unimportant part, regardless of what your application will consume. In a way, it may not be a lot more valuable to spend more energy writing your own faster network layer of access, almost without attracting attention.
- There is data to show that Mina is used by tens of millions of applications around the world. There are some Apache-based Mina projects, and they work very well. This is some form of assurance that you do not need to spend a few hours on some mysterious error in your own network layer implementation.
- There are support protocol Mina, which comes with various existing protocols such as HTTP, XML, TCP, LDAP, Dhcp,npt,dns,xmpp,ssh, FTP ... To some extent, not only can you see the Mina NiO framework, but it is implemented as a network layer protocol. One of the features of Mina is to provide you with a set of existing protocols that you can use in the near future.
1.1.3.
features
Mina is a full-featured network application framework:
- · The unified API is used for a wide variety of transport types:
- o TCP/IP & UDP/IP Java NIO
- o Serial Communication serial communication (RS232) via RXTX
- o IN-VM Pipe Communication
- O You can implement your own!
- · Filter filter interface for user-extended filtering; Similar to servlet filters
- · Low-level and advanced APIs:
- o Low-level features: Using Bytebuffers
- O Advanced Features: Use of Unified Messaging objects and encoders
- · Advanced Legacy Threading Modules:
- o Single Thread
- o one thread pool thread pool
- o More than one thread pools (i.e. SEDA) multithreaded Pool
- · Out-of-the-box SSL path TLS road StartTLS support using Java 5 sslengine
- · Overload protection and flow regulation
- · Unit tests using mock objects
- · Iohandler of stream-based I/O support flows
- · Integration with well-known containers such as Picocontainer and Spring
- · From Netty smooth migration, Netty's ancestor is Apache MINA.
1.1.4. The
first step
We will show you how easy it is to use Mina, run a very simple example and provide the Mina package. When you want to use Mina in your application, the first thing you have to do is set up your environment. We'll describe what you need to install and how to run the Mina program. No fancy, just the first time Mina feeling ...
1.1.4.1.
Download
First, you must download the latest Mina release from the download section, here. Get the latest version, unless you have good reason not to do so ...
In general, if you are using MAVEN to build your project, you don't even need to download anything, as long as you add a dependent repository containing the Mina library: you just tell maven Pom to use the required Mina jar.
1.1.4.2.
Include files
After the download is complete, extract the contents of the compressed file. tar.gz or ZIP files to the local hard disk. The downloaded compressed file has the following contents:
In Unix system input, such as:
<span style= "FONT-SIZE:12PX;" >$ Tar xzpf apache-mina-2.0.7-tar.gz</span>
In the apache-mina-2.0.7 directory, you will get the directory structure as follows:
+-Dist
+-Docs
+-Lib
+-src
+-LICENSE.txt
+-LICENSE.jzlib.txt
+-LICENSE.ognl.txt
+-LICENSE.slf4j.txt
+-LICENSE.springframework.txt
+-NOTICE.txt
1.1.4.3.
Content Details
- · Dist -contains MINA jars and its code
- · docs -Contains API documentation and code XREFS
- · Lib -contains all the jar packages needed to use Mina
1.1.4.4.
running the first Mina program
Use the downloaded release package and run its sample program.
You need to add the following jar package to Classpath:
- · Mina-core-2.0.7.jar
- · Mina-example-2.0.7.jar
- · Slf4j-api-1.6.6.jar
- · Slf4j-log4j12-1.6.6.jar
- · Log4j-1.2.17.jar
Log reminders:
- · log4j 1.2 Users:slf4j-api.jar, Slf4j-log4j12.jar, and log4j 1.2.x
- · log4j 1.3 Users:slf4j-api.jar, Slf4j-log4j13.jar, and log4j 1.3.x
- · java.util.logging Users:slf4j-api.jar and Slf4j-jdk14.jar
Important: Make sure to use the correct Slf4j-*.jar with the matching log frame you use. For example,Slf4j-log4j12.jar and Log4j-1.3.x.jar cannot be used at the same time, conflicts can occur. If you do not need a log framework you can use Slf4j-nop.jar for no logs or Slf4j-simple.jar is the most basic log framework.
At the command prompt, issue the following command:
<span style= "FONT-SIZE:12PX;" >$ Java org.apache.mina.example.gettingstarted.timeserver.minatimeserver</span>
This will start the server. Now telnet and see the executed program, issue the following command telnet:
Telnet 127. 0. 0. 1 9123
The test here can refer to one of my other articles:
Mina Quick Code Test verification Example
Well, we've run our first Mina program. Please try other sample programs accompanying Mina as an example.
1.1.5.
Summary
In this chapter, we looked at Mina to build the application architecture, the client and the server. We also implemented the sample TCP SERVER/CLIENT,UDP server and client. In the following chapters we will discuss Mina core structure and advanced topics.
Mina NiO and Bio Understanding and Telnet simple test-getting Started