how to choose java nio vs io? - java

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

Related

Why can't Java IO implement async reading?

In Java IO, we use Stream and Reader while in NIO we use Channel, Selector.
They both do the same thing, but the structure is totally different.
So why they don't write a new Stream like "AsyncStream" or a Reader like "AsyncReader" to implement what NIO have implemented. If so, we only have one structure and it's beautiful.
So why Java IO cannot implement async reading?
What are the difficulties to implement async reading using Java IO ?
Or what is the advantage of writing a new framework instead of using the existing one?
I tried to understand your idea in the comment above, but I'm afraid that there is insufficient detail to understand it as a proposal. Without that, it is impossible to make a judgment on whether it would work or not.
However, there are a couple of points that can be made in response:
IF Java was a shiny new (and unreleased) language, THEN they could improve on the I/O API design in a few areas.
In addition, IF someone came up with a well-considered, fleshed out API design that combined sync and async capabilities, THEN that could be considered.
But Java is NOT a shiny new language. It is an old programming language, with billions of lines of source code written in it. The kind of change that you are contemplating on a central API would either create huge binary compatibility problems for Oracle's paying customers, or it would lead to a huge legacy code problem. It simply would not happen.
Setting that aside, if you attempted to merge sync and async capabilities into a single API, you risk creating a situation where:
custom stream types need to implement a lot of extra functionality, and / or
the merger leads to unanticipated performance issues.
Now these concerns may not be warranted.
However, we cannot tell without seeing a concrete API design proposal and an implementation to try out for usability and performance. Bear in mind that the original elegant API design for I/O streams and (then) readers / writers actually turned out to have a variety of problems. These did not become apparent until people used the APIs in production code. For example:
character encoding concerns lead to the introduction of Reader / Writer in Java 1.1.
performance analyse identified that memory memory-to-memory copying was a problem, leading to the introduction of Buffer and so on.
In summary, it is really hard to design a good I/O API ... and Java is unlikely to change anyway.

Are ReadableByteChannel and WritableByteChannel a replacement of InputStream and OutputStream?

As far as I know, ReadableByteChannel and WritableByteChannel from the nio package can be considered a replacement of InputStream and OutputStream from the legacy io package. If fact, they can be used to do the same I/O operations and more.
Nevertheless, they still seem not much used. Also, their support in popular libraries is quite poor. For example, the Guava team is even thinking about dropping it.
Is there any reason for new code dealing with I/O to use Streams with respect to Channels?
There is a historical context that should not be ignored. When NIO was introduced (back in Java 1.4), a lot of features were missing and the promise to hand them in later was not hold for a long time. Recall that in Java 1.4 the way to get a FileChannel was to first create a FileInputStream, FileOutputStream, or RandomAccessFile and invoke getChannel on it. Thus, there was no way to write code using NIO/channels without using the older IO/stream API.
The first Java version providing a way to create a FileChannel without using the old API is Java 7. This is also the first version providing an NIO API for directory scan, change notifications and advanced file attributes.
So the NIO API can be considered quite new regarding these features and it’s not that surprising that it takes some time for developers adopting it. By the way, this kind of adoption might include removing utility methods which became unnecessary with the new API like the proposal you have linked. As far as I can see it really means removing just four methods whose functionality is trivial when using the most recent API.
Obviously, when you want to use one of the newer features like memory mapping or the Java 7 features named above you have to use the NIO API. On the other hand, when you want to use Serialization or Zip/GZip (de)compression the only channel support, Java offers, is to wrap your channel into a stream…
It really depends on what you are doing. If you are dealing directly with files and/or sockets, there are many advantages to using nio. In some cases (especially around sockets) new functionality is /only/ being exposed through nio. Some of those advantages are muted if you also compress or encrypt data, where most apis only support streams. Historically the servlet-api spec was all based on streams. However, there is growing support for nio in that space as well.
Immediate performance benefits are gained by using NIO Channels with ByteBuffers. Also if you need to block on multiple things Selectors can be handy.

HTTP Client with NIO2

Have someone familiar with HTTP Client that based on JDK7 and NIO2,
Implementation that use: AsynchronousSocketChannel
I am looking for implementation that based on NIO2 and can scale out unlimited, thousands of HTTP requests concurrently.
NOTES:
please do not suggest me an implementation that based on NIO1 like Apache::AsyncHTTPClient and JBOSS::Netty framework
Please do not suggest me a solution that based on distribute solution, I am looking for one client that can handle all the HTTP requests efficiently.
THANKS!
I would recommend checking out AsyncHttpClient. It was written by the guy that wrote Grizzy for Sun and now works for Sonatype.
I believe you are mistaken in your assumption that you must use NIO2 to scale up. Please listen!
NIO2 is the non-blocking version of Java's IO. This means it's easier to write high performance io in NIO2 because you need not start and manage threads, because most of your threads are just going to be waiting on IO. Multi-threaded programming is tricky, so anything to make it easier is a feature, hence NIO2.
However, as a library user how hard the library was to write isn't your concern. Modern JVMs can handle a lot of threads and the old io API should be powerful when used by skilled programmers. Since you are a user of the library, I suggest that you just find the fastest library.
HTTPClient from Apache is a very popular library. I suggest you get in contact with the experts on that and ask them your performance questions. I'm not an expert in this area, so I'm not telling you HTTPClient is the best, I'm just saying not to count it out yet and that talking to the HTTPClient people would be a good place to start.
Good Luck

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