国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學(xué)院 > 操作系統(tǒng) > 正文

Too many open files

2024-06-28 13:22:06
字體:
供稿:網(wǎng)友
Too many open files

部署到linux下的tomcat,今天發(fā)現(xiàn)包了“java.net.SocketException: Too many open files”,以前從來沒有遇到過,在此記錄一下: 徹底解決問題的是第三步, 所以,可以直接跳到第三步去看解決方法和步驟,當(dāng)日第一、第二步是我不斷探索,嘗試解決問題的過程,雖然沒有找到點(diǎn)上,但是還是有些意義的,因?yàn)閘inux切實(shí)有打開資源數(shù)量的限制,肯定需要修改的。異常信息:

Java代碼 收藏代碼
  1. ............
  2. Oct17,20115:22:41PMorg.apache.tomcat.util.net.JIoEndpoint$Acceptorrun
  3. SEVERE:Socketacceptfailed
  4. java.net.SocketException:Toomanyopenfiles
  5. atjava.net.PlainSocketImpl.socketAccept(NativeMethod)
  6. atjava.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:375)
  7. atjava.net.ServerSocket.implAccept(ServerSocket.java:470)
  8. atjava.net.ServerSocket.accept(ServerSocket.java:438)
  9. atorg.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:59)
  10. atorg.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:210)
  11. atjava.lang.Thread.run(Thread.java:636)
  12. Oct17,20115:22:43PMorg.apache.tomcat.util.net.JIoEndpoint$Acceptorrun
  13. SEVERE:Socketacceptfailed
  14. java.net.SocketException:Toomanyopenfiles
  15. atjava.net.PlainSocketImpl.socketAccept(NativeMethod)
  16. atjava.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:375)
  17. atjava.net.ServerSocket.implAccept(ServerSocket.java:470)
  18. atjava.net.ServerSocket.accept(ServerSocket.java:438)
  19. atorg.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:59)
  20. atorg.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:210)
  21. atjava.lang.Thread.run(Thread.java:636)
  22. Oct17,20115:22:44PMorg.apache.tomcat.util.net.JIoEndpoint$Acceptorrun
  23. SEVERE:Socketacceptfailed
  24. java.net.SocketException:Toomanyopenfiles
  25. atjava.net.PlainSocketImpl.socketAccept(NativeMethod)
  26. atjava.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:375)
  27. atjava.net.ServerSocket.implAccept(ServerSocket.java:470)
  28. atjava.net.ServerSocket.accept(ServerSocket.java:438)
  29. atorg.apache.tomcat.util.net.DefaultServerSocketFactory.acceptSocket(DefaultServerSocketFactory.java:59)
  30. atorg.apache.tomcat.util.net.JIoEndpoint$Acceptor.run(JIoEndpoint.java:210)
  31. atjava.lang.Thread.run(Thread.java:636)
  32. ............

原因: linux下有有文件限制,結(jié)果文件數(shù)操作最大限制,導(dǎo)致程序異常:問題是程序中有個靜態(tài)方法打開文件時忘記關(guān)閉。兩種解決方法,一是設(shè)置linux的最大文件打開數(shù)量(無法根本解決問題),二是解決程序中的bugs,即消除有問題的代碼。第一次解決解決:方法一、增大系統(tǒng)打開文件的數(shù)量(無法根本解決問題)、 1、默認(rèn)linux同時最大打開文件數(shù)量為1024個,用命令查看如下:ulimit -a:查看系統(tǒng)上受限資源的設(shè)置(open files (-n) 1024):

Java代碼 收藏代碼
  1. [root@****bin]#ulimit-a
  2. corefilesize(blocks,-c)0
  3. datasegsize(kbytes,-d)unlimited
  4. schedulingPRiority(-e)0
  5. filesize(blocks,-f)unlimited
  6. pendingsignals(-i)16384
  7. maxlockedmemory(kbytes,-l)32
  8. maxmemorysize(kbytes,-m)unlimited
  9. openfiles(-n)1024
  10. pipesize(512bytes,-p)8
  11. POSIXmessagequeues(bytes,-q)819200
  12. real-timepriority(-r)0
  13. stacksize(kbytes,-s)10240
  14. cputime(seconds,-t)unlimited
  15. maxuserprocesses(-u)16384
  16. virtualmemory(kbytes,-v)unlimited
  17. filelocks(-x)unlimited
  18. [root@****bin]#

