Deep understanding of several classes of Apache Mina (1)-Mina

Source: Internet
Author: User

I have been reading the Mina Source Code recently. It takes so long for Mina to be honest.
Mina has a deep understanding. I will not talk about the basic knowledge of Mina here. Many good articles on the Internet have deeply analyzed Mina, now I want
The most fundamental part of Mina is to make some in-depth discussions on Mina.

 

Or, from the Mina Entry Program, whenever you want to start a mina Program (including the server and
Client), here is only a focus on the server, as for the application of the Mina client, here is just a simple one, will not be very in-depth discussion. But the Mina Server
It is the same as that of the client, so here is a brief explanation of the problem.

 

Before that, I have been looking for a "sugar cane gourd" method to explain Mina, but it has never been
The source code of Mina is really difficult to understand. Although many articles on Mina on the Internet are very thorough, it may seem a bit esoteric for beginners, especially here.
Let's take a look at the thorough analysis of Mina's source code. If you have a certain understanding of Mina or are learning about Mina's source code, I suggest you go to his blog, there are many things in it
Is quite in place. Here is no more example. This document is intended to explain the basic knowledge of Mina to those who have just started Mina, from simple to deep, and learn the idea of Mina step by step.
The essence is that I have been dealing with Mina for a long time. I am dealing with Mina almost every day. Whenever you find a novel usage, you will be really impressed by Mina, I am not playing on Mina here
I once joked with my colleagues, saying, "When you really know Mina, you will know what Java is, I am eager to learn all the information about Mina that I know and understand now.
I want to write some things in this article. If something is not properly written, I would like to ask you to correct it more. Next I will start to provide a complete introduction to Mina.

Shao.

 

Let's talk about several classes and interfaces of Mina.
(1) ioservice
(2) baseioservice
(3) baseioacceptor
(4) ioacceptor
(5) ioconnector

These classes and interfaces are the entry programs of the entire server or client program (ioconnector). In terms of Mina, ioservice is the entry program for all Io communications, the following interfaces and classes inherit or implement the ioservice interface.

 


The following describes the overall architecture of Mina (Entry Program:

Overall architecture of Mina

 

Here we will first raise several questions:
(1) Why do I need another baseioservice when I have an ioservice?
(2) What is the difference between baseioservice and ioacceptor (ioconnector?
(3) Why does baseioacceptor (baseioconnector) not directly implement ioservice, but add

Ioacceptor (ioconnector )?

Let's take a look at the Mina source code with these questions:
First
First, I want to answer the first question. Why do I need another baseioservice if I have an ioservice? Ioservice and baseioservice are the most obvious
The difference is that ioservice is an interface, while baseioservice is an abstract class. Baseioservice implements parts of ioservice
Method.

 

The methods to be implemented in the ioservice interface and the methods implemented in baseioservice are listed as follows:

 

By comparing ioservice and baseioservice, we can find that
The getdefaconfig config () method is not implemented in baseioservice, and other methods are implemented in baseioservice. Here
There is one question: why does baseioservice only implement some ioservice methods, but not all ioservice methods? It is generally known that interfaces
The methods in must be implemented by the Implementation class, which is beyond doubt. You can write an empty method without any logic processing, but you cannot use this method in your implementation class. But in Mina
The baseioservice that is used as the implementation class does not have the getdefaconfig config () method specified by ioservice. Is Mina really unique? No
Yes! Take a closer look

Baseioservice, you will know that baseioservice is an abstract
Class, abstract class is used to be inherited, it provides some common methods of its subclass, when the abstract class implements an interface, abstract classes can selectively implement the implementation methods required by all their subclasses,
For methods specified in the interface, you can select full or partial implementation for the abstract class. In Mina, if the abstract class baseioservice is not used
Baseioacceptor and baseioconnector implement the baseioservice interface directly, which will inevitably overwrite the corresponding information in both implementation classes.
In this way, it is out of the essence of object-oriented design and does not achieve the purpose of reuse. In baseioacceptor/baseioconnector and
A baseioservice is added between baseioservices to achieve code reuse. To solve this problem, remember two points:

1) abstract classes can partially or fully implement methods in interfaces when implementing interfaces. However, when the abstract class is only real
When some methods in the interface are used, the subclass of the abstract class must implement the unimplemented interfaces in the abstract class.
Method. Here, the getdefaconfig config () method of ioservice is stored in baseioservice (baseioacceptor
Is a subclass of baseioservice, but it is also an abstract class, so it does not implement getdefaconfig config ()),

Getdefaconfig config () is implemented by the sub-classes of baseioacceptor (for example, socketacceptor, This Is

Implementation class ). Therefore, all methods of the interface must be implemented by specific implementation classes and abstract classes.

Or all methods in the implementation interface are not contradictory.

 

2) Pay attention to code reuse. Abstract classes and interfaces are provided in object-oriented programming languages.

The abstract class provides the concrete implementation of the method for its subclass to call. The interface only provides the declaration of the method

The method must be implemented by its specific implementation class. In Java, a subclass can only have one parent class, but can implement multiple interfaces.

I personally think that interfaces and abstract classes have their own characteristics. interfaces are flexible to use. Different interfaces can make their sub-classes play different angles.

Focuses on the reuse of classes, and solves the problem of code reuse to a large extent. abstract classes focus more on the reuse of methods.

In a sense, the use of abstract classes is easier for programs, but the use of abstract classes or interfaces depends on

