org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig Java Examples

The following examples show how to use org.apache.commons.pool2.impl.GenericKeyedObjectPoolConfig. 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: AbstractScripting.java    From cuba with Apache License 2.0 6 votes vote down vote up
private synchronized GenericKeyedObjectPool<String, Script> getPool() {
    if (pool == null) {
        GenericKeyedObjectPoolConfig<Script> poolConfig = new GenericKeyedObjectPoolConfig<>();
        poolConfig.setMaxTotalPerKey(-1);
        poolConfig.setMaxIdlePerKey(globalConfig.getGroovyEvaluationPoolMaxIdle());
        pool = new GenericKeyedObjectPool<>(
                new BaseKeyedPooledObjectFactory<String, Script>() {
                    @Override
                    public Script create(String key) {
                        return createScript(key);
                    }

                    @Override
                    public PooledObject<Script> wrap(Script value) {
                        return new DefaultPooledObject<>(value);
                    }
                },
                poolConfig
        );
    }
    return pool;
}
 
Example #2
Source File: ConnectionManagerTest.java    From fastdfs-java-client with Apache License 2.0 6 votes vote down vote up
@Test
public void test() {
    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(500, 500);

    GenericKeyedObjectPoolConfig conf = new GenericKeyedObjectPoolConfig();
    conf.setMaxTotal(20);
    ConnectionPool connectionPool = new ConnectionPool(pooledConnectionFactory, conf);

    Set<String> trackerSet = new HashSet<String>();
    trackerSet.add("192.168.10.128:22122");

    DefaultCommandExecutor connectionManager = new DefaultCommandExecutor(trackerSet, connectionPool);

    connectionManager.dumpPoolInfo();

    GetStorageNodeCommand command = new GetStorageNodeCommand();
    StorageNode storageNode = connectionManager.execute(command);
    logger.info(storageNode.toString());

    connectionManager.dumpPoolInfo();

    connectionPool.close();
}
 
Example #3
Source File: BaseTestProxiedKeyedObjectPool.java    From commons-pool with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() {
    final GenericKeyedObjectPoolConfig<TestObject> config = new GenericKeyedObjectPoolConfig<>();
    config.setMaxTotal(3);

    final KeyedPooledObjectFactory<String, TestObject> factory =
            new TestKeyedObjectFactory();

    @SuppressWarnings("resource")
    final KeyedObjectPool<String, TestObject> innerPool =
            new GenericKeyedObjectPool<>(
                    factory, config);

    pool = new ProxiedKeyedObjectPool<>(innerPool, getproxySource());
}
 
