Example usage for org.apache.commons.pool2 PooledObject getObject

List of usage examples for org.apache.commons.pool2 PooledObject getObject

Introduction

In this page you can find the example usage for org.apache.commons.pool2 PooledObject getObject.

Prototype

T getObject();

Source Link

Document

Obtain the underlying object that is wrapped by this instance of PooledObject .

Usage

From source file:org.springframework.ldap.pool2.factory.DirContextPooledObjectFactoryTest.java

@Test
public void testMakeObjectReadWrite() throws Exception {
    final DirContextPooledObjectFactory objectFactory = new DirContextPooledObjectFactory();

    DirContext readWriteContextMock = mock(DirContext.class);

    when(contextSourceMock.getReadWriteContext()).thenReturn(readWriteContextMock);
    objectFactory.setContextSource(contextSourceMock);

    final PooledObject createdDirContext = objectFactory.makeObject(DirContextType.READ_WRITE);

    InvocationHandler invocationHandler = Proxy.getInvocationHandler(createdDirContext.getObject());
    assertThat(readWriteContextMock).isEqualTo(Whitebox.getInternalState(invocationHandler, "target"));
}

From source file:org.wso2.andes.thrift.ThriftClientFactory.java

/**
 * When an object is invalidated this method is called. We have to properly close the sockets before removing the
 * connection./*from   w w  w  . j a v a  2 s  .  c  o  m*/
 *
 * @param client The thrift client which should be destroyed
 * @throws Exception Throws when the thrift connection could not be closed
 */
@Override
public void destroyObject(PooledObject<SlotManagementService.Client> client) throws Exception {
    client.getObject().getInputProtocol().getTransport().close();
    super.destroyObject(client);
}

From source file:org.wso2.andes.thrift.ThriftClientFactory.java

/**
 * When setTestOnBorrow is set to true in thrift connection pool. This validation method is called on each
 * connection object taken from the pool. This method do a bogus thrift call and validate the connection is
 * usable for next intended task.//from   www .  j a v  a  2 s . c om
 *
 * @param client client
 * @return connection's usability
 */
@Override
public boolean validateObject(PooledObject<SlotManagementService.Client> client) {
    try {
        return client.getObject().healthCheck();
    } catch (TException e) {
        if (log.isDebugEnabled()) {
            log.debug("Connection is not active. Hence removing the connection from pool.", e);
        }
    }
    return false;
}

From source file:org.wso2.carbon.transport.jms.factory.PooledJMSConnectionFactory.java

/**
 * This is called when an connection object is invalidated.
 *
 * @param key                    The connection key
 * @param pooledConnectionObject The invalidated wrapped connection object.
 * @throws Exception Any exception thrown when closing a JMS connection will be thrown
 *///from www  .  j a  v  a  2 s .  co m
@Override
public void destroyObject(PooledConnectionKey key, PooledObject<Connection> pooledConnectionObject)
        throws Exception {
    Connection connection = pooledConnectionObject.getObject();

    connectionKeyMap.remove(connection);
    connection.close();
}

From source file:org.wso2.carbon.transport.jms.factory.PooledJMSConnectionFactory.java

/**
 * Validates a connection object and verifies it can be used further.
 *
 * @param key                    The connection key
 * @param pooledConnectionObject The wrapped connection object
 * @return True if the object is valid//from w  w  w . j a  v  a  2 s. com
 */
@Override
public boolean validateObject(PooledConnectionKey key, PooledObject<Connection> pooledConnectionObject) {

    boolean valid = false;

    try {
        Session session = pooledConnectionObject.getObject().createSession(false, Session.AUTO_ACKNOWLEDGE);

        session.close();

        valid = true;
    } catch (JMSException e) {
        log.warn("Connection with key " + key.hashCode() + " is not valid anymore");
    }

    return valid;
}

From source file:org.wso2.carbon.transport.jms.factory.PooledJMSConnectionFactory.java

/**
 * Activates a suspended connection.//w  ww  .ja  v a  2 s . c o m
 *
 * @param key                    The connection key
 * @param pooledConnectionObject Wrapped suspended connection object.
 * @throws Exception Any exception thrown when starting a JMS connection will be thrown
 */
