超时后Java发布Poolable对象

时间:2013-09-19 04:15:09

标签: java connection-pooling apache-commons

我一直在寻找方法来池化一个对象,如果不用X分钟,它将被从对象池中删除。

此Poolable对象是与旧系统的套接字连接。我只想要一个持久连接,当需要新连接时,会创建更多对象来满足这种需求。但是我只希望这些新的非持久对象连接在最后一个请求之后可能持续5分钟,然后正常断开连接。

我不确定Apache Commons Pool项目是否可以提供帮助。

如何设置这种“创建/发布”规则?它是Apache Commons Pool的一部分,还是我的Object必须自己处理它?我真的不确定。

- 以色列

1 个答案:

答案 0 :(得分:1)

为了未来读者的利益,我使用commons pool2编写了一个小实现:

package com.apple.paymentgateway.communicator;

import org.apache.commons.pool2.BasePooledObjectFactory;
import org.apache.commons.pool2.PooledObject;
import org.apache.commons.pool2.PooledObjectFactory;
import org.apache.commons.pool2.impl.DefaultPooledObject;
import org.apache.commons.pool2.impl.GenericObjectPool;
import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

public class Test {

    public void initializePool() throws Exception {
        GenericObjectPoolConfig config = new GenericObjectPoolConfig();

        // parameters for the life cycle of the objects
        config.setMinIdle(1);
        config.setMaxIdle(1);
        config.setMaxTotal(1);

        // if the object is not accessed in 5 minutes, it evicted (throw away from the pool)
        config.setTimeBetweenEvictionRunsMillis(5 * 60 * 1000);
        // frequency at which it check for objects to be evicted
        config.setMinEvictableIdleTimeMillis(1 * 60 * 1000);

        CommonsObjectPool pool = new CommonsObjectPool(new MyObjectPoolFactory(), config);

        MyObject myObj = pool.borrowObject(); //borrow an object
    }

    /**
     * An object pool implementation of your objects
     */
    class CommonsObjectPool extends GenericObjectPool<MyObject> {

        public CommonsObjectPool(PooledObjectFactory<MyObject> factory) {
            super(factory);
        }

        public CommonsObjectPool(PooledObjectFactory<MyObject> factory, GenericObjectPoolConfig config) {
            super(factory, config);
        }
    }



    /**
     * Factory to create the objects
     */
    class MyObjectPoolFactory extends BasePooledObjectFactory<MyObject> {
        @Override
        public MyObject create() throws Exception {
            // create your object
            return new MyObject();
        }

        @Override
        public PooledObject<MyObject> wrap(MyObject arg0) {
            return new DefaultPooledObject<MyObject>(arg0);
        }

        @Override
        public boolean validateObject(PooledObject<MyObject> pooledObj) {
            return pooledObj.getObject().isMyObjectValid(); // implement a validation
        }

        @Override
        public void destroyObject(PooledObject<MyObject> pooledObj) throws Exception {
            // clean up the object
            //pooledObj.getObject().close ()
        }
    }   

    class MyObject {
        public boolean isMyObjectValid() {
            return true;
        }

        public void close () {
         // clean up the object
        }
    }
}

以下两行提供了您正在寻找的功能。

    // if the object is not accessed in 5 minutes, it evicted (throw away from the pool)
    config.setTimeBetweenEvictionRunsMillis(5 * 60 * 1000);
    // frequency at which it check for objects to be evicted
    config.setMinEvictableIdleTimeMillis(1 * 60 * 1000);