连接池apache-common-pool2

Posted 分布式服务

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了连接池apache-common-pool2相关的知识,希望对你有一定的参考价值。

Apache commons-pool2类库是对象池技术的一种具体实现,数据库连接池DBCP就是用的它。


仅记录一下如何使用,需要干货可以学习源码,具体的源码解析参见:http://blog.csdn.net/zilong_zilong/article/details/78556281


  • PooledObject:池对象,封装实际对象的。定义了getCreateTime获取PooledObject创建时间,getActiveTimeMillis获取PooledObject处于激活状态的时间,getIdleTimeMillis获取PooledObject空闲时间,getLastBorrowTime获取PooledObject最近借出时间,getLastReturnTime获取PooledObject最近归还时间,getLastUsedTime获取PooledObject最近使用时间。目前Apache commons-pool2提供了2个默认实现DefaultPooledObject和PooledSoftReference,一般使用DefaultPooledObject即可

  •  PooledObjectFactory作用是生产PooledObject,定义了makeObject创建、destroyObject销毁、validateObject校验、activateObject激活PooledObject对象,使用Apache commons-pool2的使用者需要自己实现这个接口

  • ObjectPool:对象池,管理池里面的PooledObject,borrowObject借出PooledObject,returnObject归还PooledObject,invalidateObject调用PooledObjectFactory销毁PooledObject,addObject调用PooledObjectFactory创建PooledObject,getNumIdle给出PooledObject空闲个数,getNumActive给出PooledObject激活的个数,使用Apache commons-pool2的使用者可以使用默认的5个实现(SoftReferenceObjectPool GenericObjectPool ProxiedObjectPool GenericKeyedObjectPool ProxiedKeyedObjectPool),也可以自己实现


使用对象池来管理socket连接,分三步:

  • 步骤一:实现自己的PooledObjectFactory

  • 步骤二:创建ObjectPool对象

  • 步骤三:从ObjectPool获取到PooledObject对象,进行相关业务操作 

实现自己的PooledObjectFactory

package com.ghost.pool.socket;

 

import java.io.IOException;

import java.net.Socket;

 

import org.apache.commons.pool2.PooledObject;

import org.apache.commons.pool2.PooledObjectFactory;

import org.apache.commons.pool2.impl.DefaultPooledObject;

 

publicclass SocketPooledObjectFactory implements  PooledObjectFactory<Socket> {

 

    private String hostname;

    privateintport;

 

    public SocketPooledObjectFactory(String hostname, intport) {

        this.hostname = hostname;

        this.port = port;

    }

 

    @Override

    public PooledObject<Socket>  makeObject() throws Exception {

        returnnew DefaultPooledObject<Socket>(new Socket(hostname, port));

    }

 

    @Override

    publicvoid  destroyObject(PooledObject<Socket> p) throws Exception {

        Socket socket = p.getObject();

        if (socket != null) {

            try {

                socket.close();

            } catch (IOException e) {

                System.out.println("close socket failed.");

                throwe;

            }

            socket = null;

        }

    }

 

    @Override

    publicboolean  validateObject(PooledObject<Socket> p) {

        Socket socket = p.getObject();

        if (socket == null){

            returnfalse;

        }

       

        if (socket.isClosed()){

            returnfalse;

        }

       

        if (!socket.isConnected()){

            returnfalse;

        }

       

        returntrue;

    }

 

    @Override

    publicvoid  activateObject(PooledObject<Socket> p) throws Exception {

        if (p.getObject() == null){

            p = new DefaultPooledObject<Socket>(new Socket(hostname, port));

        }

 

    }

 

    @Override

    publicvoid  passivateObject(PooledObject<Socket> p) throws Exception {

 

    }

}

 

 创建ObjectPool对象,用默认的GenericObjectPool

package com.ghost.pool;

 

import java.net.Socket;

import java.util.NoSuchElementException;

 

import org.apache.commons.pool2.ObjectPool;

import org.apache.commons.pool2.PooledObjectFactory;

import org.apache.commons.pool2.impl.GenericObjectPool;

 

import com.ghost.pool.socket.SocketPooledObjectFactory;

 

/**

 * Hello world!

 *

 */

publicclass App

{

    publicstaticvoid main( String[] args ) throws  NoSuchElementException, IllegalStateException, Exception

    {

     PooledObjectFactory<Socket>  factory = new SocketPooledObjectFactory("localhost",8080);

     ObjectPool  pool = new GenericObjectPool(factory);

     Socket  socket1 = (Socket) pool.borrowObject();

     System.out.println(socket1);

     

     Socket  socket2 = (Socket) pool.borrowObject();

     System.out.println(socket2);

     

     pool.returnObject(socket1);

     

     Socket  socket3= (Socket) pool.borrowObject();

     System.out.println(socket3);

    }

}

 

 通过pool的borrowObject可以获取对象,使用完成后归还returnObject给pool


以上是关于连接池apache-common-pool2的主要内容,如果未能解决你的问题,请参考以下文章

Java源码系列-手写数据库连接池

聊聊hikari连接池的leakDetectionThreshold

java中建立数据库连接池,有哪几个步骤

数据库连接池的Java连接池

mysql数据库连接池

Node.js实现MySQL数据库连接池