Example #4
Source File: DBCPPool.java    From MtgDesktopCompanion with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void initDefault() {
	setProperty("defaultAutoCommit",TRUE);
	setProperty("cacheState",TRUE);
	setProperty("lifo",String.valueOf(BaseObjectPoolConfig.DEFAULT_LIFO));
	setProperty("maxTotal",String.valueOf(GenericObjectPoolConfig.DEFAULT_MAX_TOTAL));
	setProperty("maxIdle",String.valueOf(GenericObjectPoolConfig.DEFAULT_MAX_IDLE));
	setProperty("minIdle",String.valueOf(GenericObjectPoolConfig.DEFAULT_MIN_IDLE));
	setProperty("initialSize","3");
	setProperty("maxWaitMILLIS",String.valueOf(BaseObjectPoolConfig.DEFAULT_MAX_WAIT_MILLIS));
	setProperty("poolPreparedStatements",FALSE);
	setProperty("maxOpenPreparedStatements",String.valueOf(GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL));
	setProperty("timeBetweenEvictionRunsMILLIS",String.valueOf(BaseObjectPoolConfig.DEFAULT_TIME_BETWEEN_EVICTION_RUNS_MILLIS));
	setProperty("numTestsPerEvictionRun",String.valueOf(BaseObjectPoolConfig.DEFAULT_NUM_TESTS_PER_EVICTION_RUN));
	setProperty("minEvictableIdleTimeMILLIS",String.valueOf(BaseObjectPoolConfig.DEFAULT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
	setProperty("softMinEvictableIdleTimeMILLIS",String.valueOf(BaseObjectPoolConfig.DEFAULT_SOFT_MIN_EVICTABLE_IDLE_TIME_MILLIS));
	setProperty("evictionPolicyClassName",String.valueOf(BaseObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME));
	setProperty("testWhileIdle",FALSE);
	setProperty("validationQuery","");
	setProperty("validationQueryTimeoutSeconds","-1");
	setProperty("accessToUnderlyingConnectionAllowed",FALSE);
	setProperty("maxConnLifetimeMILLIS","-1");
	setProperty("logExpiredConnections",TRUE);
	setProperty("jmxName","org.magic.api:type=Pool,name="+getName());
	setProperty("autoCommitOnReturn",TRUE);
	setProperty("rollbackOnReturn",TRUE);
	setProperty("defaultAutoCommit",TRUE);
	setProperty("defaultReadOnly",FALSE);
	setProperty("defaultQueryTimeoutSeconds","");
	setProperty("cacheState",TRUE);
	setProperty("testOnCreate",FALSE);
	setProperty("testOnBorrow",TRUE);
	setProperty("testOnReturn",FALSE);
	setProperty("fastFailValidation",FALSE);
}
 
Example #5
Source File: PoolConfiguration.java    From spring-thrift-starter with MIT License 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(name = "thriftClientsPool")
public KeyedObjectPool<ThriftClientKey, TServiceClient> thriftClientsPool() {
    GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    poolConfig.setMaxTotal(maxTotalThreads);
    poolConfig.setMaxIdlePerKey(maxIdleThreads);
    poolConfig.setMaxTotalPerKey(maxThreads);
    poolConfig.setJmxEnabled(false); //cause spring will autodetect itself
    return new ThriftClientPool(thriftClientPoolFactory(), poolConfig);
}
 
Example #6
Source File: QueryService.java    From kylin with Apache License 2.0 5 votes vote down vote up
private GenericKeyedObjectPool<PreparedContextKey, PreparedContext> createPreparedContextPool() {
    PreparedContextFactory factory = new PreparedContextFactory();
    KylinConfig kylinConfig = getConfig();
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setMaxTotalPerKey(kylinConfig.getQueryMaxCacheStatementInstancePerKey());
    config.setMaxTotal(kylinConfig.getQueryMaxCacheStatementNum());
    config.setBlockWhenExhausted(false);
    config.setMinEvictableIdleTimeMillis(10 * 60 * 1000L); // cached statement will be evict if idle for 10 minutes
    config.setTimeBetweenEvictionRunsMillis(60 * 1000L); 
    GenericKeyedObjectPool<PreparedContextKey, PreparedContext> pool = new GenericKeyedObjectPool<>(factory,
            config);
    return pool;
}
 
Example #7
Source File: QueryService.java    From kylin-on-parquet-v2 with Apache License 2.0 5 votes vote down vote up
private GenericKeyedObjectPool<PreparedContextKey, PreparedContext> createPreparedContextPool() {
    PreparedContextFactory factory = new PreparedContextFactory();
    KylinConfig kylinConfig = getConfig();
    GenericKeyedObjectPoolConfig config = new GenericKeyedObjectPoolConfig();
    config.setMaxTotalPerKey(kylinConfig.getQueryMaxCacheStatementInstancePerKey());
    config.setMaxTotal(kylinConfig.getQueryMaxCacheStatementNum());
    config.setBlockWhenExhausted(false);
    config.setMinEvictableIdleTimeMillis(10 * 60 * 1000L); // cached statement will be evict if idle for 10 minutes
    config.setTimeBetweenEvictionRunsMillis(60 * 1000L); 
    GenericKeyedObjectPool<PreparedContextKey, PreparedContext> pool = new GenericKeyedObjectPool<>(factory,
            config);
    return pool;
}
 
Example #8
Source File: DefaultStorageClientTest.java    From fastdfs-java-client with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(500, 500);
    GenericKeyedObjectPoolConfig conf = new GenericKeyedObjectPoolConfig();
    conf.setMaxTotal(200);
    conf.setMaxTotalPerKey(200);
    conf.setMaxIdlePerKey(100);
    connectionPool = new ConnectionPool(pooledConnectionFactory, conf);
    Set<String> trackerSet = new HashSet<String>();
    trackerSet.add("192.168.10.128:22122");
    DefaultCommandExecutor commandExecutor = new DefaultCommandExecutor(trackerSet, connectionPool);
    TrackerClient trackerClient = new DefaultTrackerClient(commandExecutor);
    storageClient = new DefaultStorageClient(commandExecutor, trackerClient);
}
 
Example #9
Source File: DefaultTrackerClientTest.java    From fastdfs-java-client with Apache License 2.0 5 votes vote down vote up
@Before
public void init() {
    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(500, 500);
    GenericKeyedObjectPoolConfig conf = new GenericKeyedObjectPoolConfig();
    conf.setMaxTotal(200);
    conf.setMaxTotalPerKey(200);
    conf.setMaxIdlePerKey(100);
    connectionPool = new ConnectionPool(pooledConnectionFactory, conf);
    Set<String> trackerSet = new HashSet<String>();
    trackerSet.add("192.168.10.128:22122");
    DefaultCommandExecutor commandExecutor = new DefaultCommandExecutor(trackerSet, connectionPool);
    trackerClient = new DefaultTrackerClient(commandExecutor);
}
 
Example #10
Source File: ConnectionManagerTest.java    From fastdfs-java-client with Apache License 2.0 5 votes vote down vote up
@Test
public void test03() throws InterruptedException {
    PooledConnectionFactory pooledConnectionFactory = new PooledConnectionFactory(5000, 5000);

    GenericKeyedObjectPoolConfig conf = new GenericKeyedObjectPoolConfig();
    conf.setMaxTotal(200);
    conf.setMaxTotalPerKey(200);
    conf.setMaxIdlePerKey(100);
    ConnectionPool connectionPool = new ConnectionPool(pooledConnectionFactory, conf);

    Set<String> trackerSet = new HashSet<String>();
    trackerSet.add("192.168.10.128:22122");

    DefaultCommandExecutor connectionManager = new DefaultCommandExecutor(trackerSet, connectionPool);

    for (int i = 0; i <= 50; i++) {
        Thread thread = new PoolTest(connectionManager);
        thread.start();
    }

    for (int i = 0; i <= 2; i++) {
        connectionManager.dumpPoolInfo();
        Thread.sleep(1000 * 2);
    }

    connectionPool.close();
}
 
Example #11
Source File: SharedPoolDataSource.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
private void registerPool(final String userName, final String password) throws NamingException, SQLException {

        final ConnectionPoolDataSource cpds = testCPDS(userName, password);

        // Create an object pool to contain our PooledConnections
        factory = new KeyedCPDSConnectionFactory(cpds, getValidationQuery(), getValidationQueryTimeout(),
                isRollbackAfterValidation());
        factory.setMaxConnLifetimeMillis(getMaxConnLifetimeMillis());

        final GenericKeyedObjectPoolConfig<PooledConnectionAndInfo> config = new GenericKeyedObjectPoolConfig<>();
        config.setBlockWhenExhausted(getDefaultBlockWhenExhausted());
        config.setEvictionPolicyClassName(getDefaultEvictionPolicyClassName());
        config.setLifo(getDefaultLifo());
        config.setMaxIdlePerKey(getDefaultMaxIdle());
        config.setMaxTotal(getMaxTotal());
        config.setMaxTotalPerKey(getDefaultMaxTotal());
        config.setMaxWaitMillis(getDefaultMaxWaitMillis());
        config.setMinEvictableIdleTimeMillis(getDefaultMinEvictableIdleTimeMillis());
        config.setMinIdlePerKey(getDefaultMinIdle());
        config.setNumTestsPerEvictionRun(getDefaultNumTestsPerEvictionRun());
        config.setSoftMinEvictableIdleTimeMillis(getDefaultSoftMinEvictableIdleTimeMillis());
        config.setTestOnCreate(getDefaultTestOnCreate());
        config.setTestOnBorrow(getDefaultTestOnBorrow());
        config.setTestOnReturn(getDefaultTestOnReturn());
        config.setTestWhileIdle(getDefaultTestWhileIdle());
        config.setTimeBetweenEvictionRunsMillis(getDefaultTimeBetweenEvictionRunsMillis());

        final KeyedObjectPool<UserPassKey, PooledConnectionAndInfo> tmpPool = new GenericKeyedObjectPool<>(factory,
                config);
        factory.setPool(tmpPool);
        pool = tmpPool;
    }
 
Example #12
Source File: PoolableManagedConnectionFactory.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
/**
 * Uses the configured XAConnectionFactory to create a {@link PoolableManagedConnection}. Throws
 * <code>IllegalStateException</code> if the connection factory returns null. Also initializes the connection using
 * configured initialization SQL (if provided) and sets up a prepared statement pool associated with the
 * PoolableManagedConnection if statement pooling is enabled.
 */
@Override
public synchronized PooledObject<PoolableConnection> makeObject() throws Exception {
    Connection conn = getConnectionFactory().createConnection();
    if (conn == null) {
        throw new IllegalStateException("Connection factory returned null from createConnection");
    }
    initializeConnection(conn);
    if (getPoolStatements()) {
        conn = new PoolingConnection(conn);
        final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(-1);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(1);
        config.setMaxTotal(getMaxOpenPreparedStatements());
        final ObjectName dataSourceJmxName = getDataSourceJmxName();
        final long connIndex = getConnectionIndex().getAndIncrement();
        if (dataSourceJmxName != null) {
            final StringBuilder base = new StringBuilder(dataSourceJmxName.toString());
            base.append(Constants.JMX_CONNECTION_BASE_EXT);
            base.append(Long.toString(connIndex));
            config.setJmxNameBase(base.toString());
            config.setJmxNamePrefix(Constants.JMX_STATEMENT_POOL_PREFIX);
        } else {
            config.setJmxEnabled(false);
        }
        final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = new GenericKeyedObjectPool<>(
                (PoolingConnection) conn, config);
        ((PoolingConnection) conn).setStatementPool(stmtPool);
        ((PoolingConnection) conn).setCacheState(getCacheState());
    }
    final PoolableManagedConnection pmc = new PoolableManagedConnection(transactionRegistry, conn, getPool(),
            getDisconnectionSqlCodes(), isFastFailValidation());
    pmc.setCacheState(getCacheState());
    return new DefaultPooledObject<>(pmc);
}
 
Example #13
Source File: TestPoolingConnection.java    From commons-dbcp with Apache License 2.0 5 votes vote down vote up
@BeforeEach
public void setUp() throws Exception {
    con = new PoolingConnection(new TesterConnection("test", "test"));
    final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
    config.setMaxTotalPerKey(-1);
    config.setBlockWhenExhausted(false);
    config.setMaxWaitMillis(0);
    config.setMaxIdlePerKey(1);
    config.setMaxTotal(1);
    final KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool =
            new GenericKeyedObjectPool<>(con, config);
    con.setStatementPool(stmtPool);
}
 
Example #14
Source File: PooledContextSource.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new pooling context source, setting up the DirContext object
 * factory and generic keyed object pool.
 */
public PooledContextSource(PoolConfig poolConfig) {
	this.dirContextPooledObjectFactory = new DirContextPooledObjectFactory();
	if (poolConfig != null) {
		this.poolConfig = poolConfig;
		GenericKeyedObjectPoolConfig objectPoolConfig = getConfig(poolConfig);
		this.keyedObjectPool =
				new GenericKeyedObjectPool<Object,Object>(this.dirContextPooledObjectFactory, objectPoolConfig);
	} else  {
		this.keyedObjectPool =
				new GenericKeyedObjectPool<Object,Object>(this.dirContextPooledObjectFactory);
	}
}
 
Example #15
Source File: ThriftClient.java    From ThriftJ with Apache License 2.0 5 votes vote down vote up
private void checkAndInit() {
	if (this.servers == null || StringUtils.isEmpty(this.servers)) {
		throw new ValidationException("servers can not be null or empty.");
	}
	if (!checkLoadBalance()) {
		this.loadBalance = Constant.LoadBalance.RANDOM;
	}
	if (this.validator == null) {
		this.validator = new ConnectionValidator() {
			@Override
			public boolean isValid(TTransport object) {
				return object.isOpen();
			}
		};
	}
	if (this.poolConfig == null) {
		this.poolConfig = new GenericKeyedObjectPoolConfig();
	}
	if (this.failoverStrategy == null) {
		this.failoverStrategy = new FailoverStrategy<>();
	}
	if (this.connTimeout == 0) {
		this.connTimeout = DEFAULT_CONN_TIMEOUT;
	}
	if (!checkServiceLevel()) {
		this.serviceLevel = Constant.ServiceLevel.NOT_EMPTY;
	}
}
 
Example #16
Source File: ClientSelector.java    From ThriftJ with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings({ "rawtypes", "unchecked" })
public ClientSelector(String servers, int loadBalance, ConnectionValidator validator, GenericKeyedObjectPoolConfig poolConfig, FailoverStrategy strategy, int connTimeout, String backupServers, int serviceLevel) {
   	this.failoverChecker = new FailoverChecker(validator, strategy, serviceLevel);
   	this.poolProvider = new DefaultThriftConnectionPool(new ThriftConnectionFactory(failoverChecker, connTimeout), poolConfig);
   	failoverChecker.setConnectionPool(poolProvider);
   	failoverChecker.setServerList(ThriftServer.parse(servers));
       if (StringUtils.isNotEmpty(backupServers)) {
       	failoverChecker.setBackupServerList(ThriftServer.parse(backupServers));
       } else{
       	failoverChecker.setBackupServerList(new ArrayList<ThriftServer>());
       }
       failoverChecker.startChecking();
   }
 
Example #17
Source File: PooledContextSource.java    From spring-ldap with Apache License 2.0 5 votes vote down vote up
private GenericKeyedObjectPoolConfig getConfig(PoolConfig poolConfig) {
	GenericKeyedObjectPoolConfig objectPoolConfig = new GenericKeyedObjectPoolConfig();

	objectPoolConfig.setMaxTotalPerKey(poolConfig.getMaxTotalPerKey());
	objectPoolConfig.setMaxTotal(poolConfig.getMaxTotal());

	objectPoolConfig.setMaxIdlePerKey(poolConfig.getMaxIdlePerKey());
	objectPoolConfig.setMinIdlePerKey(poolConfig.getMinIdlePerKey());

	objectPoolConfig.setTestWhileIdle(poolConfig.isTestWhileIdle());
	objectPoolConfig.setTestOnReturn(poolConfig.isTestOnReturn());
	objectPoolConfig.setTestOnCreate(poolConfig.isTestOnCreate());
	objectPoolConfig.setTestOnBorrow(poolConfig.isTestOnBorrow());

	objectPoolConfig.setTimeBetweenEvictionRunsMillis(poolConfig.getTimeBetweenEvictionRunsMillis());
	objectPoolConfig.setEvictionPolicyClassName(poolConfig.getEvictionPolicyClassName());
	objectPoolConfig.setMinEvictableIdleTimeMillis(poolConfig.getMinEvictableIdleTimeMillis());
	objectPoolConfig.setNumTestsPerEvictionRun(poolConfig.getNumTestsPerEvictionRun());
	objectPoolConfig.setSoftMinEvictableIdleTimeMillis(poolConfig.getSoftMinEvictableIdleTimeMillis());

	objectPoolConfig.setJmxEnabled(poolConfig.isJmxEnabled());
	objectPoolConfig.setJmxNameBase(poolConfig.getJmxNameBase());
	objectPoolConfig.setJmxNamePrefix(poolConfig.getJmxNamePrefix());

	objectPoolConfig.setMaxWaitMillis(poolConfig.getMaxWaitMillis());

	objectPoolConfig.setFairness(poolConfig.isFairness());
	objectPoolConfig.setBlockWhenExhausted(poolConfig.isBlockWhenExhausted());
	objectPoolConfig.setLifo(poolConfig.isLifo());

	return objectPoolConfig;
}
 
Example #18
Source File: AemServiceConfiguration.java    From jwala with Apache License 2.0 5 votes vote down vote up
@Bean
public GenericKeyedObjectPool<ChannelSessionKey, Channel> getChannelPool(final SshConfig sshConfig) throws JSchException {
    final GenericKeyedObjectPoolConfig genericKeyedObjectPoolConfig = new GenericKeyedObjectPoolConfig();
    genericKeyedObjectPoolConfig.setMaxTotalPerKey(10);
    genericKeyedObjectPoolConfig.setBlockWhenExhausted(true);
    return new GenericKeyedObjectPool(new KeyedPooledJschChannelFactory(sshConfig.getJschBuilder().build()));
}
 
Example #19
Source File: TcpRpcEndpoint.java    From nutzcloud with Apache License 2.0 5 votes vote down vote up
public void init() {
    debug = conf.getBoolean("literpc.endpoint.tcp.debug", false);
    GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    poolConfig.setMaxTotal(500);
    poolConfig.setTestWhileIdle(true);
    pool = new GenericKeyedObjectPool<>(new RpcSocketFactory(), poolConfig);
}
 
Example #20
Source File: DriverAdapterCPDS.java    From commons-dbcp with Apache License 2.0 4 votes vote down vote up
/**
 * Attempts to establish a database connection.
 *
 * @param pooledUserName
 *            name to be used for the connection
 * @param pooledUserPassword
 *            password to be used fur the connection
 */
@Override
public PooledConnection getPooledConnection(final String pooledUserName, final String pooledUserPassword)
        throws SQLException {
    getConnectionCalled = true;
    PooledConnectionImpl pooledConnection = null;
    // Workaround for buggy WebLogic 5.1 classloader - ignore the exception upon first invocation.
    try {
        if (connectionProperties != null) {
            update(connectionProperties, KEY_USER, pooledUserName);
            update(connectionProperties, KEY_PASSWORD, pooledUserPassword);
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), pooledUserName, pooledUserPassword));
        }
        pooledConnection.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    } catch (final ClassCircularityError e) {
        if (connectionProperties != null) {
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), connectionProperties));
        } else {
            pooledConnection = new PooledConnectionImpl(
                    DriverManager.getConnection(getUrl(), pooledUserName, pooledUserPassword));
        }
        pooledConnection.setAccessToUnderlyingConnectionAllowed(isAccessToUnderlyingConnectionAllowed());
    }
    KeyedObjectPool<PStmtKey, DelegatingPreparedStatement> stmtPool = null;
    if (isPoolPreparedStatements()) {
        final GenericKeyedObjectPoolConfig<DelegatingPreparedStatement> config = new GenericKeyedObjectPoolConfig<>();
        config.setMaxTotalPerKey(Integer.MAX_VALUE);
        config.setBlockWhenExhausted(false);
        config.setMaxWaitMillis(0);
        config.setMaxIdlePerKey(getMaxIdle());
        if (getMaxPreparedStatements() <= 0) {
            // since there is no limit, create a prepared statement pool with an eviction thread;
            // evictor settings are the same as the connection pool settings.
            config.setTimeBetweenEvictionRunsMillis(getTimeBetweenEvictionRunsMillis());
            config.setNumTestsPerEvictionRun(getNumTestsPerEvictionRun());
            config.setMinEvictableIdleTimeMillis(getMinEvictableIdleTimeMillis());
        } else {
            // since there is a limit, create a prepared statement pool without an eviction thread;
            // pool has LRU functionality so when the limit is reached, 15% of the pool is cleared.
            // see org.apache.commons.pool2.impl.GenericKeyedObjectPool.clearOldest method
            config.setMaxTotal(getMaxPreparedStatements());
            config.setTimeBetweenEvictionRunsMillis(-1);
            config.setNumTestsPerEvictionRun(0);
            config.setMinEvictableIdleTimeMillis(0);
        }
        stmtPool = new GenericKeyedObjectPool<>(pooledConnection, config);
        pooledConnection.setStatementPool(stmtPool);
    }
    return pooledConnection;
}
 
