Java Code Examples for org.apache.commons.pool2.PooledObject#getObject()

The following examples show how to use org.apache.commons.pool2.PooledObject#getObject() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ValidatingPoolableLdapConnectionFactory.java    From directory-ldap-api with Apache License 2.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * There is nothing to do to activate a connection.
 */
@Override
public void activateObject( PooledObject<LdapConnection> pooledObject ) throws LdapException
{
    LdapConnection connection = pooledObject.getObject();
    
    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_04146_ACTIVATING, connection ) );
    }
    
    super.activateObject( pooledObject );

    // clear the monitors
    ( ( MonitoringLdapConnection ) connection ).resetMonitors();
}
 
Example 2
Source File: PoolableConnectionFactory.java    From commons-dbcp with Apache License 2.0 6 votes vote down vote up
@Override
public void activateObject(final PooledObject<PoolableConnection> p) throws Exception {

    validateLifetime(p);

    final PoolableConnection conn = p.getObject();
    conn.activate();

    if (defaultAutoCommit != null && conn.getAutoCommit() != defaultAutoCommit.booleanValue()) {
        conn.setAutoCommit(defaultAutoCommit.booleanValue());
    }
    if (defaultTransactionIsolation != UNKNOWN_TRANSACTION_ISOLATION
            && conn.getTransactionIsolation() != defaultTransactionIsolation) {
        conn.setTransactionIsolation(defaultTransactionIsolation);
    }
    if (defaultReadOnly != null && conn.isReadOnly() != defaultReadOnly.booleanValue()) {
        conn.setReadOnly(defaultReadOnly.booleanValue());
    }
    if (defaultCatalog != null && !defaultCatalog.equals(conn.getCatalog())) {
        conn.setCatalog(defaultCatalog);
    }
    if (defaultSchema != null && !defaultSchema.equals(Jdbc41Bridge.getSchema(conn))) {
        Jdbc41Bridge.setSchema(conn, defaultSchema);
    }
    conn.setDefaultQueryTimeout(defaultQueryTimeoutSeconds);
}
 
Example 3
Source File: JenkinsFactory.java    From seppb with MIT License 5 votes vote down vote up
@Override
public boolean validateObject(PooledObject<JenkinsClient> p) {
    JenkinsClient jenkinsClient = p.getObject();
    try {
        return jenkinsClient.isRunning();
    } catch (final Exception e) {
        log.error("jenkins client health check failed", e);
        return false;
    }
}
 
Example 4
Source File: MysqlConnFactory.java    From migration-tool with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<Connection> p) throws Exception {
	if(null != p) {
		Connection conn = p.getObject();
		if(null != conn) {
			conn.close();
		}
	}
}
 
Example 5
Source File: ElasticSearchClientFactory.java    From elasticsearch-pool with Apache License 2.0 5 votes vote down vote up
public boolean validateObject(PooledObject<RestHighLevelClient> pooledObject) {
    RestHighLevelClient client = pooledObject.getObject();
    try {
        return client.ping();
    }catch(Exception e){
        return false;
    }
}
 
Example 6
Source File: ShardedJedisPool.java    From cachecloud with Apache License 2.0 5 votes vote down vote up
@Override
public boolean validateObject(PooledObject<ShardedJedis> pooledShardedJedis) {
  try {
    ShardedJedis jedis = pooledShardedJedis.getObject();
    for (Jedis shard : jedis.getAllShards()) {
      if (!shard.ping().equals("PONG")) {
        return false;
      }
    }
    return true;
  } catch (Exception ex) {
    return false;
  }
}
 
Example 7
Source File: SentryServiceClientPoolFactory.java    From incubator-sentry with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<SentryPolicyServiceClient> pooledObject) {
  SentryPolicyServiceClient client = pooledObject.getObject();
  LOGGER.debug("Destroying Sentry Service Client: " + client);
  if (client != null) {
    // The close() of TSocket or TSaslClientTransport is called actually, and there has no
    // exception even there has some problems, eg, the client is closed already.
    // The close here is just try to close the socket and the client will be destroyed soon.
    client.close();
  }
}
 
Example 8
Source File: ElasticsearchClientFactory.java    From super-cloudops with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<RestHighLevelClient> p) throws Exception {
	if (p.getObject() != null) {
		try {
			if (p.getObject().ping()) {
				p.getObject().close();
			}
		} catch (IOException e) {
			log.debug("es http client close exception:{}", e.getMessage());
		}
	}

}
 
Example 9
Source File: ChannelPooledObjectFactory.java    From brpc-java with Apache License 2.0 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<Channel> p) throws Exception {
    Channel channel = p.getObject();
    if (channel != null && channel.isOpen() && channel.isActive()) {
        channel.close();
    }
    channel = null;
}
 
Example 10
Source File: ProcessFactory.java    From jasperreports with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Override
public void activateObject(PooledObject<PhantomJSProcess> pooledObject) throws Exception
{
	super.activateObject(pooledObject);
	
	PhantomJSProcess process = pooledObject.getObject();
	if (process.hasEnded())
	{
		if (log.isDebugEnabled())
		{
			log.debug(process.getId() + " has ended");
		}
		
		throw new JRRuntimeException("Process " + process.getId() + " has ended");
	}
	
	long borrowedCount = ((DefaultPooledObject<PhantomJSProcess>) pooledObject).getBorrowedCount();
	if (borrowedCount >= expirationCount)
	{
		if (log.isDebugEnabled())
		{
			log.debug(process.getId() + " borrow count " + borrowedCount 
					+ " exceeded expiration count " + expirationCount);
		}
		
		throw new JRRuntimeException("Process " + process.getId() + " borrow count exceeded");
	}
	
	long now = System.currentTimeMillis();
	if (now >= pooledObject.getCreateTime() + expirationTime)
	{
		if (log.isDebugEnabled())
		{
			log.debug(process.getId() + " expiration time " + expirationTime 
					+ " from " + pooledObject.getCreateTime() + " exceeded");
		}
		
		throw new JRRuntimeException("Process " + process.getId() + " expiration time exceeded");
	}
}
 
