Solution of large concurrent socket communication

Source: Internet
Author: User
Tags java web log4j

Large Concurrent Volume Solution for Socket communication

experience in the use of--mina framework and related points

Directory

Objective... 1

What is MINA. 2

Use case: ... 2

Sub-project of Apache directly under Mina: ... 3

Similar frame: ... 3

Mina Quick Start ... 3

Preparatory knowledge: ... 3

Resource Download: ... 4

Key key code for Hello world: ... 4

Mina Depth Understanding ... 5

Mina's application layer ... 5

Mina's internal process ... 5

Choose Mina's reason ... 7

Traditional socket programming ... 7

Improved socket programming ... 7

Programming using the Mina framework ... 8

Expand knowledge ... 9

Iobuffer interface ... 9

Use of the Sendurgentdata () method ... 10

Some classes under the concurrent package ... 12

End language ... 12

preface

The author of the previous work is to do Java web-side development, after the reason for the work involved in a national-level project, mainly responsible for the underlying communication of the front machine module. After several twists and turns, the system was completed, the results in the first round of the test on the defeat returned. The basic reason is that the original design document requirements of a single "Communication Machine" and "terminal" (note a) between the concurrent amount to reach more than 2W of the connection communication, and the actual running concurrency can only reach 2,600 a difference of nearly 10 times times. After code tuning, extended JVM memory and so on, but because the base data difference is too large, the optimization results achieved are very limited. After considering at the fundamental start, only change the communication interface of the entire system, it is possible to meet the requirements of the design document. One day in a technical QQ group in a discussion, a netizen to me recommended a framework, this is the main character of this article to introduce-mina.

Note: The front machine is divided into three parts, the design of the structure of the following is as follows:

What is MINA

Apache MINA (Multipurpose Infrastructure for Network applications) is a relatively new project for the Apache organization, which provides a very convenient framework for developing high-performance and high-availability Web applications. The currently released version of Mina supports TCP/UDP application development based on Java NIO technology, serial communication programs (available only in the latest previews), and the features supported by Mina are also used in further expansion cases:

the software currently using MINA includes: Apache Directory Project, AMQP (Advanced message Queuing Protocol), RED5 Server (Macromedia Flash Media RTMP), Objectradius, OpenFire, and so on. Sub-project of Apache directly under Mina:

Ftpserver,asyncweb,sshd

in fact, after someone recommended Mina, I have a google on the Internet, search after a few down to find their own vision is too narrow, the original has the same function of the open source framework is really a lot of, it seems to continue to bubble forum and QQ (there is a legitimate reason to go to work bubble jar, chat QQ, Steal a smile ^_^ ... ) similar frameworks:

Netty2: It has a good architecture, and the framework is very well known, and there are a number of projects using the framework, which means that the development team has a very strong incentive to keep updating. At the same time, Netty2 's documentation is very complete and supports JMX. The disadvantage of Netty2 is that the quality of the code is not very good, the biggest drawback is that only ordinary TCP support.

Cindy: after Netty2 originated from the Netty2, the Messagerecognizer class design in the current version has fully supported the general Tcp/secure TCP/UDP unicast/udp multicast/pipe, You can use the same model for synchronous/asynchronous IO processing. The disadvantage of Cindy is that there are relatively few documents and fewer projects to apply.

Grizzl: The design is quite different from the general NIO framework, the main difference is that both the reading and writing are in the blocking mode, and the temporary selector is used; The thread model is highly configurable. Performance is said to be higher than Mina, but the learning curve is very high. quickserver:http://www.quickserver.org/

Xscocket: is a lightweight solution, the core idea is to shield, simplify the development of NiO way, do not need too much learning.

As for the basic use of these frameworks and the infrastructure, I have been studied and found that these frameworks are either heavy or too light. In view of the size of the project and the urgency of the time, the comparison, I chose the learning curve is low, excellent performance of Mina. As for how these advantages are manifested, the following content will continue to be interpreted for you. Mina Quick start

gossip less, to borrow master-level writing experience, we first come to a Hello world. Of course, we have to do some preparation before this. Preliminary knowledge:

JAVA NIO

Multithreading

Socket

The above knowledge has some help to the reading comprehension of this article, but it is not necessarily necessary.

Resource Downloads :

MINA2.0: temporarily divided into 1.x and 2.x two major versions, this article only involves 2.X version, as to why only speak 2.X and not to say 1.X, the more high-sounding answer is----because a great man once said: to look at the world with a development perspective ... And the real reason ... Cough .... As you all know. I can't say more. Download Address: http://mina.apache.org/downloads.html. The project uses 2.03, as of this article, the latest version is: 2.04