@Override
public void activateObject(PooledConnectionKey key, PooledObject<Connection> pooledConnectionObject)
        throws Exception {
    pooledConnectionObject.getObject().start();
}

From source file:org.wso2.carbon.transport.jms.factory.PooledJMSConnectionFactory.java

/**
 * Suspend a connection./* ww  w .j av a  2 s  . c  o  m*/
 *
 * @param key                    The connection key
 * @param pooledConnectionObject The wrapped connection object that needs to be suspended.
 * @throws Exception Any exception thrown when stopping a JMS connection will be thrown
 */
@Override
public void passivateObject(PooledConnectionKey key, PooledObject<Connection> pooledConnectionObject)
        throws Exception {
    pooledConnectionObject.getObject().stop();
}

From source file:org.wso2.carbon.transport.jms.sender.sessionpool.SessionPoolFactory.java

@Override
public void destroyObject(PooledObject<SessionWrapper> sessionWrapper) throws Exception {
    try {/*  w w  w  . j a  v  a 2 s  .  c  o  m*/
        sessionWrapper.getObject().getMessageProducer().close();
        sessionWrapper.getObject().getSession().close();
    } catch (JMSException e) {
        throw new JMSConnectorException("Error when closing the JMS session/producer", e);
    }
}

From source file:redis.client.jedis.CustomShardedJedisFactory.java

/**
 * ?{@link PooledObject<ShardedJedis>}
 * <p>//from  w  w w  .  j av  a 2  s  .  c o m
 * {@inheritDoc}
 */
@Override
public void destroyObject(PooledObject<ShardedJedis> pooledShardedJedis) throws Exception {
    final ShardedJedis shardedJedis = pooledShardedJedis.getObject();

    // shardedJedis.disconnect(); // "?"
    for (Jedis jedis : shardedJedis.getAllShards()) {
        try {
            // 1. ?
            jedis.quit();
        } catch (Exception e) {
            // ignore the exception node, so that all other normal nodes can release all connections.

            // java.lang.ClassCastException: java.lang.Long cannot be cast to [B
            // (zadd/zcard  long  quit  string ?)
            logger.warn("quit jedis connection for server fail: " + toServerString(jedis), e);
        }

        try {
            // 2. 
            jedis.disconnect();
        } catch (Exception e) {
            // ignore the exception node, so that all other normal nodes can release all connections.

            logger.warn("disconnect jedis connection fail: " + toServerString(jedis), e);
        }
    }
}

From source file:redis.client.jedis.CustomShardedJedisFactory.java

/**
 * {@link ShardedJedis}Jedis?/*from w  w w. j ava 2  s.co  m*/
 * <p>
 * <font color="red">'PING'???"Jedis"?</font>
 * <p>
 * {@inheritDoc}
 */
@Override
public boolean validateObject(PooledObject<ShardedJedis> pooledShardedJedis) {
    final ShardedJedis shardedJedis = pooledShardedJedis.getObject();
    // "Sharded.getAllShardInfo() returns 160*shards info list not returns the original shards list"
    // https://github.com/xetorthio/jedis/issues/837
    Collection<JedisShardInfo> allClusterShardInfos = shardedJedis.getAllShardInfo(); // ?160??ShardedJedisTest.getAllShardInfo()
    // ??Shard?
    Set<JedisShardInfo> checkedShards = new HashSet<JedisShardInfo>(originalShardListSize);
    checkedShards.addAll(allClusterShardInfos);
    logger.debug("Active Shard list for current validated sharded Jedis: {}", checkedShards);

    // "?"
    if (serverStateCheckTimerTask.isActiveShardListUpdated()) {
        shards = new ArrayList<JedisShardInfo>(serverStateCheckTimerTask.getAllActiveJedisShards());
        logger.debug("Active Shard list after updated: {}", shards);
    }

    if (checkedShards.size() != shards.size()) { // ?
        logger.debug("Find a pooled sharded Jedis is updated: {}", checkedShards);
        return false;
    } else { // ????(?????)
        if (!checkedShards.containsAll(shards)) {
            logger.debug("Find a pooled sharded Jedis is updated: {}", checkedShards);
            return false;
        }
    }

    return true;
}