7

我正在尝试创建一个到队列服务器的通道/连接池,并尝试使用 ObjectPool,但在他们网站上的示例中使用它时遇到了问题。

到目前为止,我有线程可以工作,但我希望每个线程都从池中获取一个通道,然后返回它。我了解如何使用它(borrowObject/returnObjects),但不确定如何创建初始池。

以下是在 rabbitmq 中创建通道的方式:

ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    Connection connection = factory.newConnection();
    Channel channel = connection.createChannel();

我的代码只是使用通道来做事。我很困惑,因为我能找到的唯一例子(在他们的网站上)是这样开始的:

private ObjectPool<StringBuffer> pool;

    public ReaderUtil(ObjectPool<StringBuffer> pool) { 
        this.pool = pool;
    }

这对我来说没有意义。我意识到这对于建立数据库连接很常见,所以我尝试查找使用数据库和 ObjectPool 的教程,但它们似乎使用特定于数据库的 DBCP(而且我似乎无法使用我的队列服务器的逻辑)。

关于如何使用它的任何建议?还是有另一种方法用于java中的池?

4

2 回答 2

4

他们创建了一个创建对象的类,并且知道返回对象时要做什么。这对你来说可能是这样的:

public class PoolConnectionFactory extends BasePoolableObjectFactory<Connection> { 

    private final ConnectionFactory factory;
    public PoolConnectionFactory() {
        factory = new ConnectionFactory();
        factory.setHost("localhost");
    }

    // for makeObject we'll simply return a new Connection
    public Connection makeObject() { 
        return factory.newConnection();
    } 

    // when an object is returned to the pool,  
    // we'll clear it out 
    public void passivateObject(Connection con) { 
        con.I_don't_know_what_to_do(); 
    } 

    // for all other methods, the no-op  
    // implementation in BasePoolableObjectFactory 
    // will suffice 
}

现在你创建一个ObjectPool<Connection>地方:

ObjectPool<Connection> pool = new StackObjectPool<Connection>(new PoolConnectionFactory());

然后你可以pool在你的线程中使用

Connection c = pool.borrowObject();
c.doSomethingWithMe();
pool.returnObject(c);

对您没有意义的行是将池对象传递给不同类的一种方式。见最后一行,他们在创建阅读器的同时创建了池。

new ReaderUtil(new StackObjectPool<StringBuffer>(new StringBufferFactory()))
于 2012-04-28T23:35:37.263 回答
3

您将需要 PoolableObjectFactory 的自定义实现来创建、验证和销毁要池化的对象。然后将您的工厂实例传递给 ObjectPool 的构造函数,您就可以开始借用对象了。

这是一些示例代码。您还可以查看使用 commons-pool 的 commons-dbcp 的源代码。

import org.apache.commons.pool.BasePoolableObjectFactory;
import org.apache.commons.pool.ObjectPool;
import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.GenericObjectPool;

public class PoolExample {
    public static class MyPooledObject {
        public MyPooledObject() {
            System.out.println("hello world");
        }

        public void sing() {
            System.out.println("mary had a little lamb");
        }

        public void destroy() {
            System.out.println("goodbye cruel world");
        }
    }

    public static class MyPoolableObjectFactory extends BasePoolableObjectFactory<MyPooledObject> {
        @Override
        public MyPooledObject makeObject() throws Exception {
            return new MyPooledObject();
        }

        @Override
        public void destroyObject(MyPooledObject obj) throws Exception {
            obj.destroy();
        }
        // PoolableObjectFactory has other methods you can override
        // to valdiate, activate, and passivate objects.
    }

    public static void main(String[] args) throws Exception {
        PoolableObjectFactory<MyPooledObject> factory = new MyPoolableObjectFactory();
        ObjectPool<MyPooledObject> pool = new GenericObjectPool<MyPooledObject>(factory);

        // Other ObjectPool implementations with special behaviors are available;
        // see the JavaDoc for details

        try {
            for (int i = 0; i < 2; i++) {
                MyPooledObject obj;

                try {
                    obj = pool.borrowObject();
                } catch (Exception e) {
                    // failed to borrow object; you get to decide how to handle this
                    throw e;
                }

                try {
                    // use the pooled object
                    obj.sing();

                } catch (Exception e) {
                    // this object has failed us -- never use it again!
                    pool.invalidateObject(obj);
                    obj = null; // don't return it to the pool

                    // now handle the exception however you want

                } finally {
                    if (obj != null) {
                        pool.returnObject(obj);
                    }
                }
            }
        } finally {
            pool.close();
        }
    }
}
于 2012-04-28T23:57:07.043 回答