2、可以修改同時打開文件的最大數(shù)基本可以解決:ulimit -n 4096

Java代碼 收藏代碼
  1. [root@****bin]#ulimit-n4096
  2. [root@****bin]#ulimit-a
  3. corefilesize(blocks,-c)0
  4. datasegsize(kbytes,-d)unlimited
  5. schedulingpriority(-e)0
  6. filesize(blocks,-f)unlimited
  7. pendingsignals(-i)16384
  8. maxlockedmemory(kbytes,-l)32
  9. maxmemorysize(kbytes,-m)unlimited
  10. openfiles(-n)4096
  11. pipesize(512bytes,-p)8
  12. POSIXmessagequeues(bytes,-q)819200
  13. real-timepriority(-r)0
  14. stacksize(kbytes,-s)10240
  15. cputime(seconds,-t)unlimited
  16. maxuserprocesses(-u)16384
  17. virtualmemory(kbytes,-v)unlimited
  18. filelocks(-x)unlimited
  19. [root@****bin]#

已經(jīng)修改了最大打開文件數(shù)。方法二、修改程序中的bugs:程序中有個靜態(tài)的方法打開文件后,沒有關(guān)閉文件,導(dǎo)致每次請求都會去打開文件,在程序中填入關(guān)閉輸入流的操作即可以:

Java代碼 收藏代碼
  1. publicstaticList<GpsPoint>getArrayList()throwsIOException{
  2. List<GpsPoint>pointList=null;
  3. //讀取配置文件
  4. InputStreamin=ParseGpsFile.class.getClassLoader().getResourceAsStream("GPS1.TXT");
  5. //讀路徑出錯,換另一種方式讀取配置文件
  6. if(null==in){
  7. System.out.println("讀取文件失敗");
  8. returnpointList;
  9. }
  10. pointList=newArrayList<GpsPoint>();
  11. try{
  12. BufferedReaderbr=newBufferedReader(newInputStreamReader(in));
  13. Stringlongtude="";
  14. Stringlatude="";
  15. Stringelevation="";
  16. while((longtude=br.readLine())!=null){
  17. //讀下一行數(shù)據(jù),讀緯度
  18. latude=br.readLine();
  19. if(null==latude){
  20. //退出循環(huán)
  21. break;
  22. }
  23. //讀下一行數(shù)據(jù),讀海拔
  24. elevation=br.readLine();
  25. if(null==latude){
  26. //退出循環(huán)
  27. break;
  28. }
  29. //加入一個點(diǎn)
  30. pointList.add(gps2point(longtude,latude,elevation));
  31. }
  32. in.close();
  33. System.out.println("/n/n");
  34. }catch(Exceptione){
  35. in.close();
  36. e.printStackTrace();
  37. }
  38. returnpointList;
  39. }

問題徹底解決-----第二次解決:實(shí)際測試后發(fā)現(xiàn)這個問題還沒有解決,最終又找了些方法,經(jīng)過一段時間的測試,似乎解決了問題:具體步驟如下:linux為redhat服務(wù)器版本(非個人版),必須設(shè)置的內(nèi)容:1、/etc/pam.d/login 添加

Java代碼 收藏代碼
  1. sessionrequired/lib/security/pam_limits.so

# 注意看這個文件的注釋具體文件的內(nèi)容為:

