Apache Pool 2 - 具有固定容量的预定义对象实例

时间:2018-01-28 11:02:11

标签: java apache object-pooling

我想使用Apache Pool 2库,但寻找以下功能:

  1. 对象不是虚拟的新空实例,而是来自外部数据源的预加载对象。您可以想象每个对象都是唯一的工作配置,可以传递给通用任务,这样的任务就可以工作。我不需要清理物品,只需将它们归还。
  2. 池中对象的数量是固定的。
  3. 编写自己的游泳池是不是更好?或者用apache实现这个目标吗?

    或者是否有更好的模式可供使用?

    谢谢。

    致以最诚挚的问候,

    的Ladislav

1 个答案:

答案 0 :(得分:0)

好的,我在Apache Pool的网站上关注了这个例子,并且还调查了文档,但是我真的没有找到如何将我以前创建的对象推入池中的方法,所以我找到工作的唯一方法丑陋的是: 1.在一个循环中从池中获取虚拟空对象,并将它们放到外部ArrayList中 2.循环列表并设置对象并将其返回到池中

在我尝试从池中获取对象并使用以下实现后,保持其设置是按预期的行为,但我想使用工厂来创建PooledObject,我可以以某种方式推送到池中。我尝试了pool对象的use(),或者genericobjectfactory的wrap()方法,但是它没有按照我的预期工作。所以下面的代码按预期工作,很遗憾,因为我刚刚测试了功能,所以很难看:

public class ProxyMetadata  {

    public static Logger LOG = LogManager.getLogger(ProxyMetadata.class);
    public static List<Proxy> PROXIES = new ArrayList<>();
    public static int PROXIES_AVAILABLE;

    private static ProxyBufferFactory proxyBufferFactory = new ProxyBufferFactory();
    private static GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();
    private static GenericObjectPool<Proxy> pool;
    //Pool<Proxy> pool = new BoundedBlockingPool< Proxy >();


    void init() {
        final List<Proxy> availableProxies = getAvailableProxies();
        genericObjectPoolConfig.setMaxTotal(PROXIES_AVAILABLE);
        genericObjectPoolConfig.setBlockWhenExhausted(true);
        genericObjectPoolConfig.setTestOnBorrow(true);
        //pool = new BoundedBlockingPool< Proxy >();
        pool = new GenericObjectPool<Proxy>(proxyBufferFactory,genericObjectPoolConfig);
        List<Proxy> initializator = new ArrayList<>();
        LOG.debug("Pool management starting...");
        LOG.debug("Pool start idle:" + pool.getNumIdle());
        LOG.debug("Pool start active:" + pool.getNumActive());
        for (int i= 0; i < PROXIES_AVAILABLE; i++){
            try {

                //final Proxy proxy = new Proxy();
                final Proxy proxy = pool.borrowObject();
                proxy.setProxyHost(String.valueOf(i));
                //proxyBufferFactory.wrap(proxy);
                initializator.add(proxy);
                //pool.returnObject(proxy);
                LOG.debug("Pool idle - loop " + String.valueOf(i)+ ": " + pool.getNumIdle());
                LOG.debug("Pool active - loop"+ String.valueOf(i) + ": " + pool.getNumActive());

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        LOG.debug("ALL PROXIES BORROWED");
        LOG.debug("Pool after borrow idle:" + pool.getNumIdle());
        LOG.debug("Pool after borrow active:" + pool.getNumActive());
        LOG.debug("Going to return");
        for (Proxy proxy : initializator){
            pool.returnObject(proxy);
            LOG.debug("Pool after return idle:" + pool.getNumIdle());
            LOG.debug("Pool after return active:" + pool.getNumActive());
        }

        LOG.debug("Clearing buffer: " + initializator.size());
        initializator.clear();
        LOG.debug("Clearing buffer: " + initializator.size());
        LOG.debug("Pool status before reborrow.");
        LOG.debug("Pool idle:" + pool.getNumIdle());
        LOG.debug("Pool active:" + pool.getNumActive());
        for (int i= 0; i < PROXIES_AVAILABLE; i++){
            try {

                final Proxy proxy = pool.borrowObject();

                LOG.debug("Pool idle:" + pool.getNumIdle());
                LOG.debug("Pool active:" + pool.getNumActive());
                LOG.debug("Borrowed final proxy:" + proxy.toString());
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

这里是日志输出,可以看到再次借用最初借用和配置的对象可用:

Pool management starting...
Pool start idle:0
Pool start active:0
Pool idle - loop 0: 0
Pool active - loop0: 1
Pool idle - loop 1: 0
Pool active - loop1: 2
Pool idle - loop 2: 0
Pool active - loop2: 3
ALL PROXIES BORROWED
Pool after borrow idle:0
Pool after borrow active:3
Going to return
Pool after return idle:1
Pool after return active:2
Pool after return idle:2
Pool after return active:1
Pool after return idle:3
Pool after return active:0
Clearing buffer: 3
Clearing buffer: 0
Pool status before reborrow.
Pool idle:3
Pool active:0
Pool idle:2
Pool active:1
Borrowed final proxy:Proxy(proxyHost=2, proxyPort=null, proxyUser=null, proxyPassword=null, geoIPLocation=null, isAvailable=true)
Pool idle:1
Pool active:2
Borrowed final proxy:Proxy(proxyHost=1, proxyPort=null, proxyUser=null, proxyPassword=null, geoIPLocation=null, isAvailable=true)
Pool idle:0
Pool active:3
Borrowed final proxy:Proxy(proxyHost=0, proxyPort=null, proxyUser=null, proxyPassword=null, geoIPLocation=null, isAvailable=true)

    Process finished with exit code 0

所以行为是预期的,但我只是用PooledObject的put()方法扩展池作为参数,这样你就可以用更直观的方式初始化池......