简体   繁体   中英

Is there a request–response networking API for Java?

I'm looking for a simple java library which would enable to write code in the following fashion:

Remote remote = Remote.connect("some_host:1234"); 
Future<String> response = remote.request("hello");
// do something else
String reply = response.get();

It should be based on tcp/ip and use plain text messages across the network to be language agnostic, so that non-Java servers are also able to send/receive requests/responses.

(Before telling me to use plain sockets, keep in mind that in that case you need to implement wrappers to delimit payload, care about received messages reordering, thread handling... the example is simple, but it is not that trivial to implement well .)

Is there any existing API out there like this?

PS: ...the simpler, the better!

Take a look at JMS .

Use plain sockets:-)

Sockets use TCP, and TCP takes care of payloads, multiple packets and ordering. You'll still need to handle the threading, but java.util.concurrent has all you need for this. Don't forget to decide of a character encoding for your strings.

TCP implementations usually give to the applications only a stream-like interface, with no access to the individual packets. (Also, some routers/firewalls might want to repack the data in the TCP streams, which means that they do not necessarily arrive in the same blocks as sent.) Thus we really need to use some packaging protocol on top of TCP (or really on top of any stream-pair ).

A really simple protocol would be one line for each request/response , but this will work only with small data sizes (or you need to somehow escape embedded newlines).

If you want it more structured, you could use something XML-based (like XMPP): each request/response would be a complete XML element (including subelements, if necessary).

Also, if you want to use the request–response scheme, you will need to either say responses have to come in the same order as the requests were ordered (which disallows or at least complicates parallel processing on the server side for multiple requests on the same connection), or you will have to define request numbers, and the responses then will somehow include the request number they relate to.

As examples, HTTP uses the first approach (from 1.1 - before there was only one request/response pair for each connection), while the X protocol uses the second one.

For HTTP, there are already implementations around (both on client and on server side), and it can be made totally plain text (depending on the data you are sending).

Alternatively, we could build our protocol directly on a packet-based protocol like UDP. But this has the usual reliability problems of UDP, and we also need to use message-numbers (to relate responses to requests) or such - which could mean that we have to re-implement half of TCP again.

So, sorry, no real answer other than use HTTP .

With Apache Mina you're able to develop your own protocol, but it might be overkill.

I am not aware of any library which gives you a layer quite like this over sockets. The IMAP protocol has a framing layer which is quite like this, but i don't know of any way to use it independently of the rest of IMAP. Such a library would be simple enough to write, and potentially quite useful, so if anyone fancies trying it, i encourage them to do so!

The closest thing i can think of to what you want is ZeroMQ in request-reply mode . ZeroMQ is written in C, but there is a Java binding . It's a pretty good library - there are bindings for many languages, so it's practically language agnostic, and it does indeed take care of delimiting payload, caring about received messages reordering, and thread handling. I don't think it's plain-text, though.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM