package com.binovel.util; import org.apache.commons.pool2.KeyedPooledObjectFactory; import org.apache.commons.pool2.PooledObject; import org.apache.commons.pool2.impl.DefaultPooledObject; import org.apache.commons.pool2.impl.GenericKeyedObjectPool; public class MyTestPool { private GenericKeyedObjectPool<String, FileUtilTest> myObjectPool; public void init(){ if(this.myObjectPool == null){ this.myObjectPool = new GenericKeyedObjectPool<String, FileUtilTest>( new KeyedPooledObjectFactory<String, FileUtilTest>() { public PooledObject<FileUtilTest> makeObject(String key) throws Exception { //这里是生成对象 FileUtilTest file = new FileUtilTest(); DefaultPooledObject<FileUtilTest> pooledObject = new DefaultPooledObject<FileUtilTest>(file); return pooledObject; } public void destroyObject(String key, PooledObject<FileUtilTest> p) throws Exception { System.out.println("an object closed"); } public boolean validateObject(String key, PooledObject<FileUtilTest> p) { // TODO Auto-generated method stub //这里是返回是否对象池的对象是正确的,如果返回false的话 就会一直取对象,直到报错 //这里需要对象中有一个判断方法来判断是否active //我就是瞎搞搞 直到这个方法是干嘛就行了 return true; } //看borrowobject源代码,在成功从queue中获取到对象时,会调用这个方法来激活这个对象 //然后再调用上面那个validateObject来确定对象是不是好用,所以说这个方法也很重要 //if (null != factory && null != obj) { // try { //factory.activateObject(ref); // if (!factory.validateObject(ref)) { // throw new Exception("ValidateObject failed"); // } public void activateObject(String key, PooledObject<FileUtilTest> p) throws Exception { // TODO Auto-generated method stub } public void passivateObject(String key, PooledObject<FileUtilTest> p) throws Exception { // TODO Auto-generated method stub } } ); // Set max idle (not max active) since our connections always idle in the pool. this.myObjectPool.setMaxIdlePerKey(1); // We always want our validate method to control when idle objects are evicted. this.myObjectPool.setTestOnBorrow(true); this.myObjectPool.setTestWhileIdle(true); } } public GenericKeyedObjectPool<String, FileUtilTest> getPooded(){ return this.myObjectPool; } public synchronized FileUtilTest getFileUtilTest(String key){ FileUtilTest test = null; //这里我写死设置线程池中为5 if(this.getPooded().getNumIdle(key)<5){ try { // we want borrowObject to return the one we added. myObjectPool.setLifo(true); myObjectPool.addObject(key); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }else{ myObjectPool.setLifo(false); } while(test==null){ try { //这里循环取出对象 有对象的话就break //其实写的不好 可以看下人家的pool中的实现 /* while (connection == null) { connection = connectionsPool.borrowObject(key); synchronized (connection) { if (connection.getConnection() != null) { connection.incrementReferenceCount(); break; } // Return the bad one to the pool and let if get destroyed as normal. connectionsPool.returnObject(key, connection); connection = null; } }*/ test = myObjectPool.borrowObject(key); synchronized (test) { if (test != null) { break; } //这里如果失败了就把对象放回去 myObjectPool.returnObject(key, test); } } catch (Exception e) { System.out.println("this is message"); e.printStackTrace(); } } return test; } public static void main(String[] args) { MyTestPool mypool = new MyTestPool(); mypool.init(); FileUtilTest file = mypool.getFileUtilTest("first"); FileUtilTest file2 = mypool.getFileUtilTest("second"); System.out.println(file); System.out.println(file2); } }
这段时间看JMS 规范。 顺便看了下Activemq 的实现,其中activemq 自己实现了connectionpool 来存储大对象Connection 其中实现利用了apache.commons.pool中的GenericKeyedObjectPool 线程池 就顺便自己模仿这学习了下 ,代码里面写的很清楚了。。最后返回2个不同的对象
附带一个blog 这里面是objectPool的源码分析 挺好的
http://shmilyaw-hotmail-com.iteye.com/blog/1986566
还有就是这个 写的是pool2的 整体结构,感觉比原来多的是一个PooledObject包装类,所以我们在makeObject中返回的是一个包装类而不是直接一个object,这是自己的理解,还要看看pool1中的实现,
据说pool2 全部重新架构了对象池。。。哎 不知道有时间没 看看吧
相关推荐
org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet...
com.springsource.org.apache.commons.pool-sources-1.5.3.jar源码
org.apache.commons.lang.BitField.class org.apache.commons.lang.BooleanUtils.class org.apache.commons.lang.CharEncoding.class org.apache.commons.lang.CharRange.class org.apache.commons.lang.CharSet...
org.apache.commons.commons-math3:3.6.1
import org.apache.commons.httpclient.HttpClient; import org.apache.commons.httpclient.HttpException; import org.apache.commons.httpclient.HttpStatus; import org.apache.commons.httpclient.methods....
jar包分享,你懂的 com.springsource.org.apache.commons.io-1.4.0.jar
Maven坐标:org.apache.commons:commons-pool2:2.10.0; 标签:apache、commons、pool2、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译...
com.springsource.org.apache.commons.logging-1.1.1.jar
com.springsource.org.apache.commons.lang
com.springsource.org.apache.commons.logging-sources-1.1.1.jar
最新org.apache.commons.net..包完整、干净、实用
org.apache.commons.beanutils.jar
com.springsource.org.apache.commons.fileupload 自己打入本地Jar包即可
org.apache.commons.dbcp.BasicDataSource的解决方法,commons-dbcp.jar、commons-pool.jar
org.apache.commons.lang所有包的集合,
import org.apache.commons.beanutils.PropertyUtilsBean; import org.apache.commons.lang.StringUtils; import org.apache.poi.hssf.usermodel.HSSFCell; import org.apache.poi.hssf.usermodel.HSSFCellStyle; ...
解决经常遇到的The import org.apache.commons.codec cannot be resolved问题,只需导入文件中的commons-codec-1.11.jar就可以,如和导入包就不赘述了。
com.springsource.org.apache.commons.dbcp-sources-1.2.2.osgi.jar源码 jar包
Maven坐标:org.apache.commons:commons-pool2:2.0; 标签:apache、commons、pool2、中文文档、jar包、java; 使用方法:解压翻译后的API文档,用浏览器打开“index.html”文件,即可纵览文档内容。 人性化翻译,...
org.apache.commons.net.ftp.FTPClient包,不错,在实际项目和产品中使用过。