Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Commons池:如何实例化一个具体的池?_Java_Apache_Pooling_Apache Commons Pool - Fatal编程技术网

Java Commons池:如何实例化一个具体的池?

Java Commons池:如何实例化一个具体的池?,java,apache,pooling,apache-commons-pool,Java,Apache,Pooling,Apache Commons Pool,我已将commons-pooling-1.6.jar添加到我的类路径中,并尝试实例化StackObjectPool,但每次都失败了: // Deprecated. ObjectPool<T> oPool = new StackObjectPool<T>(); // Error: Cannot instantiate the type BasePoolableObjectFactory<T>. PoolableObjectFactory<T> oF

我已将commons-pooling-1.6.jar添加到我的类路径中,并尝试实例化
StackObjectPool
,但每次都失败了:

// Deprecated.
ObjectPool<T> oPool = new StackObjectPool<T>();

// Error: Cannot instantiate the type BasePoolableObjectFactory<T>.
PoolableObjectFactory<T> oFact = new BasePoolableObjectFactory<T>();
ObjectPool<T> oPool = new StackObjectPool<T>(oFact);
//已弃用。
ObjectPool opol=新的StackObjectPool();
//错误:无法实例化类型BasePoolableObjectFactory。
PoolableObjectFactory OFAT=新的BasePoolableObjectFactory();
ObjectPool OPOL=新堆栈对象池(OFAT);

这是一个不推荐使用的API吗?如果是这样的话,有哪些开源方案可以替代公共资源池?否则,如何实例化一个
StackObjectPool

您需要编写自己的工厂,可能需要扩展BasePoolableObjectFactory。有关更多信息,请参见此处:

下面是创建StringBuffers的PoolableObjectFactory实现:

import org.apache.commons.pool.BasePoolableObjectFactory; 

public class StringBufferFactory extends BasePoolableObjectFactory<StringBuffer> { 
    // for makeObject we'll simply return a new buffer 
    public StringBuffer makeObject() { 
        return new StringBuffer(); 
    } 

    // when an object is returned to the pool,  
    // we'll clear it out 
    public void passivateObject(StringBuffer buf) { 
        buf.setLength(0); 
    } 

    // for all other methods, the no-op  
    // implementation in BasePoolableObjectFactory 
    // will suffice 
}
import org.apache.commons.pool.BasePoolableObjectFactory;
公共类StringBufferFactory扩展BasePoolableObjectFactory{
//对于makeObject,我们只需返回一个新的缓冲区
公共StringBuffer makeObject(){
返回新的StringBuffer();
} 
//当对象返回到池中时,
//我们会解决的
公共无效被动对象(StringBuffer buf){
buf.设定长度(0);
} 
//对于所有其他方法,无操作
//BasePoolableObjectFactory中的实现
//就够了
}
然后按如下方式使用:

new StackObjectPool<StringBuffer>(new StringBufferFactory())
new StackObjectPool(new StringBufferFactory())

您需要编写自己的工厂,可能需要扩展BasePoolableObjectFactory。有关更多信息,请参见此处:

下面是创建StringBuffers的PoolableObjectFactory实现:

import org.apache.commons.pool.BasePoolableObjectFactory; 

public class StringBufferFactory extends BasePoolableObjectFactory<StringBuffer> { 
    // for makeObject we'll simply return a new buffer 
    public StringBuffer makeObject() { 
        return new StringBuffer(); 
    } 

    // when an object is returned to the pool,  
    // we'll clear it out 
    public void passivateObject(StringBuffer buf) { 
        buf.setLength(0); 
    } 

    // for all other methods, the no-op  
    // implementation in BasePoolableObjectFactory 
    // will suffice 
}
import org.apache.commons.pool.BasePoolableObjectFactory;
公共类StringBufferFactory扩展BasePoolableObjectFactory{
//对于makeObject,我们只需返回一个新的缓冲区
公共StringBuffer makeObject(){
返回新的StringBuffer();
} 
//当对象返回到池中时,
//我们会解决的
公共无效被动对象(StringBuffer buf){
buf.设定长度(0);
} 
//对于所有其他方法,无操作
//BasePoolableObjectFactory中的实现
//就够了
}
然后按如下方式使用:

new StackObjectPool<StringBuffer>(new StringBufferFactory())
new StackObjectPool(new StringBufferFactory())

大多数库都将输入重点放在对象工厂上。这将告诉池在需要时如何创建新对象。例如,对于连接池,所有这些连接都连接到具有相同配置的同一数据库,如用户、密码、url、驱动程序

需要创建一个具体的工厂扩展类并编写方法,如下例所示

static class MyObject {
    private String config;

    public MyObject(String config) {
        this.config = config;
    }
}

static class MyFactory extends BasePoolableObjectFactory<MyObject> {

    public String config;

    public MyFactory(String config) {
        this.config = config;
    }

    @Override
    public MyObject makeObject() throws Exception {
        return new MyObject(config);
    }
}

public static void main(String[] args) {
    MyFactory factory = new MyFactory("config parameters");
    StackObjectPool<MyObject> pool = new StackObjectPool<>(factory);
}
静态类MyObject{
私有字符串配置;
公共MyObject(字符串配置){
this.config=config;
}
}
静态类MyFactory扩展了BasePoolableObjectFactory{
公共字符串配置;
公共MyFactory(字符串配置){
this.config=config;
}
@凌驾
公共MyObject makeObject()引发异常{
返回新的MyObject(配置);
}
}
公共静态void main(字符串[]args){
MyFactory=新的MyFactory(“配置参数”);
StackObjectPool=新的StackObjectPool(工厂);
}

斯瓦兰加·萨尔玛编纂了一个非常有趣的例子。请参见

大多数库都将输入焦点放在对象工厂上。这将告诉池在需要时如何创建新对象。例如,对于连接池,所有这些连接都连接到具有相同配置的同一数据库,如用户、密码、url、驱动程序

需要创建一个具体的工厂扩展类并编写方法,如下例所示

static class MyObject {
    private String config;

    public MyObject(String config) {
        this.config = config;
    }
}

static class MyFactory extends BasePoolableObjectFactory<MyObject> {

    public String config;

    public MyFactory(String config) {
        this.config = config;
    }

    @Override
    public MyObject makeObject() throws Exception {
        return new MyObject(config);
    }
}

public static void main(String[] args) {
    MyFactory factory = new MyFactory("config parameters");
    StackObjectPool<MyObject> pool = new StackObjectPool<>(factory);
}
静态类MyObject{
私有字符串配置;
公共MyObject(字符串配置){
this.config=config;
}
}
静态类MyFactory扩展了BasePoolableObjectFactory{
公共字符串配置;
公共MyFactory(字符串配置){
this.config=config;
}
@凌驾
公共MyObject makeObject()引发异常{
返回新的MyObject(配置);
}
}
公共静态void main(字符串[]args){
MyFactory=新的MyFactory(“配置参数”);
StackObjectPool=新的StackObjectPool(工厂);
}

斯瓦兰加·萨尔玛编纂了一个非常有趣的例子。参见

我本打算抱怨使用“编目”来表示“已对实现进行了编码”,但读了那篇文章后,我认为术语“编目”非常合适!我本打算抱怨使用“编目”来表示“已编码的实现”,但读了那篇文章后,我认为术语“编目”非常合适!