How many connections can selector in java.nio select one at a time?

随声附和 提交于 2019-12-24 11:47:54

问题


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?


回答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.




回答2:


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:

  • select() max sockets

Related article:

  • select system call limitation in Linux


来源:https://stackoverflow.com/questions/4243585/how-many-connections-can-selector-in-java-nio-select-one-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!