Jakarta Commons项目研究 --pool
一.pool项目到底什么是 pool, 简单来说, 就是先建立一些存在的 object, 放在 pool 之中,当你有需要的时候,可以从 pool 中直接获取,不需要重新建立. . 最常听到的就是 database connection pooling, 因为建立数据库连结是一件耗时的工作, 如果我们先把连结建立好, 就 可以节省这一些时间。database connection pooling 即DBCP。
1.基础类结构 pool的基础类:
public interface ObjectPool {
Object borrowObject();
void returnObject(Object borrowed);
}
主要扩展类 :
public interface KeyedObjectPool
{
Object borrowObject(Object key);
void returnObject(Object key, Object borrowed);
}
按照key来索引pool的对象 基础类仅提供了最基本的两个函数,用来创建和返回pool对象。
2.实现的基本类 BaseObjectPool--PoolableObjectFactory--BasePoolableObjectFactory KeyedObjectPool--KeyedPoolableObjectFactory--BaseKeyedPoolableObjectFactory StackObjectPool--StackKeyedObjectPool--可以在初始化创建实例,提供有限的 idle 数量GenericObjectPool--GenericKeyedObjectPool--包含了设定 idle, active 的数量以及回收到pool中的设置SoftReferenceObjectPool--可以随需要进行增加,他的回收是由垃圾回收站进行的总的来说,它提供了 Pool 的 API 主要有三个方面: 提供一般性的对象 pool 接口, 可以简单地去使用和实现. 比如BaseObjectPool和KeyedObjectPool. 提供小工具可以建立模块化的 pool. 比如StackObjectPool. 实现出一些通用性的 pool. 比如GenericObjectPool.
3.实现
ObjectPool
Object obj = null;
try
{
//创建对象
obj = pool.borrowObject();
//捕获异常
}
catch (Exception e) { }
finally
{
//确认对象是否已经返回
if(null != obj) {
pool.returnObject(obj);
}
}
KeyedObjectPool Object obj = null;
Object key = "Key";
try
{
//创建对象
obj = pool.borrowObject(key);
//捕获异常
}
catch (Exception e) { }
finally
{
//确认返回
if(null != obj) {
pool.returnObject(key, obj);
}
}
其它的类型继承开发就可以了
4.范例(取自apache的commons组) ReaderUtil.java
import java.io.Reader;
import java.io.IOException;
public class ReaderUtil {
public ReaderUtil() { }
/** * Dumps the contents of the {@link Reader} to a *
String, closing the {@link Reader} when done.
*/
public String readToString(Reader in) throws IOException
{
StringBuffer buf = new StringBuffer();
try {
for( int c = in.read(); c != -1; c = in.read())
{
buf.append((char) c);
}
return buf.toString();
} catch (IOException e) {
throw e;
} finally {
try {
in.close();
} catch (Exception e)
{
// ignored } } }
}
改换一下顺序, 先取得pool, 再由pool取值( 推荐使用)
import org.apache.commons.pool.ObjectPool;
import java.io.Reader;
import java.io.IOException;
public class ReaderUtil {
private ObjectPool pool;
public ReaderUtil(ObjectPool pool) {
this.pool = pool;
}
/** * Dumps the contents of the {@link Reader} to a * String,
closing the {@linkReader} when done.
*/
public String readToString(Reader in) throws IOException {
StringBuffer buf = null;
try {
buf = (StringBuffer) (pool.borrowObject());
for (int c = in.read(); c != -1; c = in.read()) {
buf.append((char) c);
}
return buf.toString();
} catch (IOException e) {
throw e;
} catch (Exception e) {
throw new RuntimeException("Unable to borrow buffer from pool" + e.toString());
} finally {
try {
in.close();
} catch (Exception e) {
// ignored } try { if(null != buf) { pool.returnObject(buf);
}
} catch(Exception
e) { // ignored } } } }
用StringBuffer做pool的例子(不推荐使用,仅供熟悉知识)
StringBufferFactory.java
import org.apache.commons.pool.BasePoolableObjectFactory;
public class StringBufferFactory extends BasePoolableObjectFactory {
// for makeObject we'll simply return a new buffer
public Object makeObject() {
return new StringBuffer();
}
// when an object is returned to the pool,
// we'll clear it out
public void passivateObject(Object obj) {
StringBuffer buf = (StringBuffer) obj;
buf.setLength(0);
}
// for all other methods, the no-op
// implementation in BasePoolableObjectFactory
// will suffice }
修改 ReaderUtil 由 StringBufferFactory Pool 得到 StringBuffer.
new ReaderUtil(
new StackObjectPool( new StringBufferFactory() ) )
5.总结我们通常会在 io 的部分采用 pool 机制, 减少一些建立存取的时间, 对于最耗时的数据
库存取, 更是相对的重要,我将会在commons-DBCP专题进行介绍,本篇是本系列的第二篇
本文详细解析了Java Commons Pool的概念、基础类结构、实现类型及其在IO操作、数据库连接池等场景的应用,强调了如何通过pool机制优化资源管理和性能提升。
997

被折叠的 条评论
为什么被折叠?



