netty channelPool连接池怎么使用

发布时间:2021-12-18 17:27:29 作者:iii
来源:亿速云 阅读:562

本篇内容介绍了“netty channelPool连接池怎么使用”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!

场景:当我们作为客户端 向服务端发起连接时,做数据传输时  如果目标服务端只有一个或者可能同时会有多个请求使用channel[多种不同业务数据]。那我们对服务端的channel connect 可以进行池化。方便提高channel 利用率。

如果流数据推送【持续不断的数据 向服务端进行推送 没有必要使用channelPool。也没有必要释放channel】

核心接口:ChannelPool

public ChannelPool newPool(Bootstrap bootstrap) {        return new FixedChannelPool(bootstrap, iridiumClientChannelHandler,            protocolOptions.getMaxConnections());    }}

核心实现类:

public class FixedChannelPool extends SimpleChannelPool

 

一个抽象类

public abstract class AbstractChannelPoolHandler implements ChannelPoolHandler {    @Override    public void channelAcquired(@SuppressWarnings("unused") Channel ch) throws Exception {        // NOOP    }    @Override    public void channelReleased(@SuppressWarnings("unused") Channel ch) throws Exception {        // NOOP    }}

Channel.close() 不是断开连接 是 从eventLoop 移除 注销

关于channelPool 健康检查:仅仅是获取连接前

ChannelHealthChecker 这个类可以判定连接池中的chanel 是否有效

 

public interface ChannelPoolHandler {
   /**
    * Called once a {@link Channel} was released by calling {@link ChannelPool#release(Channel)} or
    * {@link ChannelPool#release(Channel, Promise)}.
    *
    * This method will be called by the {@link EventLoop} of the {@link Channel}.
    */
   void channelReleased(Channel ch) throws Exception;
   /**
    * Called once a {@link Channel} was acquired by calling {@link ChannelPool#acquire()} or
    * {@link ChannelPool#acquire(Promise)}.
    *
    * This method will be called by the {@link EventLoop} of the {@link Channel}.
    */
   void channelAcquired(Channel ch) throws Exception;
   /**
    * Called once a new {@link Channel} is created in the {@link ChannelPool}.
    *
    * This method will be called by the {@link EventLoop} of the {@link Channel}.
    */
   void channelCreated(Channel ch) throws Exception;
}

 /* 当链接创建的时候添加channelhandler,只有当channel不足时会创建,但不会超过限制的最大channel数*/

@Override
   protected void initChannel(Channel ch) throws Exception {
       channelPoolHandler.channelCreated(ch);
   }
}

“netty channelPool连接池怎么使用”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

推荐阅读:
  1. netty使用EmbeddedChannel对channel
  2. netty填坑----论填坑,我是专业的

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

netty

上一篇:怎么深入Java Timer 定时任务调度器实现原理

下一篇:如何进行springboot配置templates直接访问的实现

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》