Note that there are some explanatory texts on larger screens.

plurals
  1. POHow can I implement a request-response protocol without blocking while awaiting response?
    primarykey
    data
    text
    <p>I need to implement an application that is simultaneously communicating with multiple clients using a (bidirectional) request-response protocol. Previously I have implemented this using two dedicated threads for each client (one reader/reactor and one writer/initiator). The problem with this is that the thread management became quite complex and ugly. Is there any standard way of handling this, possibly even with just one thread, or at least a constant amount of threads for handling all of the clients? </p> <p>This is sort of how some communication would look in a thread with the blocking implementation:</p> <pre><code>Command response = request("cmd1", "a", "b"); if(!response.is("OK")) { return; } response = request("cmd2", "c"); if(!response.is("OK")) { return; } response = request("cmd3"); </code></pre> <p>Between sending the request and awaiting the corresponding response I would like for the current thread to be able to do other work, but then continue execution once the response actually arrives.</p> <p>I know that it could be possible to use asynchronious IO and register Java Future/Runnable instances to run once the response for the request is received, but this easily turns into a multi-level nesting of anonymous Runnable subclasses and I suspect that it will be more pain then it is worth. This would probably result in something like the sample below, which quickly becomes highly nested and unreadable. Surely there must be an easier way?</p> <pre><code>request("cmd1", "a", "b", new ResponseHandler() { public void response(Command response) { if(response.is("OK")) { request("cmd2", "c", new ResponseHandler() { public void response(Command response) { if(response.is("OK")) { request("cmd3", new NullResponseHandler()); } }}); } }}); </code></pre> <p>I have also considered the possibility of using a dedicated <a href="http://sujitpal.blogspot.com/2009/01/more-java-actor-frameworks-compared.html" rel="nofollow">Actor framework</a> to handle the request-response logic. While they look like they could help in this situation I have never worked with such frameworks before so I do not know if they are appropriate for this situation. </p> <p>In short, my question is: How do I handle an arbitrary amount of request-response connections in a non-blocking way so that a constant amount of threads is sufficient? Are Actor frameworks a valid approach? </p> <p>PS. I am using Java for this project. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload