Solution to too workflow open files problems during use of Apache NiO framework Mina

Source: Internet
Author: User

Recently, Mina is using the Apache NiO framework. It feels good to use it.

We use Apache NiO as a TCP server to process TCP packets.

It was just recently discovered that the server often fails to be connected once or twice a week. The user directly restarts without a screen and does not find the first-hand fault information.

I thought it was a problem with JDK and other Java package versions. The problem persists after several consecutive upgrades.

Later, I finally caught the current issue at the customer's site. After the screen and log files are backed up, Ping/telnet each server and its ports one by one. No problem found, strange. Suddenly, I used netstat to check the network connection status (Windows Server 2008) and found a large number of connections from 127.0.0.1 to 127.0.0.1 in the status of established. The port seems to increase gradually.

Looking at the log file, we found that the written "too open files" causes the socket connection to fail to be established.

Search for Google on the Internet and find many people report this problem, but no one has a solution. The FAQ on the Apache Mina website also mentioned this problem, saying that it is a nonsense to change the Windows registry. I can only investigate it myself.

This is similar to memory leakage, but it is caused by the fact that the socket is not closed. The English name is "socket leak ".

After several days of debugging, we found a solution, which was recorded for your reference.

A. Use niosocketacceptor for listen. No problem.

B. The custom iohandleradapter is Singleton in Mina and is only created once.

C. The custom iohandleradapter must contain the followingCode:

Public void exceptioncaught (iosession session, throwable cause) throws exception {
Session. Close (true); // force close right now
}

Public void sessionopened (iosession session) throws exception {
Session. getconfig (). setbothidletime (180); // set timeout seconds, must
}
Public void sessionidle (iosession session, idlestatus status) throws exception {
Session. Close (true); // timeout now, close it
}
D. Because the Code uses a Proxy ServerProgram. The proxy program provided by Mina has no practical value. It uses multiple client connections, and the server uses only one niosocketconnefor forwarding, which is very problematic. If ctor Ctor is disconnected, isn't it a big impact? Therefore, you need to change the forwarding mode of each client connection to a connector.

E. niosocketconnector is NOT thread safe, which is not mentioned in the Mina document. It's crazy.

F. The following Mina NiO Java objects are used in the system:

Niosocketacceptor is used for listen, with one iohandleradapter.

Each time a client socket is connected, corresponding to one iosession, A niosocketconneis created to connect to the backend server, and an iosession is automatically created as the Peer (PEER) of the current client socket ), that is, two iosessions have a ing relationship.

G. whether it is the client Socket connected iosession or peer iosession, you need to call peer in sessionclosed. close (false); here close (false) is not to close immediately, but to let the peer end after sending data. This is determined by NIO Asynchronous Operation features. This will not cause an endless loop: the client iosession calls peer. Close (false), while the peer calls client iosession. Close (false) in turn ). It seems that Mina has made a special deal.

H. A special reminder is that niosocketconneshould also be disabled. The function name is dispose (). This is especially important. The root cause of too program open files is here. The Mina document does not contain any words. Niosocketconnector and peer iosession use 127.0.0.1 to connect to random ports, which is incredible.

After the peer iosession is closed, the niosocketconnector is not closed, nor the Close event is sent to it, or the exception is thrown. This design is not good. After iosession is disabled, it is useless to keep niosocketconnector. It also becomes an established connection, resulting in socket leak. This seems to be the so-called half-open socket? I still think the design of Mina is not good.

The above describes how to solve the problem. I would like to share it with you. We hope that other people who encounter this problem will avoid detours.

In conclusion, the overall design of Mina is good and the code quality is also good. I reported a bug and the development team can reply quickly. I only found that the document is missing. The examples are all "Schematic", meaning it only, and cannot be used directly. There is a threshold for getting started.

Posted in http://jacklondon.cnblogs.com, reprinted please indicate the source.

-----------------

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.