commons-pool对象池实现原理及使用(二)
程序员文章站
2022-07-14 16:13:47
...
纸上得来终觉浅,绝知此事要躬行,我们用Commons-pool-1.6来写一个我们自己的对象池。
类MyObject 为池对象
类MyPoolableObjectFactory 为池对象工厂
类MyObjectPool封装的一个对象池
/**
*
* @author yujie.wang
* 测试类
*/
public class MyObject {
private String name;
// true 表明对象可用,false表明当前对象不可用
private boolean isValidate;
private String state ;
private boolean passivateFlag;
public MyObject(String name, boolean isValidate,String state){
this.name = name;
this.isValidate = isValidate;
this.state = state;
}
public void sayHello(){
System.out.println(name + " say hello world");
}
public boolean isPassivateFlag() {
return passivateFlag;
}
public void setPassivateFlag(boolean passivateFlag) {
this.passivateFlag = passivateFlag;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isValidate() {
return isValidate;
}
public void setValidate(boolean isValidate) {
this.isValidate = isValidate;
}
}
/**
*
* @author yujie.wang
* 池对象工厂 对象池会调用这个工厂操作我们的对象 比如创建 ** 验证 销毁 钝化
*/
public class MyPoolableObjectFactory extends BasePoolableObjectFactory<MyObject>{
@Override
public void activateObject(MyObject arg0) throws Exception {
// TODO Auto-generated method stub
arg0.setState("i am ok");
arg0.setValidate(true);
System.out.println(arg0.getName() + " 对象已经**");
}
/**
* 归还对象时 如果调用validateObject 验证对象失败,或者连接池被关闭,
* 又或者归还对象时连接池中空闲的对象数量大于等于MaxIdel
* 若符合上述三种情况之一 都会调用这个方法销毁对象
*
*/
@Override
public void destroyObject(MyObject arg0) throws Exception {
// TODO Auto-generated method stub
arg0.setState("i am done");
arg0.setValidate(false);
System.out.println(arg0.getName() + " 对象已经销毁");
}
/**
* 对象池调用该方法 创建一个对象
*/
@Override
public MyObject makeObject() throws Exception {
// TODO Auto-generated method stub
MyObject obj = new MyObject("new obj", false, " i am new");
return obj;
}
/**
* 归还对象时 钝化对象 比如某些对象用完之后需要休眠一段时间
*/
@Override
public void passivateObject(MyObject arg0) throws Exception {
// TODO Auto-generated method stub
arg0.setPassivateFlag(true);
System.out.println(arg0.getName() + " 钝化完成");
}
/**
* testOnBorrow设置为true时,创建对象时调用该方法验证对象的有效性,
* 如果无效直接抛出异常throw new Exception("ValidateObject failed");
* testOnReturn设置为true时,归还对象时 验证对象是否还有效 比如连接是否还在,
* 如果无效了则不往对象池中放对象。
*/
@Override
public boolean validateObject(MyObject arg0) {
// TODO Auto-generated method stub
System.out.println(arg0.getName() + " 验证对象 "+ arg0.isValidate());
return arg0.isValidate() ? true :false;
}
}
/**
*
* @author yujie.wang
* 直接使用GenericObjectPool 来封装一个对象池组件
*/
public class MyObjectPool {
private GenericObjectPool<MyObject> pool;
public MyObjectPool(PoolableObjectFactory<MyObject> factory,MyObjectConfig config){
pool = new GenericObjectPool<MyObject>(factory, config);
}
/**
* 从对象池中返回一个对象
* @return
*/
public MyObject borrowObject(){
try {
return pool.borrowObject();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
/**
* 将已经使用过的对象返还到对象池
* @param obj
*/
public void returnObject(MyObject obj){
try {
pool.returnObject(obj);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 使对象池中的obj对象无效
* @param obj
*/
public void invalidateObject(MyObject obj){
try {
//内部调用了池对象工程的destroyObject方法
pool.invalidateObject(obj);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 关闭连接池
*/
public void destory(){
try {
pool.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
//这里通过继承GenericObjectPool.Config 这个内部类 来操作相关参数
public static class MyObjectConfig extends GenericObjectPool.Config{
public MyObjectConfig(){
setTestWhileIdle(true);
setMinEvictableIdleTimeMillis(60000L);
setTimeBetweenEvictionRunsMillis(30000L);
setNumTestsPerEvictionRun(-1);
}
public int getMaxIdle() {
return maxIdle;
}
public void setMaxIdle(int maxIdle) {
this.maxIdle = maxIdle;
}
public int getMinIdle() {
return minIdle;
}
public void setMinIdle(int minIdle) {
this.minIdle = minIdle;
}
public int getMaxActive() {
return maxActive;
}
public void setMaxActive(int maxActive) {
this.maxActive = maxActive;
}
public long getMaxWait() {
return maxWait;
}
public void setMaxWait(long maxWait) {
this.maxWait = maxWait;
}
public byte getWhenExhaustedAction() {
return whenExhaustedAction;
}
public void setWhenExhaustedAction(byte whenExhaustedAction) {
this.whenExhaustedAction = whenExhaustedAction;
}
public boolean isTestOnBorrow() {
return testOnBorrow;
}
public void setTestOnBorrow(boolean testOnBorrow) {
this.testOnBorrow = testOnBorrow;
}
public boolean isTestOnReturn() {
return testOnReturn;
}
public void setTestOnReturn(boolean testOnReturn) {
this.testOnReturn = testOnReturn;
}
public boolean isTestWhileIdle() {
return testWhileIdle;
}
public void setTestWhileIdle(boolean testWhileIdle) {
this.testWhileIdle = testWhileIdle;
}
public long getTimeBetweenEvictionRunsMillis() {
return timeBetweenEvictionRunsMillis;
}
public void setTimeBetweenEvictionRunsMillis(
long timeBetweenEvictionRunsMillis) {
this.timeBetweenEvictionRunsMillis = timeBetweenEvictionRunsMillis;
}
public int getNumTestsPerEvictionRun() {
return numTestsPerEvictionRun;
}
public void setNumTestsPerEvictionRun(int numTestsPerEvictionRun) {
this.numTestsPerEvictionRun = numTestsPerEvictionRun;
}
public long getMinEvictableIdleTimeMillis() {
return minEvictableIdleTimeMillis;
}
public void setMinEvictableIdleTimeMillis(long minEvictableIdleTimeMillis) {
this.minEvictableIdleTimeMillis = minEvictableIdleTimeMillis;
}
public long getSoftMinEvictableIdleTimeMillis() {
return softMinEvictableIdleTimeMillis;
}
public void setSoftMinEvictableIdleTimeMillis(
long softMinEvictableIdleTimeMillis) {
this.softMinEvictableIdleTimeMillis = softMinEvictableIdleTimeMillis;
}
}
}
测试类:
/**
*
* @author yujie.wang
* 测试类
*/
public class TestMyObjectPool {
public static void main(String[] args) {
// TODO Auto-generated method stub
//创建对象池
MyObjectPool pool = new MyObjectPool(new MyPoolableObjectFactory(),getConfig());
//从对象池中借出一个对象
MyObject object = pool.borrowObject();
object.sayHello();
//object.setValidate(false);
//归还对象
System.out.println("***归还对象***");
pool.returnObject(object);
}
/**
* 配置对象池的相关参数
* @return
*/
public static MyObjectPool.MyObjectConfig getConfig(){
MyObjectPool.MyObjectConfig config = new MyObjectPool.MyObjectConfig();
config.setMaxActive(10);
config.setMaxIdle(5);
config.setMinIdle(0);
config.setTestOnBorrow(true);
config.setTestOnReturn(true);
return config;
}
}
执行代码运行结果如下:
new obj 对象已经**
new obj 验证对象 true
new obj say hello world
***归还对象***
new obj 验证对象 true
new obj 钝化完成
上一篇: 由数据库连接所联想到的5种设计模式