首页 > Web开发 > 详细

使用netty自带连接池,异步发送消息,同步获取返回结果

时间:2020-04-26 20:56:43      阅读:1039      评论:0      收藏:0      [点我收藏+]

Netty获取异步执行结果

	// 定义个存储响应消息的变量responseMsgCache
    private static Cache<String, BlockingQueue<String>> responseMsgCache = CacheBuilder.newBuilder()
            .maximumSize(50000)
            .expireAfterWrite(1000, TimeUnit.SECONDS)
            .build();

步骤:(具体代码参考下面demo)

  1. 给消息设置个唯一key

  2. 初始化响应消息队列:responseMsgCache.put(key,new LinkedBlockingQueue(1)); 见NettyTools.initReceiveMsg(key);

  3. 执行异步IO:future.addListener 见NettyClientService.sendMsg

  4. 阻塞等待:responseMsgCache.get(key).poll()见NettyTools.waitReceiveMsg(key);

  5. 处理事件:如果监听到事件,responseMsgCache.get(key).add(Response)见NettyTools.setReceiveMsg(key, msgStr);

  6. 等待结束,返回response。

demo git地址:https://gitee.com/xjmroot/netty-pool
demo演示结果
技术分享图片

参考:http://thoreauz.com/2019/01/19/rpc2-netty-handler/

使用netty自带连接池,异步发送消息,同步获取返回结果

原文:https://www.cnblogs.com/JaminXie/p/12781748.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!