Java代碼 收藏代碼
  1. [root@****~]#vi/etc/pam.d/login
  2. #%PAM-1.0
  3. auth[user_unknown=ignoresuccess=okignore=ignoredefault=bad]pam_securetty.so
  4. authincludesystem-auth
  5. accountrequiredpam_nologin.so
  6. accountincludesystem-auth
  7. passWordincludesystem-auth
  8. #pam_selinux.socloseshouldbethefirstsessionrule
  9. sessionrequiredpam_selinux.soclose
  10. sessionoptionalpam_keyinit.soforcerevoke
  11. sessionrequiredpam_loginuid.so
  12. sessionincludesystem-auth
  13. sessionoptionalpam_console.so
  14. #pam_selinux.soopenshouldonlybefollowedbysessionstobeexecutedintheusercontext
  15. sessionrequiredpam_selinux.soopen
  16. ~
  17. "/etc/pam.d/login"15L,644C

修改后的內(nèi)容:

Java代碼 收藏代碼
  1. -bash:[root@****:commandnotfound
  2. [root@****~]#clear
  3. [root@****~]#cat/etc/pam.d/login
  4. #%PAM-1.0
  5. auth[user_unknown=ignoresuccess=okignore=ignoredefault=bad]pam_securetty.so
  6. authincludesystem-auth
  7. accountrequiredpam_nologin.so
  8. accountincludesystem-auth
  9. passwordincludesystem-auth
  10. #pam_selinux.socloseshouldbethefirstsessionrule
  11. sessionrequiredpam_selinux.soclose
  12. sessionoptionalpam_keyinit.soforcerevoke
  13. sessionrequiredpam_loginuid.so
  14. sessionincludesystem-auth
  15. sessionoptionalpam_console.so
  16. #pam_selinux.soopenshouldonlybefollowedbysessionstobeexecutedintheusercontext
  17. sessionrequiredpam_selinux.soopen
  18. #kevin.xieadded,fixed'toomanyopenfile'bug,limitopenmaxfiles1024,2011-10-24
  19. sessionrequired/lib/security/pam_limits.so
  20. [root@****~]#

2. /etc/security/limits.conf 添加

Java代碼 收藏代碼
  1. root–nofile1006154

root 是一個用戶,如果是想所有用戶生效的話換成 * ,設(shè)置的數(shù)值與硬件配置有關(guān),別設(shè)置太大了。修改前內(nèi)容

Java代碼 收藏代碼
  1. [root@****~]#cat/etc/security/limits.conf
  2. #/etc/security/limits.conf
  3. #
  4. #Eachlinedescribesalimitforauserintheform:
  5. #
  6. #<domain><type><item><value>
  7. #
  8. #Where:
  9. #<domain>canbe:
  10. #-anusername
  11. #-agroupname,with@groupsyntax
  12. #-thewildcard*,fordefaultentry
  13. #-thewildcard%,canbealsousedwith%groupsyntax,
  14. #formaxloginlimit
  15. #
  16. #<type>canhavethetwovalues:
  17. #-"soft"forenforcingthesoftlimits
  18. #-"hard"forenforcinghardlimits
  19. #
  20. #<item>canbeoneofthefollowing:
  21. #-core-limitsthecorefilesize(KB)
  22. #-data-maxdatasize(KB)
  23. #-fsize-maximumfilesize(KB)
  24. #-memlock-maxlocked-in-memoryaddressspace(KB)
  25. #-nofile-maxnumberofopenfiles
  26. #-rss-maxresidentsetsize(KB)
  27. #-stack-maxstacksize(KB)
  28. #-cpu-maxCPUtime(MIN)
  29. #-nproc-maxnumberofprocesses
  30. #-as-addressspacelimit
  31. #-maxlogins-maxnumberofloginsforthisuser
  32. #-maxsyslogins-maxnumberofloginsonthesystem
  33. #-priority-theprioritytorunuserprocesswith
  34. #-locks-maxnumberoffilelockstheusercanhold
  35. #-sigpending-maxnumberofpendingsignals
  36. #-msgqueue-maxmemoryusedbyPOSIXmessagequeues(bytes)
  37. #-nice-maxnicepriorityallowedtoraiseto
  38. #-rtprio-maxrealtimepriority
  39. #
  40. #<domain><type><item><value>
  41. #
  42. #*softcore0
  43. #*hardrss10000
  44. #@studenthardnproc20
  45. #@facultysoftnproc20
  46. #@facultyhardnproc50
  47. #ftphardnproc0
  48. #@student-maxlogins4
  49. #Endoffile
  50. [root@****~]#
  51. [root@****~]#cat/etc/security/limits.conf
  52. #/etc/security/limits.conf
  53. #
  54. #Eachlinedescribesalimitforauserintheform:
  55. #
  56. #<domain><type><item><value>
  57. #
  58. #Where:
  59. #<domain>canbe:
  60. #-anusername
  61. #-agroupname,with@groupsyntax
  62. #-thewildcard*,fordefaultentry
  63. #-thewildcard%,canbealsousedwith%groupsyntax,
  64. #formaxloginlimit
  65. #
  66. #<type>canhavethetwovalues:
  67. #-"soft"forenforcingthesoftlimits
  68. #-"hard"forenforcinghardlimits
  69. #
  70. #<item>canbeoneofthefollowing:
  71. #-core-limitsthecorefilesize(KB)
  72. #-data-maxdatasize(KB)
  73. #-fsize-maximumfilesize(KB)
  74. #-memlock-maxlocked-in-memoryaddressspace(KB)
  75. #-nofile-maxnumberofopenfiles
  76. #-rss-maxresidentsetsize(KB)
  77. #-stack-maxstacksize(KB)
  78. #-cpu-maxCPUtime(MIN)
  79. #-nproc-maxnumberofprocesses
  80. #-as-addressspacelimit
  81. #-maxlogins-maxnumberofloginsforthisuser
  82. #-maxsyslogins-maxnumberofloginsonthesystem
  83. #-priority-theprioritytorunuserprocesswith
  84. #-locks-maxnumberoffilelockstheusercanhold
  85. #-sigpending-maxnumberofpendingsignals
  86. #-msgqueue-maxmemoryusedbyPOSIXmessagequeues(bytes)
  87. #-nice-maxnicepriorityallowedtoraiseto
  88. #-rtprio-maxrealtimepriority
  89. #
  90. #<domain><type><item><value>
  91. #
  92. #*softcore0
  93. #*hardrss10000
  94. #@studenthardnproc20
  95. #@facultysoftnproc20
  96. #@facultyhardnproc50
  97. #ftphardnproc0
  98. #@student-maxlogins4
  99. #kevin.xieadded,fixed'toomanyopenfile'bug,limitopenmaxfiles1024,2011-10-24
  100. *-nofile102400
  101. #Endoffile
  102. [root@****~]#

修改后的內(nèi)容

Java代碼 收藏代碼

3. 修改 /etc/rc.local 添加

Java代碼 收藏代碼
  1. echo8061540>/proc/sys/fs/file-max

修改前內(nèi)容

Java代碼 收藏代碼
  1. [root@****~]#cat/proc/sys/fs/file-max
  2. 4096
  3. [root@****~]#

修改后內(nèi)容

Java代碼 收藏代碼
  1. [root@****~]#cat/proc/sys/fs/file-max
  2. 4096000
  3. [root@****~]#

做完3個步驟,就可以了。**************************************補(bǔ)充說明:/proc/sys/fs/file-max該文件指定了可以分配的文件句柄的最大數(shù)目。如果用戶得到的錯誤消息聲明由于打開文件數(shù)已經(jīng)達(dá)到了最大值,從而他們不能打開更多文件,則可能需要增加該值。可將這個值設(shè)置成有任意多個文件,并且能通過將一個新數(shù)字值寫入該文件來更改該值。缺省設(shè)置:4096/proc/sys/fs/file-nr該文件與 file-max 相關(guān),它有三個值:已分配文件句柄的數(shù)目已使用文件句柄的數(shù)目文件句柄的最大數(shù)目該文件是只讀的,僅用于顯示信息。關(guān)于“打開文件數(shù)”限制Linux系統(tǒng)上對每一個用戶可使用的系統(tǒng)資源都是有限制的,這是多用戶系統(tǒng)必然要采用的一種資源管理手段,試想假如沒有這種機(jī)制,那么任何一個普通用戶寫一個死循環(huán)程序,用不了多久系統(tǒng)就要“拒絕服務(wù)”了。今天我遇到了tomcat日志報的錯誤信息”too many open files”,第一意識就想到了是ulimit控制的”open files“限制。然而問題來了。我在/etc/profile里加入了 ulimit -n 4096保存之后,普通用戶登錄的時候均會收到一條錯誤信息ulimit: open files: cannot modify limit: Operation not permitted。然后普通用戶的open files限制還是默認(rèn)值1024。然后開始在互聯(lián)網(wǎng)上搜索關(guān)于ulimit的信息。互聯(lián)網(wǎng)果然方便,信息鋪天蓋地。大家也可以搜一下試一下。其中我了解到兩個以前不知道的相關(guān)內(nèi)容。第一個是內(nèi)核參數(shù) fs.file-max ,影射為 /proc/sys/fs/file-max第二個是配置文件 /etc/security/limits.conf其中大部分的信息中提到 將 /proc/sys/fs/file-max的值設(shè)置為4096和ulimit -n 4096是相同的效果。對此我很懷疑,為什么呢?首先ulimit 是一個普通用戶也可以使用的命令,而fs.file-max只有root有權(quán)設(shè)置。其次,很明顯fs.file-max是一個全局的設(shè)置,而ulimit 是一個局部的設(shè)置,很明顯的是不相同的。帶著疑慮,又在網(wǎng)上搜索了許久,未果(實(shí)際上是我搜索的關(guān)鍵字不夠準(zhǔn)確)。最后終于在內(nèi)核文檔/usr/src/linux/Documentation/sysctl/fs.txt里找到下面一段話:file-max & file-nr:The kernel allocates file handles dynamically, but as yet it doesn’t free them again. The value in file-max denotes the maximum number of file-handles that the Linux kernel will allocate. When you get lots of error messages about running out of file handles, you might want to increase this limit.The three values in file-nr denote the number of allocated file handles, the number of unused file handles and the maximum number of file handles. When the allocated file handles come close to the maximum, but the number of unused file handles is significantly greater than 0, you’ve encountered a peak in your usage of file handles and you don’t need to increase the maximum.這兩段話的大致意思是:內(nèi)核動態(tài)地分配和釋放“file handles”(句柄)。file-max的值是內(nèi)核所能分配到的最大句柄數(shù)。當(dāng)你收到大量關(guān)于句柄用完的錯誤信息時,你可以需要增加這個值以打破老的限制。file-nr中的三個值的含意分別是:系統(tǒng)已經(jīng)分配出去(正在使用)的句柄數(shù),沒有用到的句柄數(shù)和所有分配到的最大句柄數(shù)。當(dāng)分配出去的句柄數(shù)接近 最大句柄數(shù),而“無用的句柄數(shù)”大于零時,表明你遇到了一個“句柄”使用高峰,這意為著你不需要增加file-max的值。看完這段話,相信大家都明白了。file-max是系統(tǒng)全局的可用句柄數(shù)。根據(jù)我后來又翻查的信息,以及對多個系統(tǒng)的查看求證,這個參數(shù)的默認(rèn)值是跟內(nèi)存大小有關(guān)系的,增加物理內(nèi)存以后重啟機(jī)器,這個值會增大。大約1G內(nèi)存10萬個句柄的線性關(guān)系。再回過頭來看這兩段話,不知道你意識到了沒有,文中只提到了file-max的增加,而沒有提到了該值的減少。那些在操作ulimit時同時操 作了file-max的哥們兒,估計無一例外地將file-max設(shè)置成了4096或者2048。但以似乎也沒有因此而導(dǎo)致系統(tǒng)無法打開文件或者建議連 接。(實(shí)際上,我將file-max的值設(shè)備成256,然后使用shell編寫用vi打開500個文件角本運(yùn)行,并沒有得到任何錯誤信息,查看file- nr的值,系統(tǒng)當(dāng)前分配的句柄值已經(jīng)遠(yuǎn)超過了后面的最大值。所以我猜想對于file-max的任何減少的操作都是毫無意義的,姑且不去管他。實(shí)踐中需要減 少file-max的時候總是不多見的。 )實(shí)事證明我犯了一個致命的錯誤。我測試的時候使用的是root用戶,而當(dāng)我再次使用普通用戶測試的時候,預(yù)料中的錯誤信息出現(xiàn)了:”Too many open files in system”。可見,file-max的減少對系統(tǒng)也是影響力的。前面的結(jié)論“所以我猜想對于file-max的任何減少的操作都是毫無意義的”是錯誤 的。然后便是/etc/security/limits.conf文件,這個文件很簡單,一看就能明白。于是我按照注釋中描述的格式兩個兩行:*  soft  nofile  4096*  hard  nofile  4096恐怖的是,網(wǎng)上居然有人說改了這個設(shè)置是需要重啟系統(tǒng)的!實(shí)在是讓人想不通啊,鼎鼎大名的UNIX系統(tǒng),怎么可能因?yàn)檫@么小小的一個改動就需要 重啟系統(tǒng)呢?結(jié)果當(dāng)我再次以普通用戶登錄的時候,那個”ulimit: open files: cannot modify limit: Operation not permitted”提示沒有了,查看ulimit -n,果然已經(jīng)變成了4096。linux lsof 修改句柄限制(轉(zhuǎn))在Linux下,我們使用ulimit -n 命令可以看到單個進(jìn)程能夠打開的最大文件句柄數(shù)量(socket連接也算在里面)。系統(tǒng)默認(rèn)值1024。對于一般的應(yīng)用來說(象Apache、系統(tǒng)進(jìn)程)1024完全足夠使用。但是如何象squid、MySQL、java等單進(jìn)程處理大量請求的應(yīng)用來說就有點(diǎn)捉襟見肘了。如果單個進(jìn)程打開的文件句柄數(shù)量超過了系統(tǒng)定義的值,就會提到“too many files open”的錯誤提示。如何知道當(dāng)前進(jìn)程打開了多少個文件句柄呢?下面一段小腳本可以幫你查看:lsof -n |awk ‘{print $2}’|sort|uniq -c |sort -nr|more在系統(tǒng)訪問高峰時間以root用戶執(zhí)行上面的腳本,可能出現(xiàn)的結(jié)果如下:# lsof -n|awk ‘{print $2}’|sort|uniq -c |sort -nr|more131 2420457 2424457 2423156 24264其中第一行是打開的文件句柄數(shù)量,第二行是進(jìn)程號。得到進(jìn)程號后,我們可以通過ps命令得到進(jìn)程的詳細(xì)內(nèi)容。ps -aef|grep 24204mysql 24204 24162 99 16:15 ? 00:24:25 /usr/sbin/mysqld哦,原來是mysql進(jìn)程打開最多文件句柄數(shù)量。但是他目前只打開了131個文件句柄數(shù)量,遠(yuǎn)遠(yuǎn)底于系統(tǒng)默認(rèn)值1024。但是如果系統(tǒng)并發(fā)特別大,尤其是squid服務(wù)器,很有可能會超過1024。這時候就必須要調(diào)整系統(tǒng)參數(shù),以適應(yīng)應(yīng)用變化。Linux有硬性限制和軟性限制。可以通過ulimit來設(shè)定這兩個參數(shù)。方法如下,以root用戶運(yùn)行以下命令:ulimit -HSn 4096以上命令中,H指定了硬性大小,S指定了軟性大小,n表示設(shè)定單個進(jìn)程最大的打開文件句柄數(shù)量。個人覺得最好不要超過4096,畢竟打開的文件句柄數(shù)越多響應(yīng)時間肯定會越慢。設(shè)定句柄數(shù)量后,系統(tǒng)重啟后,又會恢復(fù)默認(rèn)值。如果想永久保存下來,可以修改.bash_profile文件,可以修改 /etc/profile 把上面命令加到最后。 仍未處理的問題:為什么redhat9的個人版按照以上的方式修改1024tcp連接限制依然不行呢?是因?yàn)閭€人版最多支持1024個tcp連接,還是修改方式、相關(guān)文件會有所不同?以上內(nèi)容,來源網(wǎng)絡(luò)并加自己親自測試,經(jīng)過測試,似乎沒有再出現(xiàn)過問題,但不知道是否真的解決,有待更長時間的測試看看第三次解決--還解決不了,就徹底無語了(經(jīng)過壓力測試,運(yùn)行7天再也沒有出現(xiàn)該問題)問題的原因是:原來的MINA2程序之關(guān)了IoSession,并沒有關(guān)閉IoConnector實(shí)例,但恰恰就是因?yàn)闆]有關(guān)閉每次打開的IoConnector實(shí)例,造成了"Too many open files ".原來的程序:

