views:

248

answers:

5

I have a situation where I want a Java client to have a two-way data channel with a servlet (I have control over both), so that either can begin data transferring without having to wait for the other to do something first, but to get through the firewalls this needs to be tunnelled in http or https.

I have looked around, but I do not believe I know the right terms for asking Google.

I was originally looking at http-tunneling modules, but realizing that I have a web container in the other end, I believe that the appropriate way is to think of a fat client needing to communicate home. I was thinking that the persistant connection in http 1.1 might be very useful here. I can easily do heartbeat transfers to keep the connection from ideling.

At this point in time I just need to do a proof of concept so I primarily need something that works now, which can then be optimized or even replaced later.

So, I'd appreciate pointers to projects that allow me to have a connection where either side can at will push information (like a serialized object or a descriptive stream of bytes) to the other side. I'd prefer pure Java, if at all possible.


EDIT: Thanks for the pointers. It appears that what I need, will be available in the servlet 3.0 specification, which I might end up using in the long term depending on when it will be supported in the various web containers.

For now I am investigating the Cometd package, which appears to be able to do exactly what I need for my prototype.

A: 

How fast does it need to be? You could always just do polling on the client. Just check for new messages every so often.

Chad Okere
A: 

You can use the Hessian protocol over HTTP. It's a fast binary protocol for serializing data. Typically used for a web-services style RPC communication, but there's no reason it couldn't be 2-way - see Hessian mux. It's pure Java, too :-)

Vinay Sajip
+1  A: 

Search terms: comet, long-polling

These are mostly used in an AJAX context, but I see no reason why you could not use them in a Java project.

Simon Groenewolt
A: 

Generally this is done by having the server not respond to an http request immediately. It waits around for some update (or a timeout) before sending a response. Obviously some care needs to be made ensuring that the server will handle this under load.

See, for instance, Comet.

Tom Hawtin - tackline
+1  A: 

Please take a look at Eclipse Net4J,

http://wiki.eclipse.org/Net4j

It supports all the features you mentioned. A special nice feature is that it supports HTTP connection pooling so you can have lots of channels between client and server but use only a few HTTP connections.

The only problem is that it doesn't have documentation at all. You just have to read the source code. Once you figure it out, it's very easy to use.

There are a few more diagrams on old Net4J site,

http://net4j.berlios.de/

ZZ Coder