Example #21
Source File: KeyedObjectPoolForTest.java    From x-pipe with Apache License 2.0 4 votes vote down vote up
public KeyedObjectPoolForTest(ObjectFactory pooledObjectFactory) {
    this(createDefaultConfig(GenericKeyedObjectPoolConfig.DEFAULT_MAX_TOTAL_PER_KEY));
    this.pooledObjectFactory = pooledObjectFactory;
}
 
Example #22
Source File: ObjectPoolIssue326.java    From commons-pool with Apache License 2.0 4 votes vote down vote up
private void run() throws Exception {
    final GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    poolConfig.setMaxTotal(10);
    poolConfig.setMaxTotalPerKey(5);
    poolConfig.setMinIdlePerKey(-1);
    poolConfig.setMaxIdlePerKey(-1);
    poolConfig.setLifo(true);
    poolConfig.setFairness(true);
    poolConfig.setMaxWaitMillis(30 * 1000);
    poolConfig.setMinEvictableIdleTimeMillis(-1);
    poolConfig.setSoftMinEvictableIdleTimeMillis(-1);
    poolConfig.setNumTestsPerEvictionRun(1);
    poolConfig.setTestOnCreate(false);
    poolConfig.setTestOnBorrow(false);
    poolConfig.setTestOnReturn(false);
    poolConfig.setTestWhileIdle(false);
    poolConfig.setTimeBetweenEvictionRunsMillis(5 * 1000);
    poolConfig.setEvictionPolicyClassName(BaseObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME);
    poolConfig.setBlockWhenExhausted(false);
    poolConfig.setJmxEnabled(false);
    poolConfig.setJmxNameBase(null);
    poolConfig.setJmxNamePrefix(null);

    final GenericKeyedObjectPool<Integer, Object> pool = new GenericKeyedObjectPool<>(new ObjectFactory(), poolConfig);

    // number of threads to reproduce is finicky. this count seems to be best for my
    // 4 core box.
    // too many doesn't reproduce it ever, too few doesn't either.
    final ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2);
    final long startTime = System.currentTimeMillis();
    long testIter = 0;
    try {
        while (true) {
            testIter++;
            if (testIter % 1000 == 0) {
                System.out.println(testIter);
            }
            final List<Task> tasks = createTasks(pool);
            final List<Future<Object>> futures = service.invokeAll(tasks);
            for (final Future<Object> future : futures) {
                future.get();
            }
        }
    } finally {
        System.out.println("Time: " + (System.currentTimeMillis() - startTime) / 1000.0);
        service.shutdown();
    }
}
 
