Asynchronous IO in Java? - java

What options for async io (socket-based) are there in java other then java.nio? Also does java.nio use threads in the backround (as I think .NET's async-socket-library does, maybe it's been changed) or is it "true" async io using a proper select call?

Java's NIO package (as of Java6), provides support for non-blocking I/O only, via Selectors. Java7 is hopefully going to ship with NIO.2, which includes asynchronous I/O support. Today, your best bet is to make use of a framework. ARMistice mentioned Mina. Here are some others.
Grizzly. This is the I/O core for Sun's GlassFish server. Grizzly provides a facility for doing asynchronous reads/writes (via a queue model). It supports TCP and UDP alike. I've used Grizzly in a couple of projects. There are things I like and dislike about the framework, but to detail this is really another topic. I will say that it's quite easy to get something up and running and Grizzly does a lot of the heavy lifting for you.
Netty. This project comes from one of the original authors of the Mina project. I haven't used this one so I don't know about its support for asynchronous I/O. You should take a look.
Now, with regard to your question about threads, NIO Selectors do not use threads for non-blocking I/O. In JDK6 they use select() under Windows and the epoll facility on newer Linux kernels. For asynchronous I/O, threading details depend on the framework.

JAVA 7 arrived so new answer is NIO.2 with Future class. Example :
On server side:
final AsynchronousServerSocketChannel serverSocket=
AsynchronousServerSocketChannel.open().bind(new InetSocketAddress("127.0.0.1", 2587)); // Listening on port 2587 for client connection
Future<AsynchronousSocketChannel> future= serverSocket.accept();
final AsynchronousSocketChannel clientSocket= future.get(); // now it's blocking, useful: future.isDone() and .isCancelled()
//Do whatever you want ..
InputStream stream = Channels.newInputStream(clientSocket) (...)
On client side:
AsynchronousSocketChannel clientChannel = AsynchronousSocketChannel.open();
Future connected = localSocket.connect(ourServerSocketAddress);
// later: if(future.isDone())
connected.get();
//Send something
OutputStream os = Channels.newOutputStream(clientChannel );
os.write (...)
Update:
If you can use actor model then AKKA TCP IO would be even better.

