{"id":446,"date":"2016-04-20T18:20:56","date_gmt":"2016-04-20T10:20:56","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=446"},"modified":"2017-10-09T17:33:38","modified_gmt":"2017-10-09T09:33:38","slug":"mina%e6%89%93%e5%bc%80%e6%96%87%e4%bb%b6%e8%bf%87%e5%a4%9a%e9%97%ae%e9%a2%98%e8%a7%a3%e5%86%b3%e6%96%b9%e6%a1%88","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=446","title":{"rendered":"Mina\u6253\u5f00\u6587\u4ef6\u8fc7\u591a\u95ee\u9898\u89e3\u51b3\u65b9\u6848"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u5728\u4f7f\u7528mina\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9047\u5230\u4e86\u6253\u5f00\u6587\u4ef6\u8fc7\u591a\u95ee\u9898\uff0c\u4e00\u5927\u5806\u7684<strong>java.io.IOException: Too many open files<\/strong>\u3002\u7f51\u4e0a\u641c\u7d22\u95ee\u9898\u7684\u89e3\u51b3\u65b9\u6848\uff0c\u5927\u591a\u90fd\u662f\u4fee\u6539Linux\u7cfb\u7edf\u7684\u914d\u7f6e\uff0c\u63d0\u9ad8\u7a0b\u5e8f\u6253\u5f00\u6587\u4ef6\u7684\u6570\u91cf\u3002<br \/>\n\t\u7136\u800c\u5e76\u6ca1\u6709\u4ec0\u4e48\u5375\u7528\uff0c\u56e0\u4e3a\u4f60\u7a0b\u5e8f\u4e0a\u7684\u6839\u672c\u95ee\u9898\u6ca1\u6709\u5f97\u5230\u89e3\u51b3\uff0c\u518d\u600e\u4e48\u8bbe\u7f6e\u90fd\u4f1a\u6709\u74f6\u9888\u3002<br \/>\n\t\u6253\u5f00\u6587\u4ef6\u8fc7\u591a\uff0c\u9020\u6210\u7684\u539f\u56e0\uff0c\u4e00\u822c\u6709\u4ee5\u4e0b\u4e24\u79cd\u60c5\u51b5\uff1a<br \/>\n\t<span style=\"color:#FF0000\">1.\u6253\u5f00\u7684\u6587\u4ef6\u6ca1\u6709\u5173\u95ed<br \/>\n\t2.\u901a\u8baf\u8fde\u63a5\u6ca1\u6709\u5b8c\u5168\u5173\u95ed<\/span><br \/>\n\t\u6709\u4eba\u53ef\u80fd\u6709\u7591\u95ee\u4e86\uff0c\u4e3a\u4ec0\u4e48\u6253\u5f00\u6587\u4ef6\u8fc7\u591a\u548c\u901a\u8baf\u6709\u4ec0\u4e48\u5173\u7cfb\u3002\u8fd9\u4e5f\u6b63\u662f\u5f88\u591a\u4eba\u51fa\u73b0\u95ee\u9898\u4e0d\u77e5\u9053\u662f\u901a\u8baf\u9020\u6210\u7684\u539f\u56e0\uff0c\u800c\u9519\u8bef\u76f2\u76ee\u7684\u4e71\u6539\u4ee3\u7801\u3002\u771f\u6b63\u7684\u539f\u56e0\u662f\u56e0\u4e3aLinux\u7cfb\u7edf\u4e0a\u4e00\u4e2asocket\u5c31\u662f\u4e00\u4e2a\u6253\u5f00\u7684\u6587\u4ef6\uff0c\u56e0\u6b64\u8fc7\u591a\u7684socket\u672a\u88ab\u5b8c\u5168\u5173\u95ed\uff0c\u5c31\u4f1a\u9020\u6210\u6253\u5f00\u6587\u4ef6\u8fc7\u591a\u3002<br \/>\n\t\u90a3\u4e48\u5982\u4f55\u5f7b\u5e95\u7684\u5173\u95edMina\u7684socket\u8fde\u63a5\u5462\uff0c\u770b\u4ee5\u4e0b\u4ee3\u7801\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\npublic void test(){\r\n&nbsp;&nbsp; &nbsp;IoConnector connector = null;\r\n&nbsp;&nbsp; &nbsp;IoSession session = null;\r\n&nbsp;&nbsp; &nbsp;try {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (null == session || null == session.getRemoteAddress() || !session.isConnected()) {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;connector = new NioSocketConnector();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u8fde\u63a5\u670d\u52a1\u5668\u52a8\u4f5c\uff0c\u8d85\u65f6\u65f6\u95f4\u4e3a3\u79d2\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;connector.setConnectTimeoutMillis(3000L);\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;connector.getSessionConfig().setUseReadOperation(true);\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u521b\u5efa\u63a5\u6536\u6570\u636e\u7684\u8fc7\u6ee4\u5668\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;DefaultIoFilterChainBuilder chain = connector.getFilterChain();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u8bbe\u7f6e\u901a\u4fe1\u534f\u8bae\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;chain.addLast(&quot;codec&quot;, new ProtocolCodecFilter(new AppClientCodecFactory()));\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/** update by Herman.Xiong at 2014\u5e7411\u670821\u65e5 16:34:15 \u65b0\u589e\u5fc3\u8df3\u5904\u7406 *\/\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/chain.addLast(&quot;keepAlive&quot;, new ControlHeatbeat());\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u8bbe\u5b9a\u5ba2\u6237\u7aef\u7684\u6d88\u606f\u5904\u7406\u5668:\u4e00\u4e2aAppClientHandler\u5bf9\u8c61\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;connector.setHandler(new AscJavaClientHandler());\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u8fde\u7ed3\u5230\u670d\u52a1\u5668:\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ConnectFuture cf = connector.connect(new InetSocketAddress(&quot;10.10.2.215&quot;, 6001));\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u7b49\u5f85\u8fde\u63a5\u521b\u5efa\u5b8c\u6210\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;cf.awaitUninterruptibly();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u83b7\u53d6\u4f1a\u8bdd\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;session = cf.getSession();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;session.write(message).awaitUninterruptibly(); \/\/\u7531\u4e8e\u4e0a\u9762\u5df2\u7ecf\u8bbe\u7f6esetUseReadOperation(true),\u6545IoSession.read()\u65b9\u6cd5\u624d\u53ef\u7528 &nbsp;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;ReadFuture readFuture = session.read(); \/\/\u56e0\u5176\u5185\u90e8\u4f7f\u7528BlockingQueue,\u6545Server\u7aef\u7528\u4e4b\u53ef\u80fd\u4f1a\u5185\u5b58\u6cc4\u6f0f,\u4f46Client\u7aef\u53ef\u9002\u5f53\u7528\u4e4b &nbsp;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if(readFuture.awaitUninterruptibly(90, TimeUnit.SECONDS)){ \/\/\u7b49\u5230\u6536\u5230\u6d88\u606f &nbsp;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log.info(readFuture.getMessage());\/\/\u83b7\u53d6\u6536\u5230\u7684\u6d88\u606f\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}else{ &nbsp;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log.warn(&quot;\u8bfb\u53d6[\/\" + ipAddress + \":\" + port + \"]\u8d85\u65f6&quot;);\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;} &nbsp;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;}catch (Exception e) {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;log.error(&quot;\u901a\u8baf\u5f02\u5e38\uff01&quot;+e.getMessage(),e);\r\n&nbsp;&nbsp; &nbsp;}finally{\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u7b49\u5f85\u8fde\u63a5\u5173\u95ed\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (null != session) {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;session.close(true);\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;session.getService().dispose();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;session=null;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;\/\/ \u5173\u95ed\u5ba2\u6237\u7aef\u7ebf\u7a0b\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if (null != connector) {\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;connector.dispose();\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;connector=null;\r\n&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;}\r\n&nbsp;&nbsp; &nbsp;}\r\n}<\/pre>\n<p>\n\t\u5f88\u591a\u4eba\u5c31\u662f\u56e0\u4e3a\u6ca1\u6709\u5199session.getService().dispose();\u548cconnector.dispose();\u5bfc\u81f4\u6587\u4ef6\u53e5\u67c4\u6cc4\u6f0f\u7684\u3002\u5f53\u603b\u7684\u6587\u4ef6\u53e5\u67c4\u6570\u8d85\u8fc7\u7cfb\u7edf\u8bbe\u7f6e\u503c\uff08ulimit -n &nbsp;\u67e5\u770b\u540c\u4e00\u4e2a\u8fdb\u7a0b\u5141\u8bb8\u7684\u6700\u5927\u6587\u4ef6\u53e5\u67c4\u6570\uff09\uff0c\u5219\u629b\u51fa\u5f02\u5e38&ldquo;java.io.IOException: Too many open &nbsp;files&quot;,\u5bfc\u81f4\u65e0\u6cd5\u521b\u5efa\u65b0\u7684\u8fde\u63a5\uff0c\u670d\u52a1\u5668\u6302\u6389\u3002\n<\/p>\n<p>\n\t<strong>\u7248\u6743\u58f0\u660e\uff1a\u672c\u6587\u4e3a\u535a\u4e3b\u539f\u521b\u6587\u7ae0\uff0c\u672a\u7ecf\u535a\u4e3b\u5141\u8bb8\u4e0d\u5f97\u8f6c\u8f7d\u3002<\/strong>\u539f\u6587\u5730\u5740<a href=\"http:\/\/www.xttblog.com\/?p=446\" target=\"_blank\">http:\/\/www.xttblog.com\/?p=446<\/a><\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=446\">Mina\u6253\u5f00\u6587\u4ef6\u8fc7\u591a\u95ee\u9898\u89e3\u51b3\u65b9\u6848<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u5728\u4f7f\u7528mina\u5f00\u53d1\u8fc7\u7a0b\u4e2d\u9047\u5230\u4e86\u6253\u5f00\u6587\u4ef6\u8fc7\u591a\u95ee\u9898\uff0c\u4e00\u5927\u5806\u7684java.io.IOException: Too  [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[176,178,177],"class_list":["post-446","post","type-post","status-publish","format-standard","hentry","category-java","tag-mina","tag-too-many-open-files","tag-177"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/446","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=446"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions"}],"predecessor-version":[{"id":1996,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/446\/revisions\/1996"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=446"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=446"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=446"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}