views:

544

answers:

7

Hi folks!

I'm currently searching for a Java networking library. What I want to do is sending XML, JSON or other serialized messages from a client to another client and/or client to server.

My first attempt was to create an POJO for each message, plus a MessageWriter for sending and a MessageReader for receiving it. Plus socket and error handling. Which is quite a lot of error prone work.

What I'm looking for is a a higher level library which abstracts from sockets. Furthermore it should supports something like code generation for the messages.

Google's Protocol Buffers (http://code.google.com/apis/protocolbuffers/) looks promising. But are there alternatives? The emphasis is not on speed or security (at the moment), it is just supposed to work reliable and with a low amount of implementation time.

+2  A: 

Try HttpClient from apache.

dacracot
+1  A: 

If you are not already using Spring, this might be way more than you are looking for. But the HttpInvoker is a pretty high level abstraction for sending messagess. All of the remoting information is available online, and the HTTP Invoker section is located at 17.4.

Beau Simensen
+7  A: 

You have several options depending on how abstracted from raw sockets you want to get. Once you depart from socket level programming, you're pretty much into remoting territory,

  • Standard Remoting Options for Java: RMI or JMS
  • Implement JMX Mbeans in each client and the servers and use JMX remoting to invoke message passing operations.
  • If you think you might want to use multicast, I would definitely check JGroups.
  • If you're looking to create your own protocol but want to use some existing building blocks, check out Jakarta Commons Net. The HttpClient referenced in Answer #1 has been incorporated into this package.
  • There are also some interesting proprietary messaging systems that have the added virtue of supporting multiple platforms/languages such as Spread and DBus.
  • Can't enumerate remoting options without mentioning WebServices.... but.... blech!

I am not completely sure what you mean by code generation for the messages. Can you elaborate ?

Nicholas
I need an object to represent the actual message. (class Message { getId(); getSender(); ... }) But you need to have a reader to feed your data into this object from a stream and to a stream. I'd like to simplify this process. Protobuf can generate a reader/writer from an message description file.
Stefan Teitge
+2  A: 

Ah... gotcha. But rather than using code gen. to marshall and unmarshall, if you have Java on both ends, could you use simple object serialization ? If performance and/or message size is a concern, you could make your Message class externalizable.

I had not looked at Protobuf before. Looks pretty decent. Using that, you would then just need a transmission method.

Nicholas
JGroups looks quite promising for transferring the messages.
Stefan Teitge
A: 

(Can't seem to post comments replying to your comments....) If you like the capabilities, you may also want to look at JBossCache. It is implemented using JGroups and while your requirement is not for a caching solution, it does abstract some of the salient functions of JGroups quite well by basically implementing a distributed hash map with event notifications etc.

Nicholas
I just played around a little with JGroups and Java serialization. It seems like this is the thing I was looking for. Thanks.
Stefan Teitge
A: 

IDL (variants of interface description languages) based language neutral (sort of) network library/framework:

  • Thrift - Apache license.
  • Ice - GPL and commercial licenses.
  • CORBA - If you want more choices.
obecalp
A: 

I suggest you look at ActiveMQ for passing messages. It is simple, flexible and fast. It supports Serializable objects, but also Map messages and other JMS message types so you shouldn't need your own Serialization format unless efficiency is important. If performance is important I would suggest Hessian, it is very fast without requiring code generation.

Peter Lawrey