Download fulltext - DiVA Portal

Transcript
2.5.1
Java
The Java runtime comes bundled with a huge number of built-in libraries,
making complex tasks such as networking a lot easier. TCP networking can
be realized through the java .net.Socket and java .net.ServerSocket classes,
along with different reader- and writer-classes for reading and writing information to and from the sockets. Java takes care of all of the low-level socket
handling for us, all we need to do is connect, and we can send and receive
data.
However, these built-in libraries use what we call synchronous I/O, and will
only work for one concurrent user unless we implement threading. Threading
gives us the ability to have several concurrent users, but also requires us to
have two threads per user and one thread for the accepting socket. This can
potentially spawn a great number of threads and therefore be a stress-factor
for the server. As an opposing technology to synchronous I/O, we have
asynchronous I/O. Proper, asynchronous I/O, does not rely on threading
sockets, and will therefore be easier on the server and probably easier to
implement. Java offers asynchronous I/O through the java.NIO package, but
setting this up in a application is far from trivial. Luckily, there exists a few
libraries which eases the work quite a bit:
• Grizzly is a large-scale wrapper around the java.NIO libraries to make
it easier to get up and running with asynchronous calls. It is used in
Sun’s Glassfish server, which means it should be a quite stable framework.
http://grizzly.java.net/
• Naga is a very small library providing asynchronous I/O. It is intended
for smaller projects, and comes self contained in a jar-file without any
external dependencies.
http://code.google.com/p/naga/
Given that Grizzly is a much larger product than Naga, it also requires more
work to set up. The overhead for smaller project will therefore be larger
than when using smaller libraries like Naga. Nevertheless, care should be
taken when integrating any of these in our project. By choosing a strict
architecture for the server, we can easily exchange the networking library
later on with a new one without affecting the rest of our program. This will
36