Example 11
Source File: WorkerFactory.java    From bazel with Apache License 2.0 5 votes vote down vote up
/** The worker is considered to be valid when its files have not changed on disk. */
@Override
public boolean validateObject(WorkerKey key, PooledObject<Worker> p) {
  Worker worker = p.getObject();
  boolean hashMatches =
      key.getWorkerFilesCombinedHash().equals(worker.getWorkerFilesCombinedHash());

  if (workerOptions.workerVerbose && reporter != null && !hashMatches) {
    StringBuilder msg = new StringBuilder();
    msg.append(
        String.format(
            "%s %s (id %d) can no longer be used, because its files have changed on disk:",
            key.getMnemonic(),
            WorkerKey.makeWorkerTypeName(key.getProxied()),
            worker.getWorkerId()));
    TreeSet<PathFragment> files = new TreeSet<>();
    files.addAll(key.getWorkerFilesWithHashes().keySet());
    files.addAll(worker.getWorkerFilesWithHashes().keySet());
    for (PathFragment file : files) {
      HashCode oldHash = worker.getWorkerFilesWithHashes().get(file);
      HashCode newHash = key.getWorkerFilesWithHashes().get(file);
      if (!oldHash.equals(newHash)) {
        msg.append("\n")
            .append(file.getPathString())
            .append(": ")
            .append(oldHash != null ? oldHash : "<none>")
            .append(" -> ")
            .append(newHash != null ? newHash : "<none>");
      }
    }

    reporter.handle(Event.warn(msg.toString()));
  }

  return hashMatches;
}
 
Example 12
Source File: ValidatingPoolableLdapConnectionFactory.java    From directory-ldap-api with Apache License 2.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * Here, passivating a connection means we re-bind it, so that the existing LDAPSession
 * is reset.
 * 
 * @throws LdapException If unable to reconfigure and rebind.
 */
@Override
public void passivateObject( PooledObject<LdapConnection> pooledObject ) throws LdapException
{
    LdapConnection connection = pooledObject.getObject();

    if ( LOG.isDebugEnabled() )
    {
        LOG.debug( I18n.msg( I18n.MSG_04151_PASSIVATING, connection ) );
    }

    if ( !connection.isConnected() || !connection.isAuthenticated()
        || ( ( MonitoringLdapConnection ) connection ).bindCalled() )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.msg( I18n.MSG_04172_REBIND_BIND_CONNECTION, connection ) );
        }
        
        connectionFactory.bindConnection( connection );
    }
    
    if ( ( ( MonitoringLdapConnection ) connection ).startTlsCalled() )
    {
        if ( LOG.isDebugEnabled() )
        {
            LOG.debug( I18n.msg( I18n.MSG_04173_UNBIND_START_TLS, connection ) );
        }
        
        // unbind to clear the tls
        connection.unBind();
        connectionFactory.bindConnection( connection );
    }

    // in case connection had configuration changed
    connectionFactory.configureConnection( connection );
}
 
Example 13
Source File: BytesFactory.java    From x-pipe with Apache License 2.0 5 votes vote down vote up
@Override
public void activateObject(PooledObject<byte[]> p) throws Exception {
	byte[]data = p.getObject();
	for(int i=0; i < activeLength;i++){
		data[i] = 'c';
	}
}
 
Example 14
Source File: PooledConnectionFactory.java    From seppb with MIT License 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<TrackerServer> pooledObject) throws IOException {
    TrackerServer trackerServer = pooledObject.getObject();
    if (trackerServer != null) {
        trackerServer.close();
    }
}
 
Example 15
Source File: JenkinsFactory.java    From seppb with MIT License 5 votes vote down vote up
@Override
public void destroyObject(PooledObject<JenkinsClient> p) throws Exception {
    JenkinsClient jenkinsClient = p.getObject();
    if (jenkinsClient.isRunning()) {
        try {
            jenkinsClient.close();
        } catch (Exception e) {
            log.error("", e);
        }
    }
}
 
Example 16
Source File: PooledCloseableHttpClientFactory.java    From timely with Apache License 2.0 4 votes vote down vote up
@Override
public boolean validateObject(PooledObject<CloseableHttpClient> pooledObject) {
    return pooledObject.getObject() != null;
}
 
Example 17
Source File: EsClientPoolFactory.java    From dk-fitting with Apache License 2.0 4 votes vote down vote up
public void destroyObject(PooledObject<TransportClient> pooledObject) throws Exception {
    TransportClient client = pooledObject.getObject();
    client.close();
}
 
Example 18
Source File: SvnObjectPools.java    From proctor with Apache License 2.0 4 votes vote down vote up
@Override
public void destroyObject(final PooledObject<SVNClientManager> p) throws Exception {
    final SVNClientManager m = p.getObject();
    m.dispose();
}
 
Example 19
Source File: RabbitMQClient.java    From jweb-cms with GNU Affero General Public License v3.0 4 votes vote down vote up
@Override
public void destroyObject(PooledObject<Channel> p) throws Exception {
    Channel channel = p.getObject();
    channel.close();
}
 
Example 20
Source File: WebDriverPooledFactory.java    From NetDiscovery with Apache License 2.0 2 votes vote down vote up
/**
 * 对象是否有效
 * @param p
 * @return
 */
@Override
public boolean validateObject(PooledObject<WebDriver> p) {
    return null != p.getObject();
}