views:

970

answers:

6

At what point is it better to switch from java.net to java.nio? .net (not the Microsoft entity) is easier to understand and more familiar, while nio is scalable, and comes with some extra nifty features.

Specifically, I need to make a choice for this situation: We have one control center managing hardware at several remote sites (each site with one computer managing multiple hardware units (a transceiver, TNC, and rotator)). My idea was to have write a sever app on each machine that acts as a gateway from the control center to the radio hardware, with one socket for each unit. From my understanding, NIO is meant for one server, many clients, but what I'm thinking of is one client, many servers.

I suppose a third option is to use MINA, but I'm not sure if that's throwing too much at a simple problem.

+3  A: 

Scalability will probably drive your choice of package. java.net will require one thread per socket. Coding it will be significantly easier. java.nio is much more efficient, but can be hairy to code around.

I would ask yourself how many connections you expect to be handling. If it's relatively few (say, < 100), I'd go with java.net.

Jim Nelson
With a modern 64-bit OS you can have hundreds of thousands of threads.
Tom Hawtin - tackline
Sure, but that's not necessarily the most scalable way to architect an application.
Jim Nelson
But a socket is much more resource intensive than a thread...
Zombies
The question is not one of more sockets versus more threads. It's how many threads per socket.
Jim Nelson
A: 

Each remote server will have up to 8 connections, all from the same client (to control all the hardware, and separate TX/RX sockets). The single client will want to connect to several servers at once, though. Instead of putting each server on different ports, is it possible to use channel selectors on the client side, or is it better to go multi-threaded io on the client side of things and configure the servers differently?

drhorrible
A: 

Actually, since the remote machines serve only to interact with other hardware, would RMI or IDL/CORBA be a better solution? Really, I just want to be able to send commands and receive telemetry from the hardware, and not have to make up some application layer protocol to do it.

drhorrible
A: 

The number of connections you're talking about tells me you should use java.net. Really, there's no reason to complexify your task with non-blocking I/O. (Unless your remote systems are underpowered, but then why are you using Java on them?)

Take a look at Apache's XML-RPC package. It's easy to use, completely hides the network stuff from you, and works over good ol' HTTP. No need to worry about protocol issues ... it'll all look like method calls to you, on both ends.

Jim Nelson
XML-RPC looks interesting, but I'm using SE, and I think adding/switching to EE is much more complicated than even NIO, but correct me if I'm wrong (do they run on the same JVM?).
drhorrible
It's a plain library and should work in SE and EE. Like another commentator mentioned, it does have its scalability issues. but for the 98% case, I think it's pretty solid.
Jim Nelson
+1  A: 

Given the small number of connections involved, java.net sounds like the right solution.

Other posters talked about using XML-RPC. This is a good choice if the volumes of data being shipped are small, however I have had bad experiences with XML-based protocols when writing inter-process communications that ship large amounts of data (e.g. large request/responses, or frequent small amounts of data). The cost of XML parsing is typically orders of magnitude higher than more optimised wire formats (e.g. ASN.1).

For low volume control applications the simplicity of XML-RPC should outweigh the performance costs. For high volume data communications it may be better to use a more efficient wire protocol.

Frank Taylor
+1  A: 

Avoid NIO unless you have a good reason to use it. It's not much fun and may not be as beneficial as you would think. You may get better scalability once you are dealing with tens of thousands of connections, but at lower numbers you'll probably get better throughput with blocking IO. As always though, make your own measurements before committing to something you might regret.

Something else to consider is that if you want to use SSL, NIO makes it extremely painful.

Dan Dyer