Java代碼 收藏代碼
  1. /**
  2. *<pre><b>功能描述:</b>獲取異步的session實(shí)例。
  3. *
  4. *@author:Kevin.xie
  5. *<b>創(chuàng)建日期:</b>2011-9-15上午10:06:27
  6. *
  7. *@return
  8. *
  9. *<b>修改歷史:</b>(修改人,修改時間,修改原因/內(nèi)容)
  10. *
  11. *</pre>
  12. */
  13. publicstaticIoSessiongetSession1(){
  14. //創(chuàng)建客戶端連接器
  15. IoConnectorconnector=newNioSocketConnector();
  16. //設(shè)置事件處理器
  17. connector.setHandler(newWebClientHandler());
  18. //設(shè)置編碼過濾器和按行讀取數(shù)據(jù)模式
  19. connector.getFilterChain()
  20. .addLast("codec",newProtocolCodecFilter(newObdDemuxingProtocolCodecFactory(false)));
  21. //創(chuàng)建連接
  22. ConnectFuturefuture=connector.connect(newInetSocketAddress(ServerConfigBoundle.getServerIp(),
  23. ServerConfigBoundle.getServerPort()));
  24. //等待連接創(chuàng)建完成
  25. future.awaitUninterruptibly();
  26. //獲取連接會話
  27. IoSessionsession=future.getSession();
  28. returnsession;
  29. }
  30. /**
  31. *<pre><b>功能描述:</b>必須要關(guān)閉Connector和IoSession
  32. *@author:Kevin.xie
  33. *<b>創(chuàng)建日期:</b>2011-10-20上午10:20:54
  34. *
  35. *@paramsession要關(guān)閉的session
  36. *
  37. *<b>修改歷史:</b>(修改人,修改時間,修改原因/內(nèi)容)
  38. *
  39. *</pre>
  40. */
  41. publicstaticvoidcloseSession(IoSessionsession){
  42. if(session!=null&&!session.isClosing()){
  43. //沒有關(guān)閉,就關(guān)閉
  44. session.close(true);
  45. session=null;
  46. }
  47. }

