Thrift RPC实战(五) thrift连接池

非 Y 不嫁゛ 提交于 2019-12-17 20:42:32

【推荐】2019 Java 开发者跳槽指南.pdf(吐血整理) >>>

Thrift本身没有提供连接池,我们可以用Apache Commons Pool2来实现一个

一、定义对象工厂

BasePooledObjectFactory<T> extends BaseObject implements PooledObjectFactory<T>
public class TProtocolFactory extends BasePooledObjectFactory<TProtocol> {

    private String host;
    private  int port;
    private  boolean keepAlive =true;

    public TProtocolFactory(String host, int port, boolean keepAlive) {
        this.host = host;
        this.port = port;
        this.keepAlive = keepAlive;
    }

    /**
     * 创建对象
     * @return
     * @throws Exception
     */
    @Override
    public TProtocol create() throws Exception {
        TSocket socket = new TSocket(host,port);
        //使用 TFramedTransport传输
        TTransport tTransport = new TFramedTransport(socket);
        tTransport.open();
        return new TCompactProtocol(tTransport);
    }

    @Override
    public PooledObject<TProtocol> wrap(TProtocol protocol) {
        return new DefaultPooledObject<TProtocol>(protocol);
    }

    /**
     *  对象钝化(returnObject时触发)
     * @param pooledObject
     * @throws Exception
     */
    @Override
    public void passivateObject(PooledObject<TProtocol> pooledObject) throws TTransportException {
        if (keepAlive){
            pooledObject.getObject().getTransport().flush();
            pooledObject.getObject().getTransport().close();
        }
    }

    /**
     * 对象激活(borrowObject时触发)
     * @param pooledObject
     * @throws TTransportException
     */
    @Override
    public void activateObject(PooledObject<TProtocol> pooledObject) throws TTransportException {
        if (!pooledObject.getObject().getTransport().isOpen()){
            pooledObject.getObject().getTransport().open();
        }
    }

    /**
     * 对象销毁(clear时会触发)
     * @param pooledObject
     * @throws TTransportException
     */
    @Override
    public void destroyObject(PooledObject<TProtocol> pooledObject) throws TTransportException{
        passivateObject(pooledObject);
        pooledObject.markAbandoned();
    }

    /**
     * 验证对象有效性
     * @param pooledObject
     * @return
     */
    @Override
    public boolean validateObject(PooledObject<TProtocol> pooledObject){
        if (pooledObject.getObject() != null){
            if (pooledObject.getObject().getTransport().isOpen()){
                return true;
            }
            try {
                pooledObject.getObject().getTransport().open();
                return  true;
            } catch (TTransportException e) {
                e.printStackTrace();
            }
        }
        return false;
    }
}

需要重写:activateObject(对象激活) 及 passivateObject(对象钝化)

二、定义对象池

使用GenericObjectPool的默认实现

public class AutoClearGenericObjectPool<T> extends GenericObjectPool<T>{

    public AutoClearGenericObjectPool(PooledObjectFactory<T> factory) {
        super(factory);
    }

    public AutoClearGenericObjectPool(PooledObjectFactory<T> factory, GenericObjectPoolConfig config) {
        super(factory, config);
    }

    @Override
    public void returnObject(T obj) {
        super.returnObject(obj);
        //空闲数>=激活数时,清理掉空闲连接
        if (getNumIdle() >= getNumActive()) {
            clear();
        }
    }
}

common-pools提供了对象池的默认实现:GenericObjectPool 但是该对象池中,对于处于空闲的对象,需要手动调用clear来释放空闲对象,如果希望改变这一行为,可以自己派生自己的子类,重写returnObject方法,上面的代码中,每次归还对象时,如果空闲的对象比激活的对象还要多(即:一半以上的对象都在打酱油),则调用clear方法。

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!