DefaultEvictionPolicy类是EvictionPolicy接口的实现主要描述,pool中那些idel对象会被Evict,回收。
关键代码如下:
public boolean evict(EvictionConfig config, PooledObject<T> underTest,
int idleCount) { if ((config.getIdleSoftEvictTime() < underTest.getIdleTimeMillis() &&
config.getMinIdle() < idleCount) ||
config.getIdleEvictTime() < underTest.getIdleTimeMillis()) {
return true;
}
return false;
}

ObjectPool 接口:

Object obj = null
try{
  obj = pool.borrowObject();
  try{
    //...use the object...
    }catch(Exception e) {
      pool.invalidateObject(obj);// invalidate the object
      // do not return the object to the pool twice
      obj = null
    }finally{
      // make sure the object is returned to the pool
      if(null!= obj) pool.returnObject(obj);
    } 
}catch(Exception e) { // failed to borrow an object }
PooledObjectState 定义池对象的所有可能状态:
public enum PooledObjectState {
/**
* In the queue, not in use.
*/
IDLE, /**
* In use.
*/
ALLOCATED, /**
* In the queue, currently being tested for possible eviction.
*/
EVICTION, /**
* Not in the queue, currently being tested for possible eviction. An
* attempt to borrow the object was made while being tested which removed it
* from the queue. It should be returned to the head of the queue once
* eviction testing completes.
* TODO: Consider allocating object and ignoring the result of the eviction
* test.
*/
EVICTION_RETURN_TO_HEAD, /**
* In the queue, currently being validated.
*/
VALIDATION, /**
* Not in queue, currently being validated. The object was borrowed while
* being validated and since testOnBorrow was configured, it was removed
* from the queue and pre-allocated. It should be allocated once validation
* completes.
*/
VALIDATION_PREALLOCATED, /**
* Not in queue, currently being validated. An attempt to borrow the object
* was made while previously being tested for eviction which removed it from
* the queue. It should be returned to the head of the queue once validation
* completes.
*/
VALIDATION_RETURN_TO_HEAD, /**
* Failed maintenance (e.g. eviction test or validation) and will be / has
* been destroyed
*/
INVALID, /**
* Deemed abandoned, to be invalidated.
*/
ABANDONED, /**
* Returning to the pool.
*/
RETURNING
}

ThriftClientPool 类剖析:

  • 设置配置

    • TestOnReturn= true
    • TestOnBorrow=true
  • GenericObjectPool对象池构建:关键代码如下:
  • new GenericObjectPool<>(new BasePooledObjectFactory<ThriftClient<T>>() {
    
                @Override
    public ThriftClient<T> create() throws Exception { // get from global list first
    List<ServiceInfo> serviceList = ThriftClientPool.this.services;
    ServiceInfo serviceInfo = getRandomService(serviceList);
    TTransport transport = getTransport(serviceInfo); try {
    transport.open();
    } catch (TTransportException e) {
    logger.info("transport open fail service: host={}, port={}",
    serviceInfo.getHost(), serviceInfo.getPort());
    if (poolConfig.isFailover()) {
    while (true) {
    try {
    // mark current fail and try next, until none service available
    serviceList = removeFailService(serviceList, serviceInfo);
    serviceInfo = getRandomService(serviceList);
    transport = getTransport(serviceInfo); // while break here
    logger.info("failover to next service host={}, port={}",
    serviceInfo.getHost(), serviceInfo.getPort());
    transport.open();
    break;
    } catch (TTransportException e2) {
    logger.warn("failover fail, services left: {}", serviceList.size());
    }
    }
    } else {
    throw new ConnectionFailException("host=" + serviceInfo.getHost() + ", ip="
    + serviceInfo.getPort(), e);
    }
    } ThriftClient<T> client = new ThriftClient<>(clientFactory.createClient(transport),
    pool, serviceInfo); logger.debug("create new object for pool {}", client);
    return client;
    } @Override
    public PooledObject<ThriftClient<T>> wrap(ThriftClient<T> obj) {
    return new DefaultPooledObject<>(obj);
    } @Override
    public boolean validateObject(PooledObject<ThriftClient<T>> p) {
    ThriftClient<T> client = p.getObject(); // check if return client in current service list if
    if (serviceReset) {
    if (!ThriftClientPool.this.services.contains(client.getServiceInfo())) {
    logger.warn("not return object cuase it's from previous config {}", client);
    client.closeClient();
    return false;
    }
    } return super.validateObject(p);
    } @Override
    public void destroyObject(PooledObject<ThriftClient<T>> p) throws Exception {
    p.getObject().closeClient();
    super.destroyObject(p);
    }
    }

     X iface 返回TServiceClient的代理对象关键代码如下:可以看到首先从池中借对象,然后生成TServiceClient的代理对象,代理handler主要实际执行thrift方法,成功时归还到池,失败时需要关闭TServiceClient,并在池中invalidateObject(ThriftClient)

UML类关系图:

wealoha thrift-client-pool 总结的更多相关文章

  1. 高可用的池化 Thrift Client 实现(源码分享)

    本文将分享一个高可用的池化 Thrift Client 及其源码实现,欢迎阅读源码(Github)并使用,同时欢迎提出宝贵的意见和建议,本人将持续完善. 本文的主要目标读者是对 Thrift 有一定了 ...

  2. Thrift笔记(四)--Thrift client源码分析

    thrift文件 namespace java com.gxf.demo namespace py tutorial typedef i32 int // We can use typedef to ...

  3. rpc框架之 thrift连接池实现

    接前一篇rpc框架之HA/负载均衡构架设计 继续,写了一个简单的thrift 连接池: 先做点准备工作: package yjmyzz; public class ServerInfo { publi ...

  4. 由浅入深了解Thrift之客户端连接池化续

    前文<由浅入深了解Thrift之客户端连接池化>中我们已经实现了服务调用端 连接的池化,实现的过于简陋,离实际的项目运用还很遥远.本文将在进一步改造,主要是两方面:1.服务端如何注册多个服 ...

  5. Thrift全面介绍

    官网:http://thrift.apache.org   简介 Thrift是一个软件框架,用来进行可扩展且跨语言的服务的开发.它结合了功能强大的软件堆栈和代码生成引擎,以构建在 C++, Java ...

  6. [转载] 基于zookeeper、连接池、Failover/LoadBalance等改造Thrift 服务化

    转载自http://blog.csdn.net/zhu_tianwei/article/details/44115667 http://blog.csdn.net/column/details/sli ...

  7. Thrift 基于zookeeper改造模式

    对于Thrift服务化的改造,主要是客户端,可以从如下几个方面进行: 1.服务端的服务注册,客户端自动发现,无需手工修改配置,这里我们使用zookeeper,但由于zookeeper本身提供的客户端使 ...

  8. 基于zookeeper、连接池、Failover/LoadBalance等改造Thrift 服务化

    对于Thrift服务化的改造,主要是客户端,可以从如下几个方面进行: 1.服务端的服务注册,客户端自动发现,无需手工修改配置,这里我们使用zookeeper,但由于zookeeper本身提供的客户端使 ...

  9. 用apache commons-pool2建立thrift连接池

    Apache Thrift 是 Facebook 实现的一种高效的.支持多种编程语言的远程服务调用的框架.具体的介绍可以看Apache的官方网站:http://thrift.apache.org/ . ...

  10. thrift:swift项目笔记

    先声明:此swift不是Apple公司的那个swift开发语言,而是facebook的另一个开源项目. facebook的thrift IDL文件,如果默认用thrift -gen java生成jav ...

随机推荐

  1. Java高新技术 Myeclipse 介绍

      Java高新技术   Myeclipse 介绍 知识概述:              (1)Myeclipse开发工具介绍 (2)Myeclipse常用开发步骤详解               ...

  2. WPF 在事件中绑定命令(不可以在模版中绑定命令)

    其实这也不属于MVVMLight系列中的东东了,没兴趣的朋友可以跳过这篇文章,本文主要介绍如何在WPF中实现将命令绑定到事件中. 上一篇中我们介绍了MVVMLight中的命令的用法,那么仅仅知道命令是 ...

  3. C#仪器数据文件解析-Word文件(doc、docx)

    不少仪器数据报告输出为Word格式文件,同Excel文件,Word文件doc和docx的存储格式是不同的,相应的解析Word文件的方式也类似,主要有以下方式: 1.通过MS Word应用程序的DCOM ...

  4. iOS音频播放、录音、视频播放、拍照、视频录制

    随着移动互联网的发展,如今的手机早已不是打电话.发短信那么简单了,播放音乐.视频.录音.拍照等都是很常用的功能.在iOS中对于多媒体的支持是非常强大的,无论是音视频播放.录制,还是对麦克风.摄像头的操 ...

  5. checkValidity-表达验证方法。

    调用该方法,可以显示对表单元素进行有效验证,返回值是boolean. 代码如下: <!DOCTYPE html> <html> <head> <meta ch ...

  6. 【计算机网络基础】数据交换技术和多路复用技术的正(nao)确(can)打开方式

    交换的作用   数据交换是计算机网络中两个终端进行数据传输的方式,它又可以分成两种类型:电路交换和分组交换.很显然,问题的核心在于“交换”,那么我们首先要思考的是:交换的作用是什么?   “交换”的作 ...

  7. Scrapy架构及其组件之间的交互

    最近在学Python,同时也在学如何使用python抓取数据,于是就被我发现了这个非常受欢迎的Python抓取框架Scrapy,下面一起学习下Scrapy的架构,便于更好的使用这个工具. 一.概述 下 ...

  8. IIS6、7添加反向代理的步骤

    1.安装requestRouter_amd64.msi和rewrite_x64_zh-CN.msi. 打包下载地址:http://files.cnblogs.com/files/wangwust/ii ...

  9. iOS 通讯录空格

    iOS 通讯录联系人出现 ASCII 码值为 160 的空格  NOTE:       这里的"空格"是指 在通讯录中取出的联系人中带有特殊空格 带有特殊空格的字符串 " ...

  10. React UI 组件库uiw v1.2.8 发布

    uiw 高品质的UI工具包,基于React 16+的组件库.