修改后的程序

Java代碼 收藏代碼
  1. /**
  2. *
  3. *<pre><b>功能描述:</b>獲取IoConnector和異步的session實(shí)例
  4. *無法關(guān)閉。特別的提醒,NioSocketConnector也要關(guān)閉。
  5. *函數(shù)名是dispose()。這點(diǎn)特別重要。這次出現(xiàn)toomanyopenfiles的問題根源在這里
  6. *
  7. *@author:Kevin.xie
  8. *<b>創(chuàng)建日期:</b>2011-9-15上午10:06:27
  9. *
  10. *@return
  11. *
  12. *<b>修改歷史:</b>(修改人,修改時間,修改原因/內(nèi)容)
  13. *
  14. *</pre>
  15. */
  16. publicstaticMap<String,Object>getConnectorAndSession(){
  17. //創(chuàng)建客戶端連接器
  18. IoConnectorconnector=newNioSocketConnector();
  19. //設(shè)置事件處理器
  20. connector.setHandler(newWebClientHandler());
  21. //設(shè)置編碼過濾器和按行讀取數(shù)據(jù)模式
  22. connector.getFilterChain()
  23. .addLast("codec",newProtocolCodecFilter(newObdDemuxingProtocolCodecFactory(false)));
  24. //創(chuàng)建連接
  25. ConnectFuturefuture=connector.connect(newInetSocketAddress(ServerConfigBoundle.getServerIp(),
  26. ServerConfigBoundle.getServerPort()));
  27. //等待連接創(chuàng)建完成
  28. future.awaitUninterruptibly();
  29. //獲取連接會話
  30. IoSessionsession=future.getSession();
  31. Map<String,Object>map=newHashMap<String,Object>();
  32. map.put(CONNECTOR_KEY,connector);
  33. map.put(SESSION_KEY,session);
  34. returnmap;
  35. }
  36. /**
  37. *
  38. *<pre><b>功能描述:</b>必須要關(guān)閉Connector和IoSession
  39. *特別的提醒,NioSocketConnector也要關(guān)閉。
  40. *函數(shù)名是dispose()。這點(diǎn)特別重要。這次出現(xiàn)toomanyopenfiles的問題根源在這里
  41. *@author:Kevin.xie
  42. *<b>創(chuàng)建日期:</b>2011-10-20上午10:20:54
  43. *
  44. *@paramconnector要關(guān)閉的IoConnector,不關(guān)閉會報toomanyopenfiles錯誤
  45. *@paramsession要關(guān)閉的session
  46. *
  47. *<b>修改歷史:</b>(修改人,修改時間,修改原因/內(nèi)容)
  48. *
  49. *</pre>
  50. */
  51. publicstaticvoidcloseConnectorAndSession(IoConnectorconnector,IoSessionsession){
  52. if(session!=null&&!session.isClosing()){
  53. //沒有關(guān)閉,就關(guān)閉
  54. session.close(true);
  55. session=null;
  56. }
  57. if(connector!=null&&!(connector.isDisposing()||connector.isDisposed())){
  58. //沒有關(guān)閉,就關(guān)閉
  59. connector.dispose();
  60. connector=null;
  61. }
  62. }
  63. ]