Depends on the specific situation.


For the specific usage of interfaces and abstract classes, refer to the description of Java and mode in macro.

 

The reason for listing these problems is not only to explain the principle of Mina, but
From a high point of view, this classic open-source project, through the study and understanding of Mina, can truly understand what a project is, What Is Object-Oriented Programming, what is more essential is how to use it flexibly
Java to achieve the above two goals. This is the most important thing. Even if you have a vague understanding of Mina after reading this article, you must at least know how to develop a program
From the object perspective, think about a problem, instead of using an object-oriented language to write structured programs. These things are some of the experiences of self-development for such a long period of time. The purpose of this article is to use
Communication and learning are not just selling out, but they just want more beginners to take less detours and learn more.

 

Back to the questions just raised by Mina. Now, the first problem has been solved. Why does one have another baseioservice? The answer is to reuse the code.

 

Next we will discuss the second question. What is the difference between baseioservice and ioacceptor?
Before discussing this issue, we should first provide the methods provided by these two classes (interfaces), such:

 

We have read the baseioservice method when discussing the first question, but we have not sorted out the functions of these methods. Now we will give a brief introduction to these methods:

 

Getfilterchainbuilder ()
And setfilterchainbuilder (): these two methods are mainly used to operate the iofilter of a service.
Now you can understand it as a module that processes business logic. For example, blacklist processing, data conversion, and log information processing can all be implemented in this iofilter, it works
The principle is similar to that in servlet filters.

 

Addlistener () and removelistener (): the two methods can be understood by name. They are used to add and delete a listener for the current service, this listener is mainly used to manage the iosession currently connected to the service, which will be explained in detail later.

 

Getmanagerserviceaddress ()
Similar to getmanagersessions (): These two methods have similar functions. One is to obtain the remote address managed by the current service, and the other is to obtain the session managed by the current service.
Iosession and iosession make a complete encapsulation of socketaddress.

You can first understand the functions of these two methods as one thing. The specific difference will be given later. Ismanaged (): checks whether a socketaddress is in the managed status.

 

Getlisteners (): gets the listener of the current service.

 

After reading the introduction to the baseioservice function, we can understand it now.
Baseioservice provides methods for managing the current service. To manage a service, the premise is that the Service must exist. The premise is to start
Service, or connect to a remote host. The two tasks are completed by ioacceptor and ioconnector. Note that both objects are interfaces and do not have
The specific implementation will be implemented by their related subclasses (socketacceptor, etc.) described below. In this way, ioacceptor/ioconnector Functions
We can conclude that a service is started and stopped.

 

For a complete service, both the prerequisite for starting the service and the Service Management and Service
The logic processing of service response is indispensable. The second problem is returned. What is the difference between baseioservice and ioacceptor? The difference is that
They implement different functions, but they all lay the foundation for a complete service. None of them can be called a complete service. All three are ioservice subclasses (subinterfaces
Oservice only provides some basic methods for services. baseioservice provides the implementation of some ioservice methods, while
Ioacceptor (ioconnector) is an extension of the operations required for a specific service. Such a complete service model is gradually presented to us.

 

Next, let's discuss the third question.
Baseioacceptor (baseioconnector) does not directly implement ioservice, but adds
Ioacceptor (ioconnector )? This problem has actually been involved in the above. In order to achieve the purpose of object reuse, the designer of Mina gave
Baseioservice, ioacceptor (ioconnector) is a method that must be provided to implement a specific service. Details 1
Point, ioacceptor (ioconnector) is designed for a specific service (server/client), and ioservice only provides a service that should have
. Therefore, in Mina, an ioacceptor (ioconnector) interface is provided for specific services.
Baseioacceptor (baseioconnector) provides some necessary conditions for a service. Because it implements
The ioacceptor (ioconnector) interface inherits the abstract class baseioservice, so that all methods in ioservice are implemented and
Added the method that a specific service should have (that is, the method in ioacceptor (ioconnector ). The above is the answer to the third question.

 

 

 

Several specific services provided by Mina
From the above discussion, we have learned some functions of the classes and interfaces on the upper layer of Mina. That is, the figure above shows the clear explanation.


Here, we can simply define the upper-layer structure of Mina as the "abstract layer" of Mina. Now that we have an abstract layer, we will certainly have its specific implementation, the two most important abstract classes are baseioacceptor and baseioconnector, which are respectively used as an entry program for servers and clients.

First, let's talk about the three specific implementation classes in baseioacceptor:


Datagramacceptordelegate: the server Entry Program for UDP datagram communication. This class uses UDP protocol for communication. UDP protocol is mainly used in the monitoring of videos and remote services (such as heartbeat programs) Where medium data transmission requirements are not very high.
Vmpipeacceptor: the server Entry Program for virtual channel (VM) communication. The virtual pipeline protocol is mainly used for wireless communication.
Socketacceptor: the server Entry Program for TCP/IP communication. This is a commonly used protocol. It mainly requires high data transmission requirements, such as real-time systems and game servers.

Baseioacceptor and its subclass
Corresponding to baseioacceptor is baseioconnector, which is mainly used in client programs. The specific subclass is not described in detail. here only the structure of baseioconnector and its subclass is given.

Baseioconnector and its subclass

There will be special articles on socketacceptor, iofilter, ioprocessor, iohandler and so on. These component classes are not described in detail here.


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.