log4j: because the log4j bag is missing, the friend who makes the experiment needs to go to the next log4j bag.

Development tools:Eclipse

JDK:1.6x

Monitoring test tools: Oracle JRockit Mission Control 4.0.1 strongly recommended, referred to as JRMC, the development process, using it to solve a lot of performance bottlenecks, the specific use of the method, because the space limit is not done in detail, please inquire about the relevant documents. key key code for Hello World:

Server-side main function:

Ioacceptor acceptor = new niosocketacceptor ()//Set up monitor

Acceptor.getsessionconfig (). Setreadbuffersize (2048);

Acceptor.getsessionconfig (). Setidletime (Idlestatus.both_idle, 10);

Acceptor.getfilterchain (). AddLast ("codec"),

New Protocolcodecfilter (Textlinecodecfactory charset.forname ("UTF-8"),

LineDelimiter.WINDOWS.getValue (), LineDelimiter.WINDOWS.getValue ()));//loading solution/Coding Factory

Acceptor.sethandler (newStandbaowenhandler ()); Set up the processing class, whose main content is shown below.

Acceptor.bind (new inetsocketaddress (9998))//bound listening port can be bound multiple times or simultaneously.

Key code for Standbaowenhandler

Public void messagereceived (iosession sessions, Object message) throws Exception {session.gets Ervice (). Getmanagedsessions ();

String baowensrc = message.tostring ();//Original message

System.out.println (BAOWENSRC);

}