Example #23
Source File: SimpleWorkerPool.java    From bazel with Apache License 2.0 4 votes vote down vote up
public SimpleWorkerPool(WorkerFactory factory, GenericKeyedObjectPoolConfig<Worker> config) {
  super(factory, config);
}
 
Example #24
Source File: ClientMain.java    From thrift-pool-client with Artistic License 2.0 4 votes vote down vote up
/**
 * @throws TException
 */
public static void main(String[] args) throws TException {
    // init a thrift client
    ThriftClient thriftClient = new ThriftClientImpl(() -> Arrays.asList(//
            ThriftServerInfo.of("127.0.0.1", 9090),
            ThriftServerInfo.of("127.0.0.1", 9091)
            // or you can return a dynamic result.
            ));
    // get iface and call
    System.out.println(thriftClient.iface(Client.class).echo("hello world."));

    // get iface with custom hash, the same hash return the same thrift backend server
    System.out.println(thriftClient.iface(Client.class, "hello world".hashCode()).echo(
            "hello world"));

    // customize protocol
    System.out.println(thriftClient.iface(Client.class, TBinaryProtocol::new,
            "hello world".hashCode()).echo("hello world"));

    // customize pool config
    GenericKeyedObjectPoolConfig poolConfig = new GenericKeyedObjectPoolConfig();
    // ... customize pool config here
    // customize transport, while if you expect pooling the connection, you should use TFrameTransport.
    Function<ThriftServerInfo, TTransport> transportProvider = info -> {
        TSocket socket = new TSocket(info.getHost(), info.getPort());
        TFramedTransport transport = new TFramedTransport(socket);
        return transport;
    };
    ThriftClient customizeThriftClient = new ThriftClientImpl(() -> Arrays.asList(//
            ThriftServerInfo.of("127.0.0.1", 9090),
            ThriftServerInfo.of("127.0.0.1", 9091)
            ), new DefaultThriftConnectionPoolImpl(poolConfig, transportProvider));
    customizeThriftClient.iface(Client.class).echo("hello world.");

    // init a failover thrift client
    ThriftClient failoverThriftClient = new FailoverThriftClientImpl(() -> Arrays.asList(//
            ThriftServerInfo.of("127.0.0.1", 9090),
            ThriftServerInfo.of("127.0.0.1", 9091)
            ));
    failoverThriftClient.iface(Client.class).echo("hello world.");

    // a customize failover client, if the call fail 10 times in 30 seconds, the backend server will be marked as fail for 1 minutes.
    FailoverCheckingStrategy<ThriftServerInfo> failoverCheckingStrategy = new FailoverCheckingStrategy<>(
            10, TimeUnit.SECONDS.toMillis(30), TimeUnit.MINUTES.toMillis(1));
    ThriftClient customizedFailoverThriftClient = new FailoverThriftClientImpl(
            failoverCheckingStrategy, () -> Arrays.asList(//
                    ThriftServerInfo.of("127.0.0.1", 9090),
                    ThriftServerInfo.of("127.0.0.1", 9091)
                    ), DefaultThriftConnectionPoolImpl.getInstance());
    customizedFailoverThriftClient.iface(Client.class).echo("hello world.");
}
 
