Is there a request-response API for Java?

I am looking for a simple java library that will allow you to write code as follows:

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 simple text messages throughout the network to agnostic the language, so non-Java servers can also send / receive requests / responses.

(Before telling me to use simple sockets, keep in mind that in this case you need to implement shells to differentiate the payload, taking care of reordering the received messages, processing the streams ... the example is simple, but it’s not so trivial to implement well .)

Is there any existing API there?

PS: ... the simpler the better!

+4
source share
5 answers

Take a look at JMS .

+1
source

Use simple sockets :-)

Sockets use TCP, and TCP takes care of the payloads, multiple packets, and order. You still have to handle the threads, but java.util.concurrent has everything you need for this. Remember to select the character encoding for your strings.

+1
source

TCP implementations typically provide applications with only a streaming interface without access to individual packages. (In addition, some routers / firewalls may want to repackage the data in TCP streams, which means that they do not necessarily arrive in the same blocks as the sent ones.) Thus, we really need to use the packing protocol over TCP (or really over any pair of threads).

A simple protocol would be one line for each request / response, but this will only work with small data sizes (or you need to somehow avoid the inline new lines).

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

In addition, if you want to use a request-response scheme, you need to either say that the answers must come in the same order as requests for requests (which prohibits or at least complicates the parallel processing on the server side for several requests for same connection), or you have to determine the request numbers, and the answers then somehow include the number of the request to which they relate.

As an example, 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.

For HTTP, implementations already exist (both on the client side and on the server side), and it can be made completely open (depending on the data being sent).

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

So sorry, no real answer other than using HTTP.

+1
source

With Apache Mina, you can develop your own protocol, but this can be redundant.

0
source

I do not know any library that gives you a layer similar to this socket. IMAP has a frame layer that is very similar to this, but I don’t know how 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 tried it, I encourage them to do it!

The closest thing I can think of is ZeroMQ in request-response mode . ZeroMQ is written in C, but there is a Java binding . This is a pretty good library - there are bindings for many languages, so it is practically not agnostic, and it really cares about the differentiation of the payload, the care of reordering the received messages and processing the streams. I do not think this is plain text.

0
source

All Articles