这个其实没怎么仔细研究,不过好像也挺简单的,下面是一个简单的例子。(可能还有其他更简单的用法,等我看完源代码再说)
1。自己实现一下
PoolableObjectFactory。主要是实现makeObject方法,生成并返回一个自己想要的类实例。如果想要实现可重用也很简单,加一个setObject的方法,然后在makeObject中使用反射机制生成类的实例。package com.pool;
import org.apache.commons.pool.PoolableObjectFactory;
import com.domain.User;
public class MyObjectFactory implements PoolableObjectFactory {
private int id;
public MyObjectFactory() {
super();
}
public Object makeObject() throws Exception {
User user=new User();
user.setId(id);
user.setName("name"+id);
id++;
return user;
}
public void destroyObject(Object arg0) throws Exception {
// TODO Auto-generated method stub
}
public boolean validateObject(Object arg0) {
// TODO Auto-generated method stub
return true;
}
public void activateObject(Object arg0) throws Exception {
// TODO Auto-generated method stub
}
public void passivateObject(Object arg0) throws Exception {
// TODO Auto-generated method stub
}
}
然后通过如下代码进行使用:
GenericObjectPool pool = new GenericObjectPool(new MyObjectFactory());
pool.setMaxActive(5);//默认是8
pool.borrowObject();//取出object
pool.returnObject(yourobject);//把取出的object还给对象池
posted on 2006-04-12 13:08
OO 阅读(1251)
评论(0) 编辑 收藏 所属分类:
框架、工具的使用