Example #25
Source File: CasStorageServiceImpl.java    From webanno with Apache License 2.0 4 votes vote down vote up
/**
 * @param aCasDoctor
 *            (optional) if present, CAS validation can take place
 * @param aSchemaService
 *            (optional) if present, CAS upgrades can be performed
 */
public CasStorageServiceImpl(
        @Autowired(required = false) CasDoctor aCasDoctor,
        @Autowired(required = false) AnnotationSchemaService aSchemaService,
        @Autowired RepositoryProperties aRepositoryProperties,
        @Autowired BackupProperties aBackupProperties)
{
    casDoctor = aCasDoctor;
    schemaService = aSchemaService;
    repositoryProperties = aRepositoryProperties;
    backupProperties = aBackupProperties;
    
    GenericKeyedObjectPoolConfig<CasHolder> config = new GenericKeyedObjectPoolConfig<>();
    // Since we want the pool to control exclusive access to a particular CAS, we only ever 
    // must have one instance per key (the key uniquely identifies the CAS)
    config.setMaxTotalPerKey(1);
    // Setting this to 0 because we do not want any CAS to stick around in memory indefinitely
    config.setMinIdlePerKey(0);
    // Run an evictor thread every 5 minutes
    config.setTimeBetweenEvictionRunsMillis(MINUTES.toMillis(5));
    // Allow the evictor to drop idle CASes from the pool after 5 minutes (i.e. on each run)
    config.setMinEvictableIdleTimeMillis(MINUTES.toMillis(EVICT_IDLE_CASES_AFTER_MINUTES));
    // Allow the evictor to drop all idle CASes on every eviction run
    config.setNumTestsPerEvictionRun(-1);
    // Allow viewing the pool in JMX
    config.setJmxEnabled(true);
    config.setJmxNameBase(getClass().getPackage().getName() + ":type="
            + getClass().getSimpleName() + ",name=");
    config.setJmxNamePrefix("exclusiveCasAccessPool");
    // Check if the CAS is still valid or needs to be replaced when it is borrowed and when it
    // is returned
    config.setTestOnReturn(true);
    config.setTestOnBorrow(true);
    // We do not have to set maxTotal because the default is already to have no limit (-1)
    exclusiveAccessPool = new GenericKeyedObjectPool<>(new PooledCasHolderFactory(), config);
    
    sharedAccessCache = Caffeine.newBuilder()
            .expireAfterAccess(EVICT_IDLE_CASES_AFTER_MINUTES, MINUTES)
            .maximumSize(SHARED_CAS_CACHE_SIZE)
            .recordStats()
            .build();

    if (casDoctor == null) {
        log.info("CAS doctor not available - unable to check/repair CASes");
    }

    if (backupProperties.getInterval() > 0) {
        log.info("CAS backups enabled - interval: {}sec  max-backups: {}  max-age: {}sec",
                backupProperties.getInterval(), backupProperties.getKeep().getNumber(),
                backupProperties.getKeep().getTime());
    }
    else {
        log.info("CAS backups disabled");
    }
}
 
