mina socket 通信 Too many open files Failed to open a selector
Posted 纯洁的明依
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mina socket 通信 Too many open files Failed to open a selector相关的知识,希望对你有一定的参考价值。
服务背景:
流数据接收--> 解析过滤-->调用socket服务 写入mina服务。
服务器异常日志:
java.io.IOException: Too many open files Failed to open a selector
Failed to create a new instance of org.apache.mina.transport.socket.nio.NioProcessor:null
定位到问题代码:
public void init() {
NiosocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.defaultCharset())));
connector.setConnectTimeoutMillis(10000);
connector.setHandler(new SocketClientHandler());
connector.getSessionConfig().setReceiveBufferSize(10240); // 设置接收缓冲区的大小
connector.getSessionConfig().setSendBufferSize(10240);// 设置输出缓冲区的大小
ConnectFuture cf = connector.connect(
new InetSocketAddress(socketConfig.getSocketServerIp(),
socketConfig.getSocketServerPort()));
// 等待连接创建完成
cf.awaitUninterruptibly();
session = cf.getSession();
}
问题产生原因:
当mina服务端长时间连接不上 ,并发连接特别多的时候 都会导致上述问题。
解决方案:当连接不成功的时候 进行施放句柄。
修改之后:
public void init() {
NioSocketConnector connector = new NioSocketConnector();
connector.getFilterChain().addLast("codec",
new ProtocolCodecFilter(new TextLineCodecFactory(Charset.defaultCharset())));
connector.setConnectTimeoutMillis(10000);
connector.setHandler(new SocketClientHandler());
connector.getSessionConfig().setReceiveBufferSize(10240); // 设置接收缓冲区的大小
connector.getSessionConfig().setSendBufferSize(10240);// 设置输出缓冲区的大小
ConnectFuture cf = connector.connect(
new InetSocketAddress(socketConfig.getSocketServerIp(),
socketConfig.getSocketServerPort()));
// 等待连接创建完成
cf.awaitUninterruptibly();
if (cf.isDone()) {
if (!cf.isConnected()) {
log.error("fail connect {} {}", socketConfig.getSocketServerIp(),
socketConfig.getSocketServerPort());
connector.dispose();
throw new RuntimeException("Not connect to Mina Server");
} else {
session = cf.getSession();
}
}
}
持续分享个人的成长经历,希望为你的职场发展带来些新思路,欢迎扫码关注我!
你若喜欢,为小编点个在看哦
以上是关于mina socket 通信 Too many open files Failed to open a selector的主要内容,如果未能解决你的问题,请参考以下文章
python valueError:too many values unpack,有人知道么?
kernel: nfsd: too many open TCP sockets, consider increasing the number of threads