Example usage for org.apache.commons.pool PoolableObjectFactory PoolableObjectFactory

List of usage examples for org.apache.commons.pool PoolableObjectFactory PoolableObjectFactory

Introduction

In this page you can find the example usage for org.apache.commons.pool PoolableObjectFactory PoolableObjectFactory.

Prototype

PoolableObjectFactory

Source Link

Usage

From source file:TestSoftRef.java

 public void setUp() throws Exception {
   this.pool = new SoftReferenceObjectPool(new PoolableObjectFactory() {
       int counter;
       public Object makeObject()                   { return String.valueOf(counter++); }
       public void destroyObject( Object obj )      {}
       public boolean validateObject( Object obj )  { return true; }
       public void activateObject( Object obj )     {}
       public void passivateObject( Object obj )    {}
   }, 5);//from w  w w.j a  v a 2  s  . c  om
}

From source file:net.sf.hajdbc.pool.generic.GenericObjectPoolFactory.java

@Override
public <T, E extends Exception> Pool<T, E> createPool(final PoolProvider<T, E> provider) {
    PoolableObjectFactory<T> factory = new PoolableObjectFactory<T>() {
        @Override/* www.  j av a  2s.  c om*/
        public void destroyObject(T object) {
            provider.close(object);
        }

        @Override
        public T makeObject() throws Exception {
            return provider.create();
        }

        @Override
        public boolean validateObject(T object) {
            return provider.isValid(object);
        }

        @Override
        public void activateObject(T object) {
        }

        @Override
        public void passivateObject(T object) {
        }
    };

    final ObjectPool<T> pool = new GenericObjectPool<T>(factory, this.config);

    return new Pool<T, E>() {
        @Override
        public void close() {
            try {
                pool.close();
            } catch (Exception e) {
                logger.log(Level.WARN, e, e.getMessage());
            }
        }

        @Override
        public void release(T item) {
            try {
                pool.returnObject(item);
            } catch (Exception e) {
                logger.log(Level.WARN, e, e.getMessage());
            }
        }

        @Override
        public T take() throws E {
            try {
                return pool.borrowObject();
            } catch (NoSuchElementException e) {
                return provider.create();
            } catch (IllegalStateException e) {
                throw e;
            } catch (Exception e) {
                throw provider.getExceptionClass().cast(e);
            }
        }
    };
}

From source file:org.axonframework.commandhandling.distributed.websockets.WebsocketCommandBusConnectorClient.java

public WebsocketCommandBusConnectorClient(ClientSessionFactory clientSessionFactory, int sessionCount) {
    //Create the pool. Server side connections are bound to the commands in process. Therefore scaling down the
    //amount of connections results in losing callbacks of pending commands. We will therefore never scale down or
    //invalidate connections.
    GenericObjectPool.Config config = new GenericObjectPool.Config();
    config.maxActive = sessionCount;//www  .  ja v  a2s.c  om
    config.maxIdle = sessionCount;
    config.maxWait = -1;
    config.minEvictableIdleTimeMillis = -1;
    config.minIdle = 0;
    config.numTestsPerEvictionRun = 0;
    config.softMinEvictableIdleTimeMillis = -1;
    config.testOnBorrow = true;
    config.testOnReturn = false;
    config.testWhileIdle = false;
    config.timeBetweenEvictionRunsMillis = -1;
    config.whenExhaustedAction = GenericObjectPool.WHEN_EXHAUSTED_BLOCK;

    sessions = new GenericObjectPoolFactory<>(new PoolableObjectFactory<Session>() {
        @Override
        public Session makeObject() throws Exception {
            return clientSessionFactory.createSession(WebsocketCommandBusConnectorClient.this);
        }

        @Override
        public void destroyObject(Session obj) throws Exception {
            if (obj.isOpen())
                obj.close();
        }

        @Override
        public boolean validateObject(Session obj) {
            return obj.isOpen();
        }

        @Override
        public void activateObject(Session obj) throws Exception {
            //
        }

        @Override
        public void passivateObject(Session obj) throws Exception {
            //
        }
    }, config).createPool();
}

From source file:org.ros.internal.message.MessageBufferPool.java

public MessageBufferPool() {
    pool = new StackObjectPool<ChannelBuffer>(new PoolableObjectFactory<ChannelBuffer>() {
        @Override// ww w  . j a  v  a  2  s.co  m
        public ChannelBuffer makeObject() throws Exception {
            return MessageBuffers.dynamicBuffer();
        }

        @Override
        public void destroyObject(ChannelBuffer channelBuffer) throws Exception {
        }

        @Override
        public boolean validateObject(ChannelBuffer channelBuffer) {
            return true;
        }

        @Override
        public void activateObject(ChannelBuffer channelBuffer) throws Exception {
        }

        @Override
        public void passivateObject(ChannelBuffer channelBuffer) throws Exception {
            channelBuffer.clear();
        }
    });
}

From source file:org.springframework.jms.listener.serversession.CommonsPoolServerSessionFactory.java

/**
 * Create a Commons PoolableObjectFactory adapter for the given session manager.
 * Calls <code>createServerSession</code> and <code>destroyServerSession</code>
 * as defined by the AbstractPoolingServerSessionFactory class.
 * @param sessionManager the session manager to use for
 * creating and executing new listener sessions
 * @return the Commons PoolableObjectFactory
 * @see #createServerSession//from w w  w .ja va 2s.  c o  m
 * @see #destroyServerSession
 */
protected PoolableObjectFactory createPoolableObjectFactory(final ListenerSessionManager sessionManager) {
    return new PoolableObjectFactory() {
        public Object makeObject() throws JMSException {
            return createServerSession(sessionManager);
        }

        public void destroyObject(Object obj) {
            destroyServerSession((ServerSession) obj);
        }

        public boolean validateObject(Object obj) {
            return true;
        }

        public void activateObject(Object obj) {
        }

        public void passivateObject(Object obj) {
        }
    };
}