java.nio中的选择器一次可以选择多少个连接?

发布于 2024-10-03 21:06:48 字数 1625 浏览 7 评论 0原文

我对新的 java 套接字 NIO 做了一些研究。我正在使用 MINA 构建一个模拟服务器,该服务器接受来自许多客户端(大约 1000 个)的连接并处理从它们收到的数据。我还设置了客户端模拟器,它创建大约 300 个客户端连接并使用线程将数据发送到服务器。结果是一些连接被服务器中止。代码如下

 try {
  listener = new NioSocketAcceptor(ioThread);

  listener.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageCodecFactory()));
  listener.getFilterChain().addLast("thread", new ExecutorFilter(100, 150));
  listener.setHandler(new IncomingMessageHandler(serverMessageHandler));

  listener.bind(new InetSocketAddress(PORT));
 }
 catch (IOException ioe) {
 }

这是处理程序,Session 是我的类,用于来自客户端的每个连接

 @Override
 public void sessionCreated(IoSession session) throws Exception {
  new Session(session.getRemoteAddress(), handler, session);
  super.sessionCreated(session);
 }

 @Override
 public void messageReceived(IoSession session, Object message)
   throws Exception {

  Message m = Message.wrap((MessagePOJO)message);
  if (m != null) {
   Session s = SessionManager.instance.get(session.getRemoteAddress());
   if (s != null) {
    s.submit(m);
    ArmyServer.instance.tpe.submit(s);
   }
  }

  super.messageReceived(session, message);
 }

 @Override
 public void sessionClosed(IoSession session) throws Exception {
  Session s = SessionManager.instance.get(session.getRemoteAddress());
  if (s != null)
   s.disconnect();
  super.sessionClosed(session);
 }

和客户端模拟器,SIZE ~300 - 400

     for (int i = 0; i < SIZE; i++) {
  clients[i] = new Client(i);
  pool[i] = new Thread(clients[i]);
  pool[i].start();
 }

所以问题是 Mina 一次可以接受多少个连接?或者我的代码有什么错误吗?

I did a little research about new java socket NIO. I am using MINA for building a simulated server which accept connection from many clients(about 1000) and process the data received from them. I also set up the client simulator which creates around 300 client connection and send data to server using thread. And the result is some of the connection is aborted by the server. Code is below

 try {
  listener = new NioSocketAcceptor(ioThread);

  listener.getFilterChain().addLast("codec", new ProtocolCodecFilter(new MessageCodecFactory()));
  listener.getFilterChain().addLast("thread", new ExecutorFilter(100, 150));
  listener.setHandler(new IncomingMessageHandler(serverMessageHandler));

  listener.bind(new InetSocketAddress(PORT));
 }
 catch (IOException ioe) {
 }

And here is the handler, Session is my class for each connection from client

 @Override
 public void sessionCreated(IoSession session) throws Exception {
  new Session(session.getRemoteAddress(), handler, session);
  super.sessionCreated(session);
 }

 @Override
 public void messageReceived(IoSession session, Object message)
   throws Exception {

  Message m = Message.wrap((MessagePOJO)message);
  if (m != null) {
   Session s = SessionManager.instance.get(session.getRemoteAddress());
   if (s != null) {
    s.submit(m);
    ArmyServer.instance.tpe.submit(s);
   }
  }

  super.messageReceived(session, message);
 }

 @Override
 public void sessionClosed(IoSession session) throws Exception {
  Session s = SessionManager.instance.get(session.getRemoteAddress());
  if (s != null)
   s.disconnect();
  super.sessionClosed(session);
 }

And the client simulator, SIZE ~300 - 400

     for (int i = 0; i < SIZE; i++) {
  clients[i] = new Client(i);
  pool[i] = new Thread(clients[i]);
  pool[i].start();
 }

So the question is how many connections can Mina accept one at a time? Or is there any wrong in my code?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

如日中天 2024-10-10 21:06:48

您可能只是让服务器超载了。由于操作系统和 CPU 的限制,它一次只能接受这么多请求。一旦 ServerSocket 上的待处理请求数量超过侦听队列长度,连接将被拒绝。

尝试增加监听队列长度(ServerSocket.bind() 中的 backlog 参数)和/或在客户端 for 循环中添加少量 sleep()。

我不知道 Mina 的详细信息,但除了有多少线程处理消息之外,您可能还想确保有超过 1 个线程接受。

You may just be overloading the server. It's only going to be able to accept so many requests at a time due to OS and CPU limits. Once there are more pending requests than the listen queue length on the ServerSocket, connections will be rejected.

Try increasing the listen queue length (the backlog parameter in ServerSocket.bind()) and / or adding a small amount of sleep() in the client for loop.

I do not know the details of Mina, but you may also want to make sure you have more than 1 Thread accepting in addition to how many threads you have handling messages.

极度宠爱 2024-10-10 21:06:48

据我所知,对于选择器可以选择的通道数量没有任何记录限制。通常,Integer.MAX_VALUE 或类似内容会有实现限制。对于这种特殊情况,我假设限制在于 SelectorProvider 已实现,我敢打赌它是大多数 JVM 上本机的...

相关问题:

相关文章:

From what I can see there is no documented limit on how many channels a selector can select from. Typically there will be an implementation limit on Integer.MAX_VALUE or something similar. For this particular case, I assume the limit lies in how the SelectorProvider is implemented, and I bet it's native on most JVMs...

Related question:

Related article:

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文