java.nio中的selector可以选择多少个连接,一次选择一个? [英] How many connections can selector in java.nio select one at a time?

查看:500
本文介绍了java.nio中的selector可以选择多少个连接,一次选择一个?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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) {
 }

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

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);
 }

而客户端模拟器SIZE〜300 - 400

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();
 }

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

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

推荐答案

您可能只是过载服务器。由于操作系统和CPU限制,它一次只能接受这么多的请求。一旦在ServerSocket上有比listen队列长度多的挂起请求,连接将被拒绝。

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.

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

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.

我不知道Mina的详细信息,确保你有超过1个线程接受除了你有多少线程你处理消息。

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.

这篇关于java.nio中的selector可以选择多少个连接,一次选择一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