Given the length of the relationship, the class does not write full, more specific content, please refer to the Mina compression package with the demo. But in fact the service side needs to hand in the part of the only two pieces of content, the server is written, from the time to see the introduction of Mina quickly. Now we're going to test it. Open cmd (this for the colleagues, you don't have to elaborate ...) - -。。 , enter Telnet 127.0.0.1 9998 (here's 9998 to match the above code-bound port) casually enter some characters, knocking down the back of the console will display the information you entered.

This step of the children's shoes can be said that the use of Mina can be said to have a preliminary introduction, is not too easy to think. This is too fake, this is even a primer. Oh... Personally think that this is one of Mina's strong, simple lines of code to take care of a service side, it is simply too easy to get started. But everything has a second face, really want to learn a thing, with good, or wrong, we need to know too much of things. Here we start from the bottom of the Mina framework, because in practical applications, to solve some of the difficulties and problems encountered, the framework of the entire operating system must have a comprehensive and in-depth understanding. Only in this way can we encounter problems, purposeful, targeted to find the crux of the problem. Mina Depth understanding of the application layer of Mina

Figure 1

A well-designed open source framework will always only be able to reduce intrusion and find the right place throughout the project, rather than having a disproportionate impact on the overall architectural design of the project, figure 1 is the application layer diagram of Mina. As we can see from the diagram and the demo in the previous section, Mina is very good at isolating the business code from the underlying communication, and all we have to do is set up listening and then write down the business logic we need to implement.

Mina's internal process

Figure 2

(1) Ioservice: This interface is responsible for the establishment of sockets on a thread, having its own selector, and listening for connections being built.

(2) Ioprocessor: This interface is responsible for checking whether data is read or written on the channel on another thread, which means it also has its own selector, which is a difference from the way we encode Java NIO.

Typically in Java NIO encodings, we use a selector, which is not distinguishing between ioservice and ioprocessor two functional interfaces. In addition, Ioprocessor is also one of the core components of the Mina framework. When the Mina framework is started, a thread pool is used to invoke the filter that is registered on Ioservice and call Iohandler after the filter chain. By default, Ioprocessor uses n+1 threads to ask if a monitored port has data transfer, where n is the number of cores for the CPU. According to the general multithreaded design concept, the ioprocessor number of threads is the more the better, but in fact not so, because everyone knows that IO operations are very resource-intensive, so the number of ioprocessor threads in the project should be based on the actual needs, This number can be set when the Ioacceptor object is generated. Eg ioacceptor acceptor = newniosocketacceptor (N);

(3.) Iofilter: This interface defines a set of interceptors that can include log output, blacklist filtering ( See the comments section of the previous code example ), or even use AOP to write permission controls in the filter chain (the part of the author's responsibility does not involve this permission part, However, according to the author's understanding of the framework to achieve this should not be a problem, there is a need to test themselves. Data encoding (write direction) and decoding (read direction) and other functions, where the data encode and decode is the most important, but also the use of Mina when you are the main focus of attention (I have to write for decode decoding, More than more than 10 implementations have been rewritten to find the correct decoding method for the project.

(4.) Iohandler: This interface is responsible for writing business logic, that is, where to receive and send data. In the code instance of this article, you can see that the real business code is only one sentence: System.out.println (str); it's certainly not so easy in real projects, but if you write business processes and write business interfaces, you really need to replace them here. Once again witnessed the complete separation of Mina.

Having said so much, the above content can only give you a basic understanding of Mina, the advantages of the Mina framework may not be a lot of understanding, then the following content, this discussion, so that we have a more comprehensive understanding of Mina's application scenarios and advantages. Choose Mina 's reasons traditional socket programming

In traditional I/O, the simplest way to achieve a high concurrency server is to open a thread for each client. But there are several drawbacks to this approach:

Unable to respond in a timely manner when the client cap is large

Server hardware resources are limited and performance can drop dramatically

Subject to operating system limitations

But there are some advantages to this design approach:

Simple coding, easy to implement

A certain number of connection performance is relatively good.

The author of the project development, the beginning is the use of this way, write convenient, easy to control, but unfortunately the JVM can only open to more than 2K threads, will be reported-can not create new thread error.

(See below for the implementation structure diagram:)

Figure 3 (a one-to-one structure chart). ) improved socket programming

In order to solve each thread a connection model, the author first thought of using multiple threads to deal with n users, so as to ensure that the processing of multiple users while the thread cost to the critical point of the system.

This way with the previous model advantages of the same multithreading, but the number of threads fixed, fully utilize the advantages of the system performance, there is no excess overhead. But the downside is obvious:

The cost of polling is unavoidable.

One but the IO block, its blocking time is pure waste.

Customer number fixed when no previous model response fast

Coding is more complex.

Figure 4 (one-to-many) programming using the Mina framework

In order to solve the above contradictions, the final solution can only be asynchronous NiO, and as the author of Java NiO Research found that to achieve the asynchronous NIO, and applied to the actual project, we must have a more profound understanding and grasp of NIO, the author has tried to use Java native NiO interface to write a demo (specific use, interested in child shoes can be Google one, you will find that the use of native NiO writing program and using the Mina writing program is compared to how painful ....) -_-!! , but because the author in this aspect of the foundation is too thin, the test results unsatisfactory, but to the NIO more in-depth study, time is not allowed. Until the Mina framework is greeted, the above problem is no longer a problem .... Here is a schematic of how to use Mina's implementation.

Figure 5

Where the Ioservice interface will be dedicated to a thread to poll whether there is a new connection generated, once a connection is generated to notify the Ioprocessor, and ioprocessor the n+1 thread to check whether the connection has data read and write on the above (Note II). Once the connection is generated and the data is read and written, notify the decode or encode, decode or encode the message, and then give the processed message to the business class for business processing. Where Ioprocessor is the allocation of processing requests, including selection of selector, timeout verification, status records, and so on. In short, this class works in conjunction with Ioservice, encapsulating the implementation of the NIO bottom layer and supporting the functionality within the Mina framework. It is too complicated to be explained in detail.

Combined with the examples, and based on the above graphics and text, we can easily sum up the use of the Mina programming of several general steps:

Create a class that implements the Ioservice interface

Set up a filter that implements the Iofilter interface (if necessary)

Sets the processing class for an Iohandler interface implementation to handle events (required)

Start work on Ioservice binding a port

About the general operation process of Mina and the steps to be used, we will temporarily analyze this, specifically more details on the use of some of the core classes and custom encoder methods, you can directly refer to the several cases in Mina, written in very detailed, enough to solve most of the problems encountered in the project, The next thing to communicate with you is some of the extended knowledge that is very likely to be encountered when using Mina.

Note II: This is particularly important because Ioprocessor is also a polling mechanism, which causes the last state under which the same connection must be saved (the object generated by the Iosession class in Mina) when the message is too long, or otherwise causes the message to not be transmitted at one time. In order to intercept a completed message, which is also the core of the decode (encoder) need to do, the novice is often on top of the somersault.

extending Knowledge

This part of the content to say is not related to the use of Mina, but the reality is that the project with the Mina framework is basically more or less involved in this piece, that is multithreaded programming. Multithreaded programming has always been a serious and difficult problem in Java programming, many novice encounter this kind of programming problem, often can not find out the reason, even some have many years programming experience programmer also can make a mistake on this, here I also have no ability through a very short article to explain multithreading, So let's introduce a few classes and some small common sense, I hope to bring you help.

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.