Example #26
Source File: LdapTemplateNamespaceHandlerTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void verifyParsePooling2Defaults() {
    ClassPathXmlApplicationContext ctx = new ClassPathXmlApplicationContext("/ldap-namespace-config-pooling2-defaults.xml");

    ContextSource outerContextSource = ctx.getBean(ContextSource.class);
    assertThat(outerContextSource).isNotNull();
    assertThat(outerContextSource instanceof TransactionAwareContextSourceProxy).isTrue();

    ContextSource pooledContextSource = ((TransactionAwareContextSourceProxy) outerContextSource).getTarget();
    assertThat(pooledContextSource).isNotNull();
    assertThat(pooledContextSource instanceof PooledContextSource).isTrue();
    assertThat(getInternalState(pooledContextSource, "poolConfig")).isNotNull();

    Object objectFactory = getInternalState(pooledContextSource, "dirContextPooledObjectFactory");
    assertThat(getInternalState(objectFactory, "contextSource")).isNotNull();
    assertThat(getInternalState(objectFactory, "dirContextValidator")).isNull();
    Set<Class<? extends Throwable>> nonTransientExceptions =
            (Set<Class<? extends Throwable>>) getInternalState(objectFactory, "nonTransientExceptions");
    assertThat(nonTransientExceptions).hasSize(1);
    assertThat(nonTransientExceptions.contains(CommunicationException.class)).isTrue();

    org.apache.commons.pool2.impl.GenericKeyedObjectPool objectPool =
            (org.apache.commons.pool2.impl.GenericKeyedObjectPool) getInternalState(pooledContextSource, "keyedObjectPool");
    assertThat(objectPool.getMaxIdlePerKey()).isEqualTo(8);
    assertThat(objectPool.getMaxTotal()).isEqualTo(-1);
    assertThat(objectPool.getMaxTotalPerKey()).isEqualTo(8);
    assertThat(objectPool.getMinIdlePerKey()).isEqualTo(0);
    assertThat(objectPool.getBlockWhenExhausted()).isEqualTo(true);
    assertThat(objectPool.getEvictionPolicyClassName()).isEqualTo(GenericKeyedObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME);
    assertThat(objectPool.getFairness()).isEqualTo(false);

    // ensures the pool is registered
    ObjectName oname = objectPool.getJmxName();
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    Set<ObjectName> result = mbs.queryNames(oname, null);
    assertThat(result).hasSize(1);

    assertThat(objectPool.getLifo()).isEqualTo(true);
    assertThat(objectPool.getMaxWaitMillis()).isEqualTo(-1L);
    assertThat(objectPool.getMinEvictableIdleTimeMillis()).isEqualTo(1000L*60L*30L);
    assertThat(objectPool.getNumTestsPerEvictionRun()).isEqualTo(3);
    assertThat(objectPool.getSoftMinEvictableIdleTimeMillis()).isEqualTo(-1L);
    assertThat(objectPool.getTimeBetweenEvictionRunsMillis()).isEqualTo(-1L);
    assertThat(objectPool.getTestOnBorrow()).isEqualTo(false);
    assertThat(objectPool.getTestOnCreate()).isEqualTo(false);
    assertThat(objectPool.getTestOnReturn()).isEqualTo(false);
    assertThat(objectPool.getTestWhileIdle()).isEqualTo(false);
}
 
