Reputation: 5818
I have written a server which processes requests concurrently using 1 thread per connection socket. But in the future it might grow much so I decided to change it already now for something more scalable as long as threads are quite expensive. I have read about 3 options - raw NIO, NIO framework like Netty, or Asynchronous IO from java SE7. But I have never used either of those so I am confused which of them to use for my task and how. The task is quite simple:
There is a sever which accepts connection. When it accepts one, it should allocate something like a listener for incoming requests (right now threads work for me that way). IE. when there is a connection, a new thread is not allocated. But when a formatted request comes, somehow the JVM invokes the required listener (which is associated with its channel or socket..) and processes the request. Then the thread is returned to the pool or sth and the listener is also waiting for incoming requests. THere might be 10k+ listeners. Also, the server itself might send a message to the client at any time asynchronously (so it needs to find the connection from the pool and send the message). Or multicast the message to 1k+ clients. The server accepts a custom protocol, so having some connection-break features, or message-size functionality is also a bonus.
I have been trying to check the above mentioned options for this, but I didnt quite get which one best suits my needs as long as I have never worked with any of those. I checked Netty and it seems like very heavyweight. Is my task easy to achieve with NIO or NIO2? Or shall I use sth like MINA or Netty? If it is easy to avoid programming mistakes, I would like to stick to raw APIs of java like NIO. But if it error prone, maybe its better to work with Netty?
Upvotes: 0
Views: 353
Reputation: 19682
Don't program with NIO directly; it's a lot more subtle than it first appears.
I don't like AIO either; its threading model is very confusing.
My recommendation would be that you go with Netty.
Upvotes: 1