Another suggestion in regards to libs would be Naga (http://naga.googlecode.com). It is a bit less like a framework and more like a library. It tries to look more like the ordinary java sockets, if that is your cup of tea. It's minimalistic compared to Grizzly, Mina and Netty.

java.nio is just a package - a collection of "dumb" classes - by itself it does not employ any use of threads. When used properly, such as in the Reactor design pattern, you can achieve proper, fully-scalable, asynchronous I/O.

If you are interested in using it for Network Stuff. A really good choice is:
http://mina.apache.org/
Have a look there its easy to use and very powerfull.

To the original question, the implementation only consumes a thread per I/O operation in one case, AsynchronousFileChannel on Unix/Linux systems.

Related

how to choose java nio vs io?

As we had known, If we want to use traditional IO to construct server, it must block somewhere, so we had to use loop or one thread one socket mode, So nio seem it is better choice. So I want know if the nio is better choice forever?
IMHO, Blocking IO is generally the simplest to use, and unless you have a specific requirement which demands more from your system, you should stick with simplest option.
The next simplest option is blocking NIO, which I often prefer if I want something more efficiency or control than IO. It is still relatively simple but allows you to use ByteBuffers. e.g. ByteBuffers support little endian.
A common option is to use non-blocking NIO with Selectors. Much of the complexity this introduces can be handled by frameworks such as Netty or Mina. I suggest you use such a library if you need non-blocking IO e.g. because you have thousands of concurrent connections per server. IMHO You have thousands of connections, you should consider having more servers unless what each connection does is pretty trivial. AFAIK google go for more servers rather thousands of users per server.
The more extreme option is to use NIO2. This is even more complex and lengthy it write than non-blocking NIO. I don't know of any frameworks which support this well. i.e. it is actually faster when you do. AFAIK It appears this is worth using if you have Infiniband (which is what it was designed to support) but perhaps not worth using if you have Ethernet.
If you want non-blocking IO, NIO is not the better choice—it's the only choice in Java. Keep in mind that people still use the old IO regularly because it is way simpler to code against. NIO API is quite raw and is more of an enabling low-level technology than a client-side API. I suggest using NIO through an API that provides a simpler interface to the problems you want to solve using non-blocking IO.
A little late, but personally, I use NIO even for the regular "everyday" file handling. So, I use things like:
1. if(Files.notExists(path)) { }
2. Files.createDirectory(path);
3. Files.newInputStream(path) targetPath.resolve("somefile.txt");
4. Files.newBufferedWriter(path, charset);
5. DirectoryStream<Path> directoryStream = Files.newDirectoryStream(path);
and it works fine for me. I prefer Path instead of the old File because of methods like relativize or resolveSibling.
Doesn't strike me as more complicated than IO.
You would only use NIO if you can justify the inevitable complexity that it introduces. If you do not have any guidance in terms of the expected load, and also in terms of whether your product / project has the resources to maintain the relevant code, then your should err on the side of caution and use IO.
To give my answer some weight, I have just spent three months maintaining and bug fixing an integration layer where raw Java NIO (i.e. no overarching framework was used) was used. The design was based, in essence, on client threads adding messages to a queue and a small number of worker threads performing their NIO magic and then passing replies back to client threads in an event-based manner. In retrospect, I cannot justify the original decision to use NIO, since it became a distraction that ate significant amounts of time that should have been spent on higher level business logic.
You can use any of this, unless you are going to create "super fast" server.
Of course a good approach here is to use nio, since it's new and modern way to write multi-client servers for high throughput tasks.
Some advantages of the NIO.2 API over the legacy java.io.File class for working with files:
Supports file system–dependent attributes.
Allows you to traverse a directory tree directly.
Supports symbolic links.
For specific use cases and more details, you can see this article
Traditional IO is easy and simplified code, NIO is more complicated but more flexible.
In my case i prefer use IO for small streaming and NIO for large streaming but nio is really more complex
with NIO i have to create an entire package to manage it instead io package that i directly use snippet

What is about NIO problems in Scala / Java

While searching the web for concurrency in jvm I found questions about searching Non-blocking IO library for Scala / Java.
What is the problem about? If I want to send something to file / socket I can launch separate thread which make the job.
I know there could be problem using event based threads - because whole system could be blocked. But does it reference to JVM/ Scala?
ADDED:
Please correct me if I'm wrong:
I think that when you need to call some IO function in asynchronous way it need to go into separate process or system (heavy) thread. Am I right?
So - all the questions about solving this kind of thing in common languages goes into creating and managing separate process or threads. So the only facilitate from the language is to create some pool of threads which will be assigned to IO operations in async.
So my hypotheses is.
Sentence: Language X is better then Y because calling async IO operation dosen't block the virtual machine is false because in every language that support system threads there is possibility to manage NIO, the only difference is that language X has better support for this through builtin libraries / language features.
Is this hypothese Truth?
Can some language achieve NIO without os system support? (through processes / threads)
Scala has a bunch of tools for concurrency, and NIO has a few tools for non-blocking IO. So, it should come as no surprise that there are a lot of great libraries that help connect the dots:
Finagle
... a library for building
asynchronous RPC servers and clients
in Java, Scala, or any JVM language.
Built atop Netty, Finagle provides a
rich set of tools that are protocol
independent.
Akka is a pretty nice, featureful actors/concurrency/services package which also uses Netty for their built-in remoting functionality
Naggati2 is another one from Twitter, also built on Netty, not sure if it's being superseded by Finagle though.
Here is an interesting recent blog post that may help you: http://jim-mcbeath.blogspot.com/2011/03/java-nio-and-scala-continuations.html

Manage out-of-JVM agent pool and communication

I need to managed a pool of agents from my application. All are written in Java but the agents need to run in their own JVM. I wrote a proof of concept that starts the subprocesses and uses the stdout/stdin to send commands and keep-alive information. I also open a socket connection for data transfer.
I guess that some connection pooling libraries should be able to help in the management of the agents.
What about the communication between the agents and the main process ? Using TCP with XML messages (JAXB) is not really as reliable or convenient as I would like. Any suggestion for a better library to assist here ?
I could very well write what I need myself but I'm sure other people have done that way better already.
For messaging could try something like ZeroMQ, it's a messaging tool and has local transports for communicationg between processes, then you could just serialised objects between the process.
The alternative is to go back to traditionally rmi, probably the simplest.
You could try Hessian:
http://hessian.caucho.com/
or Preon:
http://preon.sourceforge.net/
I've actually found two ways that would have been of great help when I developed this:
WebSockets. I used simple sockets but then I needed to reinvent signaling to check who's sending and done sending things. I used a line-based approach but it's really ugly. WebSockets offer the message-based communication and that's great.
Hazelcast. This is a "distributed system" and offers great things like distributed executors (I schedule a message to be sent in the app server and let any available out-of-jvm agent handle it, atomically), shared and thread safe hashmaps (to keep track of who is running) etc. Many of the similar tools I had seen were either in native code (like ZeroMQ btw) or with per-CPU licenses and such. Hazelcast is community edition and can be bundled into my apps.
Actually, I had started using vert.x to handle websocket-based communication and realized it was itself using hazelcast.

Are non blocking IOs still an issue with server side Java?

The Java NIO Socket Framework supposedly hides the dirty details of non-blocking IO from developers, allowing them to build highly scalable applications, which can handle over 10000 incoming and outgoing sockets using only one thread.
Are non blocking IOs still a pain with the typical version of Java 2 SE/EE?
Is this framework still necessary and useful?
Thanks for your time.
Well, NIO creates an abstraction over some of the details, certainly. Non-blocking IO is still a pain to get your head around (at least, I find it is) but at least it's feasible. (Personally I prefer the .NET style of asynchronous IO, but that's a different matter.)
I usually use blocking IO: for most tasks, this is all I require and I wouldn't gain significantly by using non-blocking IO. In some cases (such as the one you mentioned) non-blocking IO is really the only way forward if you want to keep your thread down.
I recommend that you learn about it, play with it, and then use judgement to decide when to use it in production code. I wouldn't suggest starting to use it everywhere...
Yes, NIO is very useful. NIO is also a bit hard to work with.
Depending on your needs you could consider using frameworks that wrap NIO, like grizzly or mina. Grizzly is the networking part of glassfish appserver from sun Oracle.
Mina is a network application framework from Apache.org.
Personally I prefer grizzly but that's just me.

java.nio.channels.*

What is up with nio channels ? There were some nice talks when it was added to java but I still don't see people using it in their applications.
Is there something wrong with it, or am I just not encountering people who use it?
Any nice examples as to why I should bother using it at all ?
Thanks
You're asking about channels, but channels only make sense within the general framework of using the (relatively) new nio capabilities as a whole.
My guess is that of the many, many Java applications out in the world, not many need the capabilities of nio. The usual "business" process read streams and/or files... nothing special.
That said, the Apache folks have recently rewritten their core Java libraries ( http://hc.apache.org/ ) to use nio, and claim some impressive performance benefits in some cases.
nio also lets you do stuff like memory-mapping files, and this can allow an application to do very fast random access to the file. Again, only some special applications need this, and that's probably why you don't see a lot of it used.
Apache Mina is a great networking library and uses NIO.
Apache MINA is a network application framework which helps users develop high performance and high scalability network applications easily. It provides an abstract · event-driven · asynchronous API over various transports such as TCP/IP and UDP/IP via Java NIO.
Net4J, a signaling platform/framework, makes heavy use of NIO channels. (One part of Net4J basically provides a convenience API to NIO channels.)

Categories

Resources