Best model for implementing NIO?

In the process of converting our Java code, I use NIO, but I'm not sure how to design it.

My initial approach was to create a selector thread pool. If necessary, streams are started / destroyed, and channels are registered in the selector stream when they are connected / received cyclically. From there, each thread blocks select (), and when woken up starts the corresponding callback associated with each channel that has the selected key.

In addition to this "multiple selector stream" design, I also saw people say that they use the same selector stream and send stream pool. When the I / O operation is ready for execution, the selector notifies the dispatcher thread, which then processes the request. This model has the advantage of not blocking the I / O stream, but now we force all the IOs into one stream and are engaged in the synchronization / queue of events in the dispatcher.

In addition, I could not use one direct byte buffer to read each channel, passing it directly to the callback. Instead, I would have to copy the data every time a read occurs in the array and reset. (I think..)

What is the best way to implement this?

+6
java nio
source share
4 answers

Take a look at the Reactor template

http://gee.cs.oswego.edu/dl/cpjslides/nio.pdf

How you want your selectors to work really depends on your use. (Number of connections, message size, etc.)

What is the problem you are trying to solve by converting from IO to NIO?

+4
source share

You really have to look at Mina,

http://mina.apache.org/

It solves all the problems you mentioned.

+3
source share

I found the Java NIO ROX Tutorial useful for getting started with NIO.

Grizzly is another NIO infrastructure (similar to MINA).

+3
source share

Also look at netty , which is very fast and functionally rich, and is also used in large systems and large companies such as Redhat (jboss), Twitter, Facebook ....

+1
source share

All Articles