Netty客户:在同一将来的请求-响应

时间:2019-10-02 14:01:27

标签: sockets netty

我正在尝试实现一个Netty客户端,该客户端将在同一Future中发送TCP请求并接收响应。代码在下面

public void sendMessage(String msg) {
  Future<Channel> future = simpleChannelPool.acquire();

  future.addListener((FutureListener<Channel>) f -> {
    if (f.isSuccess()) {
      Channel ch = f.getNow();

      ChannelFuture channelFuture = ch.writeAndFlush((msg + " " + now() + lineSeparator()));

      channelFuture.addListener(writeFuture -> {
        if (writeFuture.isSuccess()) {
          System.out.println("Channel write successful");
          // how to READ data here?
        }
      });
      // Release back to pool
      simpleChannelPool.release(ch);
    } else {
      System.out.println("Failed to acquire a channel");
    }
  });
}

我缺少的部分是如何成功写入第if (writeFuture.isSuccess()) {...行后读取数据。基本上,我需要等待下一次读取完成。

考虑到Netty的异步特性,它是否是用于请求/响应协议的正确工具?

使用的网络版本: 4.1.39。最终

完整的代码可用here

0 个答案:

没有答案