简体   繁体   中英

Asynchronous Application Design With Netty

I have a server Foo, that I connect to via a simple socket. I write text into the socket to send it messages and it replies with a response to my query once it's processed it, however it makes no guarantee of the order of responses. I can link up requests and responses with IDs.

Ex :

Send "1:request" to Foo
Foo Returns "1:response"

Send "1:request" & "2:request" to Foo
Foo Returns "2:response"
10 seconds later
Foo Returns "1:response"

Question:

What's the best way to handle this situation with Netty, how can I provide an interface that allows me to submit a message to the Foo service and give me a Promise or a Future that allows me to return without blocking waiting for the response to my message (but knowing i have an object that I can block on later)

One way to do that is by sharing a data-structure between your response handler (ie the last handler in the pipeline of your client) and the client that shoots the requests. For example, it can be a map with your ID and a blocking queue where the response will be put by the handler.

The send message in your client would be something like:

public Future send(String id, String msg) {
  BlockingQueue queue = new ArrayBlockingQueue(1);
  handler.responses().put(id, queue);
  channel.writeAndFlush(id + ":" + msg);
  return new MyFuture(queue);
}

Then, the thread that called send() can wait for the response with myFuture.get() , which will have an implementation that blocks on the queue you passed in the constructor, eg:

@Override
public String get() throws InterruptedException, ExecutionException {
  return queue.take();
}

Checkout the HTTP/2 example for another approach (still quite similar): https://github.com/netty/netty/blob/4.1/example/src/main/java/io/netty/example/http2/client/HttpResponseHandler.java

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