Example #27
Source File: PoolConfigTest.java    From spring-ldap with Apache License 2.0 4 votes vote down vote up
@Test
public void testProperties() {
    final PoolConfig poolConfig = new PoolConfig();

    poolConfig.setMaxTotalPerKey(5);
    final int maxTotalPerKey = poolConfig.getMaxTotalPerKey();
    assertThat(maxTotalPerKey).isEqualTo(5);

    poolConfig.setMaxIdlePerKey(500);
    final int maxIdle = poolConfig.getMaxIdlePerKey();
    assertThat(maxIdle).isEqualTo(500);

    poolConfig.setMaxTotal(5000);
    final int maxTotal = poolConfig.getMaxTotal();
    assertThat(maxTotal).isEqualTo(5000);

    poolConfig.setMaxWaitMillis(2000L);
    final long maxWait = poolConfig.getMaxWaitMillis();
    assertThat(maxWait).isEqualTo(2000L);

    poolConfig.setMinEvictableIdleTimeMillis(60000L);
    final long minEvictableIdleTimeMillis = poolConfig.getMinEvictableIdleTimeMillis();
    assertThat(minEvictableIdleTimeMillis).isEqualTo(60000L);

    poolConfig.setMinIdlePerKey(100);
    final int minIdle = poolConfig.getMinIdlePerKey();
    assertThat(minIdle).isEqualTo(100);

    poolConfig.setNumTestsPerEvictionRun(5);
    final int numTestsPerEvictionRun = poolConfig.getNumTestsPerEvictionRun();
    assertThat(numTestsPerEvictionRun).isEqualTo(5);

    poolConfig.setTestOnBorrow(true);
    final boolean testOnBorrow = poolConfig.isTestOnBorrow();
    assertThat(testOnBorrow).isEqualTo(true);

    poolConfig.setTestOnReturn(true);
    final boolean testOnReturn = poolConfig.isTestOnReturn();
    assertThat(testOnReturn).isEqualTo(true);

    poolConfig.setTestWhileIdle(true);
    final boolean testWhileIdle = poolConfig.isTestWhileIdle();
    assertThat(testWhileIdle).isEqualTo(true);

    poolConfig.setTestOnCreate(true);
    final boolean testOnCreate = poolConfig.isTestOnCreate();
    assertThat(testOnCreate).isEqualTo(true);

    poolConfig.setTimeBetweenEvictionRunsMillis(120000L);
    final long timeBetweenEvictionRunsMillis = poolConfig.getTimeBetweenEvictionRunsMillis();
    assertThat(timeBetweenEvictionRunsMillis).isEqualTo(120000L);

    poolConfig.setSoftMinEvictableIdleTimeMillis(120000L);
    final long softMinEvictableIdleTimeMillis = poolConfig.getSoftMinEvictableIdleTimeMillis();
    assertThat(softMinEvictableIdleTimeMillis).isEqualTo(120000L);

    poolConfig.setBlockWhenExhausted(true);
    final boolean whenExhaustedAction = poolConfig.isBlockWhenExhausted();
    assertThat(whenExhaustedAction).isEqualTo(GenericKeyedObjectPoolConfig.DEFAULT_BLOCK_WHEN_EXHAUSTED);

    poolConfig.setEvictionPolicyClassName(GenericKeyedObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME);
    final String evictionPolicyClassName = poolConfig.getEvictionPolicyClassName();
    assertThat(evictionPolicyClassName).isEqualTo(GenericKeyedObjectPoolConfig.DEFAULT_EVICTION_POLICY_CLASS_NAME);

    poolConfig.setFairness(true);
    final boolean fairness = poolConfig.isFairness();
    assertThat(fairness).isEqualTo(true);

    poolConfig.setJmxEnabled(true);
    final boolean jmxEnabled = poolConfig.isJmxEnabled();
    assertThat(jmxEnabled).isEqualTo(true);

    poolConfig.setJmxNameBase("test");
    final String jmxBaseName = poolConfig.getJmxNameBase();
    assertThat(jmxBaseName).isEqualTo("test");

    poolConfig.setJmxNamePrefix("pool");
    final String prefix = poolConfig.getJmxNamePrefix();
    assertThat(prefix).isEqualTo("pool");

    poolConfig.setLifo(true);
    final boolean lifo = poolConfig.isLifo();
    assertThat(lifo).isEqualTo(true);
}
 