用完后一定要釋放資源:

Java代碼 收藏代碼
  1. Map<String,Object>resultMap=SocketUtils.getConnectorAndSession();
  2. IoSessionsession=(IoSession)resultMap.get(SocketUtils.SESSION_KEY);
  3. IoConnectorconnector=(IoConnector)resultMap.get(SocketUtils.CONNECTOR_KEY);
  4. ............
  5. ............
  6. //主動關(guān)閉連接
  7. SocketUtils.closeConnectorAndSession(connector,session);

同時在配置文件 /etc/security/limits.conf 加了一個配置(該不該問題不大):# kevin.xie added, fixed 'too many open file' bug', 2012-01-04* soft nofile 65536* hard nofile 65536

Java代碼 收藏代碼
    1. #第二次解決添加的內(nèi)容
    2. #kevin.xieadded,fixed'toomanyopenfile'bug,limitopenmaxfiles1024,2011-10-24
    3. *-nofile102400
    4. #第三次(本次)解決添加的問題(不過這個應(yīng)該可以不修改,沒有印證,也懶得修改了)
    5. #kevin.xieadded,fixed'toomanyopenfile'bug',2012-01-04
    6. *softnofile65536
    7. *hardnofile65536
    8. 原帖子出自--http://xieyanhua.CUOxin.com/blog/1198708

發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 德令哈市| 班戈县| 三门县| 鹰潭市| 乌苏市| 定结县| 揭阳市| 土默特左旗| 阜南县| 巩留县| 济宁市| 内江市| 蛟河市| 哈密市| 定安县| 黄陵县| 彰武县| 靖边县| 湟中县| 谷城县| 仁化县| 西充县| 马山县| 徐闻县| 诸暨市| 增城市| 寻乌县| 云安县| 通辽市| 新龙县| 遂宁市| 莆田市| 西乌珠穆沁旗| 双峰县| 晴隆县| 通化县| 祁门县| 水城县| 法库县| 桦甸市| 通渭县|