当前位置: 首页 > 工具软件 > 豆豆Pool > 使用案例 >

commons-pool实战之 PoolableObjectFactory和ObjectPool

许正平
2023-12-01
工作中需要减少对一些比较耗系统资源对象的创建和初始化工作,因此想到了apache commons-pool工具包。commons-pool包里主要包括三个重要的接口:
ObjectPool用于管理要被池化的对象的借出和归还;
ObjectPoolFactory用于大量生成相同类型和设置的ObjectPool。

看看下面的例子
一个Connection类,可以想象成一个远程连接比如数据库连接等。其中包括创建连接,关闭连接,和一个print方法。
package com.googlecode.garbagecan.commons.pool.sample1;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyConnection {
	
	private static Logger logger = LoggerFactory.getLogger(MyConnection.class);
	
	private String name;
	private boolean connected;

	public MyConnection(String name) {
		this.name = name;
	}

	public void connect() {
		this.connected = true;
		logger.info(name + ": " + connected);
	}

	public void close() {
		this.connected = false;
		logger.info(name + ": " + connected);
	}

	public boolean isConnected() {
		return this.connected;
	}
	
	public String getName() {
		return this.name;
	}
	
	public void print() {
		logger.info(this.name);
	}
}
一个PoolableObjectFactory接口的实现类,提供makeObject, activateObject, passivateObject, validateObject, destroyObject方法。
package com.googlecode.garbagecan.commons.pool.sample1;

import org.apache.commons.pool.PoolableObjectFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MyConnectionPoolableObjectFactory implements PoolableObjectFactory {

	private static Logger logger = LoggerFactory.getLogger(MyConnectionPoolableObjectFactory.class);
	
	private static int count = 0;
	
	public Object makeObject() throws Exception {
		MyConnection myConn = new MyConnection("conn_" + (++count));
		myConn.connect();
		logger.info(myConn.getName());
		return myConn;
	}
	
	public void activateObject(Object obj) throws Exception {
		MyConnection myConn = (MyConnection)obj;
		logger.info(myConn.getName());
	}

	public void passivateObject(Object obj) throws Exception {
		MyConnection myConn = (MyConnection)obj;
		logger.info(myConn.getName());
	}
	
	public boolean validateObject(Object obj) {
		MyConnection myConn = (MyConnection)obj;
		logger.info(myConn.getName());
		return myConn.isConnected();
	}
	
	public void destroyObject(Object obj) throws Exception {
		MyConnection myConn = (MyConnection)obj;
		logger.info(myConn.getName());
		myConn.close();
	}
}
最后是一个测试类
package com.googlecode.garbagecan.commons.pool.sample1;

import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.StackObjectPool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class Test {
	private static Logger logger = LoggerFactory.getLogger(Test.class);
	
	public static void main(String[] args) throws Exception {
		PoolableObjectFactory factory = new MyConnectionPoolableObjectFactory();
		ObjectPool pool = new StackObjectPool(factory);
		try {
			logger.info("================================================");
			for (int i = 0; i < 10; i++) {
				MyConnection myConn = (MyConnection)pool.borrowObject();
				try {
					myConn.print();
				} catch(Exception ex) {
					pool.invalidateObject(myConn);
				} finally {
					pool.returnObject(myConn);
				}
			}
			
			logger.info("================================================");
			for (int i = 0; i < 10; i++) {
				MyConnection myConn1 = (MyConnection)pool.borrowObject();
				MyConnection myConn2 = (MyConnection)pool.borrowObject();
				myConn1.print();
				myConn2.print();
				pool.returnObject(myConn1);
				pool.returnObject(myConn2);
			}
		} finally {
			try {
				pool.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
	}
}
运行测试类,可以看到在第一个循环里虽然循环了10次,一共要了10个MyConnection对象,但是每次返回的都是“conn_1”这个MyConnection对象实例,并且从日志可以看出,makeObject方法只被调用了一次,因此,除了第一次以外,后面的每次申请都是从pool里取出来的。而在第二个循环中,每次申请了两个MyConnection对象实例,从日志可以看到,在第二个循环里也只调用了一次makeObject方法,并且创建的是conn_2对象实例,这是由于conn_1这个对象已经在第一个循环中被创建了出来,此时只是直接拿出来使用了。这里为了好测试,没有在第二个循环中做异常处理,真实情况下应该像第一个循环里的代码类是,在borrowObject和使用pool中对象出现异常时要记得调用invalidateObject方法,并且归还pool中的对象。




 类似资料: