服务热线:13616026886

技术文档 欢迎使用技术文档,我们为你提供从新手到专业开发者的所有资源,你也可以通过它日益精进

位置:首页 > 技术文档 > JAVA > 新手入门 > 基础入门 > 查看文档

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 {@link reader} 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专题进行介绍,本篇是本系列的第二篇,以后将陆续推出,下期主题就是dbcp,请继续关注.
6.参考 -- 相关书目或相关文章
*jakarta commons:
http://jakarta.apache.org/commons/
*jakarta commons pool
http://jakarta.apache.org/commons/pool/
*jakarta commons pool api:
http://jakarta.apache.org/commons/pool/apidocs/index.html
*oreilly: using the jakarta commons, part 3: #3
http://www.onjava.com/pub/a/onjava/2003/07/23/commons.html?page=3

扫描关注微信公众号