Example #28
Source File: ConnectionPool.java    From fastdfs-java-client with Apache License 2.0 4 votes vote down vote up
/**
 * 默认构造函数
 */
public ConnectionPool(KeyedPooledObjectFactory<InetSocketAddress, Connection> factory, GenericKeyedObjectPoolConfig config) {
    super(factory, config);
}
 
Example #29
Source File: JmsPoolConnectionFactory.java    From pooled-jms with Apache License 2.0 4 votes vote down vote up
public void initConnectionsPool() {
    if (this.connectionsPool == null) {
        final GenericKeyedObjectPoolConfig<PooledConnection> poolConfig = new GenericKeyedObjectPoolConfig<>();
        poolConfig.setJmxEnabled(false);
        this.connectionsPool = new GenericKeyedObjectPool<PooledConnectionKey, PooledConnection>(
            new KeyedPooledObjectFactory<PooledConnectionKey, PooledConnection>() {
                @Override
                public PooledObject<PooledConnection> makeObject(PooledConnectionKey connectionKey) throws Exception {
                    Connection delegate = createProviderConnection(connectionKey);

                    PooledConnection connection = createPooledConnection(delegate);
                    connection.setIdleTimeout(getConnectionIdleTimeout());
                    connection.setMaxSessionsPerConnection(getMaxSessionsPerConnection());
                    connection.setBlockIfSessionPoolIsFull(isBlockIfSessionPoolIsFull());
                    if (isBlockIfSessionPoolIsFull() && getBlockIfSessionPoolIsFullTimeout() > 0) {
                        connection.setBlockIfSessionPoolIsFullTimeout(getBlockIfSessionPoolIsFullTimeout());
                    }
                    connection.setUseAnonymousProducers(isUseAnonymousProducers());
                    connection.setExplicitProducerCacheSize(getExplicitProducerCacheSize());

                    LOG.trace("Created new connection: {}", connection);

                    JmsPoolConnectionFactory.this.mostRecentlyCreated.set(connection);

                    return new DefaultPooledObject<PooledConnection>(connection);
                }

                @Override
                public void destroyObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) throws Exception {
                    PooledConnection connection = pooledObject.getObject();
                    try {
                        LOG.trace("Destroying connection: {}", connection);
                        connection.close();
                    } catch (Exception e) {
                        LOG.warn("Close connection failed for connection: " + connection + ". This exception will be ignored.",e);
                    }
                }

                @Override
                public boolean validateObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) {
                    PooledConnection connection = pooledObject.getObject();
                    if (connection != null && connection.expiredCheck()) {
                        LOG.trace("Connection has expired: {} and will be destroyed", connection);
                        return false;
                    }

                    return true;
                }

                @Override
                public void activateObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) throws Exception {
                }

                @Override
                public void passivateObject(PooledConnectionKey connectionKey, PooledObject<PooledConnection> pooledObject) throws Exception {
                }

            }, poolConfig);

        // Set max idle (not max active) since our connections always idle in the pool.
        this.connectionsPool.setMaxIdlePerKey(DEFAULT_MAX_CONNECTIONS);
        this.connectionsPool.setLifo(false);
        this.connectionsPool.setMinIdlePerKey(1);
        this.connectionsPool.setBlockWhenExhausted(false);

        // We always want our validate method to control when idle objects are evicted.
        this.connectionsPool.setTestOnBorrow(true);
        this.connectionsPool.setTestWhileIdle(true);
    }
}
 
Example #30
Source File: AbstractTransportPool.java    From jigsaw-payment with Apache License 2.0 4 votes vote down vote up
public AbstractTransportPool() {
    this.poolConfig = new GenericKeyedObjectPoolConfig();
    this.poolConfig.setMaxIdlePerKey(maxIdlePerKey);
    this.poolConfig.setMaxTotalPerKey(maxTotalPerKey);
}