Package org.apache.commons.pool

Examples of org.apache.commons.pool.ObjectPool


   
    /**
     * @deprecated - to be removed in pool 2.0
     */
    public void testCanResetFactoryWithoutActiveObjects() throws Exception {
        ObjectPool pool = new StackObjectPool();
        {
            pool.setFactory(new SimpleFactory());
            Object obj = pool.borrowObject();       
            assertNotNull(obj);
            pool.returnObject(obj);
        }
        {
            pool.setFactory(new SimpleFactory());
            Object obj = pool.borrowObject();       
            assertNotNull(obj);
            pool.returnObject(obj);
        }
    }
View Full Code Here


     */
    public void testBorrowWithSometimesInvalidObjects() throws Exception {
        SelectiveFactory factory = new SelectiveFactory();
        factory.setValidateSelectively(true)// Even numbers fail validation
        factory.setPassivateSelectively(true); // Multiples of 3 fail passivation
        ObjectPool pool = new StackObjectPool(factory, 20);
        Object[] obj = new Object[10];
        for(int i=0;i<10;i++) {
            Object object = null;
            int k = 0;
            while (object == null && k < 100) { // bound not really needed
                try {
                    k++;
                    object = pool.borrowObject();
                    if (((Integer) object).intValue() % 2 == 0) {
                        fail("Expecting NoSuchElementException");
                    } else {
                        obj[i] = object;
                    }
                } catch (NoSuchElementException ex) {
                    // Should fail for evens
                }
            }
            assertEquals("Each time we borrow, get one more active.", i+1, pool.getNumActive());
        }
        // 1,3,5,...,19 pass validation, get checked out
        for(int i=0;i<10;i++) {
            pool.returnObject(obj[i]);
            assertEquals("Each time we return, get one less active.", 9-i, pool.getNumActive());
        }
        // 3, 9, 15 fail passivation. 
        assertEquals(7,pool.getNumIdle());
        assertEquals(new Integer(19), pool.borrowObject());
        assertEquals(new Integer(17), pool.borrowObject());
        assertEquals(new Integer(13), pool.borrowObject());
        assertEquals(new Integer(11), pool.borrowObject());
        assertEquals(new Integer(7), pool.borrowObject());
        assertEquals(new Integer(5), pool.borrowObject());
        assertEquals(new Integer(1), pool.borrowObject());    
    }
View Full Code Here

    {
        int numIdle = 0;
        try
        {
            PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( DRIVER_NAME );
            ObjectPool connectionPool = driver.getConnectionPool( this.getPoolName() );

            if ( log.isDebugEnabled() )
            {
                log.debug( connectionPool );
            }
            numIdle = connectionPool.getNumIdle();
        }
        catch ( Exception e )
        {
            log.error( e );
        }
View Full Code Here

    {
        int numActive = 0;
        try
        {
            PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( DRIVER_NAME );
            ObjectPool connectionPool = driver.getConnectionPool( this.getPoolName() );

            if ( log.isDebugEnabled() )
            {
                log.debug( connectionPool );
            }
            numActive = connectionPool.getNumActive();
        }
        catch ( Exception e )
        {
            log.error( e );
        }
View Full Code Here

    {
        // First, we'll need a ObjectPool that serves as the
        // actual pool of connections.
        // We'll use a GenericObjectPool instance, although
        // any ObjectPool implementation will suffice.
        ObjectPool connectionPool = new GenericObjectPool( null, maxActive );

        // TODO make configurable
        // By dfault the size is 8!!!!!!!
        ( (GenericObjectPool) connectionPool ).setMaxIdle( -1 );
View Full Code Here

     */
    public void logDriverStats()
        throws Exception
    {
        PoolingDriver driver = (PoolingDriver) DriverManager.getDriver( DRIVER_NAME );
        ObjectPool connectionPool = driver.getConnectionPool( this.getPoolName() );

        if ( connectionPool != null )
        {
            if ( log.isDebugEnabled() )
            {
View Full Code Here

        // actual pool of connections.
        //
        // We'll use a GenericObjectPool instance, although
        // any ObjectPool implementation will suffice.
        //
        ObjectPool connectionPool = new GenericObjectPool(null);

        //
        // Next, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        // We'll use the DriverManagerConnectionFactory,
View Full Code Here

        // actual pool of connections.
        //
        // We'll use a GenericObjectPool instance, although
        // any ObjectPool implementation will suffice.
        //
        ObjectPool connectionPool = new GenericObjectPool(null);

        //
        // Next, we'll create a ConnectionFactory that the
        // pool will use to create Connections.
        // We'll use the DriverManagerConnectionFactory,
View Full Code Here

   * Returns a ServerSession from the pool, creating a new pool for the given
   * session manager if necessary.
   * @see #createObjectPool
   */
  public ServerSession getServerSession(ListenerSessionManager sessionManager) throws JMSException {
    ObjectPool pool = null;
    synchronized (this.serverSessionPools) {
      pool = (ObjectPool) this.serverSessionPools.get(sessionManager);
      if (pool == null) {
        if (logger.isInfoEnabled()) {
          logger.info("Creating Commons ServerSession pool for: " + sessionManager);
        }
        pool = createObjectPool(sessionManager);
        this.serverSessionPools.put(sessionManager, pool);
      }
    }
    try {
      return (ServerSession) pool.borrowObject();
    }
    catch (Exception ex) {
      JMSException jmsEx = new JMSException("Failed to borrow ServerSession from pool");
      jmsEx.setLinkedException(ex);
      throw jmsEx;
View Full Code Here

  /**
   * Returns the given ServerSession, which just finished an execution
   * of its listener, back to the pool.
   */
  protected void serverSessionFinished(ServerSession serverSession, ListenerSessionManager sessionManager) {
    ObjectPool pool = (ObjectPool) this.serverSessionPools.get(sessionManager);
    try {
      pool.returnObject(serverSession);
    }
    catch (Exception ex) {
      logger.error("Failed to return ServerSession to pool", ex);
    }
  }
View Full Code Here

TOP

Related Classes of org.apache.commons.pool.ObjectPool

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.