Mina high Concurrent short connection causes java.io.IOException:Too many open files solution

Source: Internet
Author: User
These days a bug was created in the process of resolving a highly concurrent communication developed with Mina.

The simulation scenario is:

By timing trigger start thread to simulate high concurrency short connection test, the test server has 2, one is the service has started, the other is not, the client and service end are all on the same servers. After some time on the Linux host through the lsof command view, found that there are incremental file handles, pipe and eventpoll.

The exceptions thrown are as follows:

2012-10-13 10:09:48-org.apache.mina.core.service.simpleioprocessorpool.<init> (SimpleIoProcessorPool.java : 197) 2. Failed to create a new instance of Org.apache.mina.transport.socket.nio.NioProcessor:null 3.JAVA.LANG.REFLECT.INVOC        Ationtargetexception 4.        At Sun.reflect.GeneratedConstructorAccessor110.newInstance (Unknown Source) 5.        At Sun.reflect.DelegatingConstructorAccessorImpl.newInstance (delegatingconstructoraccessorimpl.java:27) 6.        At Java.lang.reflect.Constructor.newInstance (constructor.java:513) 7.        At Org.apache.mina.core.service.simpleioprocessorpool.<init> (simpleioprocessorpool.java:180) 8.        At Org.apache.mina.core.service.simpleioprocessorpool.<init> (simpleioprocessorpool.java:112) 9.        At Org.apache.mina.core.polling.abstractpollingioconnector.<init> (abstractpollingioconnector.java:93) 10. At Org.apache.mina.transport.socket.nio.niosocketconnector.<iniT> (niosocketconnector.java:56) 11.        At Com.develop.webplatform.funnel.client.JobClient.sendMessage (jobclient.java:39) 12.        At Com.develop.webplatform.funnel.client.JobClient.sendJob (jobclient.java:126) 13.    
At Com.develop.webplatform.funnel.extend.JobExecRemotelyBySocket.execJobByTask (jobexecremotelybysocket.java:66)        At Com.develop.webplatform.funnel.JobManager.execJobByTask (jobmanager.java:27) 15.        At Com.develop.webplatform.quartz.job.TaskJob.executeInternal (taskjob.java:38) 16.        At Org.springframework.scheduling.quartz.QuartzJobBean.execute (quartzjobbean.java:86) 17.        At Org.quartz.core.JobRunShell.run (jobrunshell.java:223) 18. At Org.quartz.simpl.simplethreadpool$workerthread.run (simplethreadpool.java:549) 19.Caused by:   
org.apache.mina.core.RuntimeIoException:Failed to open a selector. At Org.apache.mina.transport.socket.nio.nioprocessor.<init> (nioprocessor.java:61) 21. ...        More 22.Caused By:java.io.IOException:Too many open files 23.        At Sun.nio.ch.IOUtil.initPipe (Native method) 24.        At Sun.nio.ch.epollselectorimpl.<init> (epollselectorimpl.java:49) 25.        At Sun.nio.ch.EPollSelectorProvider.openSelector (epollselectorprovider.java:18) 26.        At Java.nio.channels.Selector.open (selector.java:209) 27.        At Org.apache.mina.transport.socket.nio.nioprocessor.<init> (nioprocessor.java:59) 28. ...  
 More


In the source code, the code for the client connection is as follows:

Final  niosocketconnector connector =  new  niosocketconnector ();    
2.        final  string[] result =  new  string[1];    
3.        connector.getfilterchain (). AddLast ("Codec",    
4.                New  Protocolcodecfilter (new  objectserializationcodecfactory ());    
5.        Connector.sethandler (handler);   
6.   
7.        Set timeout     
8.        Connector.setconnecttimeoutmillis (defaultconnecttimeout);   
9.        connectfuture connectfuture = connector.connect (address);        connectfuture.awaituninterruptibly ();   Sync, wait until the connection is complete     
.        If  (Connectfuture.isdone ()) {    
.            If  (!connectfuture.isconnected ()) {  //If no connection succeeds within the specified time, an exception is thrown     
.                Logger.info ("Fail to connect"  + loginfo);    
.                Throw   new  Exception ();   
.        }


After analysis, causing the host file handle leakage because, when the client initiates a service-side connection, it will request the system to assign the relevant file handle, in the original code, only to determine whether the connection is successful, but not the connection failed to release resources, resulting in file handle leakage. When the total number of file handles exceeds the system setting value (Ulimit-n view the maximum number of file handles allowed by the same process), an exception "Java.io.IOException:Too Many open files" is thrown, resulting in the inability to create a new connection and the server hangs.

The changed code is as follows:

final niosocketconnector connector = new Niosocketconnector ();    
2. Final string[] result = new string[1];                3. Connector.getfilterchain (). AddLast ("Codec", 4.    
New Protocolcodecfilter (New Objectserializationcodecfactory ());   
5. Connector.sethandler (handler);        6.7.        Set Timeout 8.   
Connector.setconnecttimeoutmillis (defaultconnecttimeout);   
9. Connectfuture connectfuture = connector.connect (address);   Connectfuture.awaituninterruptibly ();        Sync, wait until the connection completes 11.            if (Connectfuture.isdone ()) {12.                if (!connectfuture.isconnected ()) {//If no connection succeeds within the specified time, an exception 13 is thrown.    
Logger.info ("Fail to connect" + loginfo);    Connector.dispose ();   
If you do not close it will run for a period of time after the throw, too many open files exception, resulting in the inability to connect 15.    
throw new Exception ();        17.} 18. }


Original post address: http://marsvaadin.iteye.com/blog/1698924

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.