Search in sources :

Example 26 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project jetcache by alibaba.

the class RedisCacheTest method readFromSlaveTest.

@Test
public void readFromSlaveTest() throws Exception {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);
    JedisPool pool1 = new JedisPool(pc, "127.0.0.1", 6379);
    JedisPool pool2 = new JedisPool(pc, "127.0.0.1", 6380);
    JedisPool pool3 = new JedisPool(pc, "127.0.0.1", 6381);
    RedisCacheBuilder builder = RedisCacheBuilder.createRedisCacheBuilder();
    builder.setJedisPool(pool1);
    builder.setReadFromSlave(true);
    builder.setJedisSlavePools(pool2, pool3);
    builder.setSlaveReadWeights(1, 1);
    builder.setKeyConvertor(FastjsonKeyConvertor.INSTANCE);
    builder.setValueEncoder(JavaValueEncoder.INSTANCE);
    builder.setValueDecoder(JavaValueDecoder.INSTANCE);
    builder.setKeyPrefix(new Random().nextInt() + "");
    builder.setExpireAfterWriteInMillis(500);
    readFromSlaveTestAsserts(pool1, builder);
    builder.setSlaveReadWeights(null);
    readFromSlaveTestAsserts(pool1, builder);
}
Also used : Random(java.util.Random) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) RefreshCacheTest(com.alicp.jetcache.RefreshCacheTest) RedisLettuceCacheTest(com.alicp.jetcache.redis.lettuce.RedisLettuceCacheTest) Test(org.junit.Test) LoadingCacheTest(com.alicp.jetcache.LoadingCacheTest) AbstractExternalCacheTest(com.alicp.jetcache.test.external.AbstractExternalCacheTest)

Example 27 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project jetcache by alibaba.

the class RedisCacheTest method testSimplePool.

@Test
public void testSimplePool() throws Exception {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);
    JedisPool pool = new JedisPool(pc, "127.0.0.1", 6379);
    testImpl(pool);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisPool(redis.clients.jedis.JedisPool) RefreshCacheTest(com.alicp.jetcache.RefreshCacheTest) RedisLettuceCacheTest(com.alicp.jetcache.redis.lettuce.RedisLettuceCacheTest) Test(org.junit.Test) LoadingCacheTest(com.alicp.jetcache.LoadingCacheTest) AbstractExternalCacheTest(com.alicp.jetcache.test.external.AbstractExternalCacheTest)

Example 28 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project jetcache by alibaba.

the class RedisCacheTest method testSentinel.

@Test
public void testSentinel() throws Exception {
    GenericObjectPoolConfig pc = new GenericObjectPoolConfig();
    pc.setMinIdle(2);
    pc.setMaxIdle(10);
    pc.setMaxTotal(10);
    Set<String> sentinels = new HashSet<>();
    sentinels.add("127.0.0.1:26379");
    sentinels.add("127.0.0.1:26380");
    sentinels.add("127.0.0.1:26381");
    JedisSentinelPool pool = new JedisSentinelPool("mymaster", sentinels, pc);
    testImpl(pool);
}
Also used : GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) JedisSentinelPool(redis.clients.jedis.JedisSentinelPool) HashSet(java.util.HashSet) RefreshCacheTest(com.alicp.jetcache.RefreshCacheTest) RedisLettuceCacheTest(com.alicp.jetcache.redis.lettuce.RedisLettuceCacheTest) Test(org.junit.Test) LoadingCacheTest(com.alicp.jetcache.LoadingCacheTest) AbstractExternalCacheTest(com.alicp.jetcache.test.external.AbstractExternalCacheTest)

Example 29 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project java-learn by laidu.

the class PoolApp method main.

public static void main(String[] args) throws Exception {
    ProxyPooledFactory factory = new ProxyPooledFactory();
    GenericObjectPoolConfig<ProxyInfo> poolConfig = new GenericObjectPoolConfig<>();
    poolConfig.setMaxIdle(500);
    poolConfig.setMaxTotal(20);
    poolConfig.setMinIdle(150);
    poolConfig.setMaxWaitMillis(50);
    poolConfig.setTestOnBorrow(true);
    poolConfig.setTestWhileIdle(true);
    poolConfig.setTestOnCreate(false);
    poolConfig.setMaxWaitMillis(100);
    poolConfig.setTimeBetweenEvictionRunsMillis(1000);
    ObjectPool<ProxyInfo> pool = new GenericObjectPool<>(factory, poolConfig);
    while (true) {
        ProxyInfo proxyInfo = pool.borrowObject();
        Thread.sleep(1000000);
        pool.returnObject(proxyInfo);
        System.out.println(pool.getNumActive());
        System.out.println(pool.getNumIdle());
    }
}
Also used : ProxyInfo(org.laidu.learn.commons.pool.openproxy.ProxyInfo) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) ProxyPooledFactory(org.laidu.learn.commons.pool.openproxy.ProxyPooledFactory)

Example 30 with GenericObjectPoolConfig

use of com.frameworkset.commons.pool2.impl.GenericObjectPoolConfig in project cloudstack by apache.

the class TransactionLegacy method createDataSource.

/**
 * Creates a data source
 */
private static DataSource createDataSource(String uri, String username, String password, Integer maxActive, Integer maxIdle, Long maxWait, Long timeBtwnEvictionRuns, Long minEvictableIdleTime, Boolean testWhileIdle, Boolean testOnBorrow, String validationQuery, Integer isolationLevel) {
    ConnectionFactory connectionFactory = new DriverManagerConnectionFactory(uri, username, password);
    PoolableConnectionFactory poolableConnectionFactory = new PoolableConnectionFactory(connectionFactory, null);
    GenericObjectPoolConfig config = createPoolConfig(maxActive, maxIdle, maxWait, timeBtwnEvictionRuns, minEvictableIdleTime, testWhileIdle, testOnBorrow);
    ObjectPool<PoolableConnection> connectionPool = new GenericObjectPool<>(poolableConnectionFactory, config);
    poolableConnectionFactory.setPool(connectionPool);
    if (validationQuery != null) {
        poolableConnectionFactory.setValidationQuery(validationQuery);
    }
    if (isolationLevel != null) {
        poolableConnectionFactory.setDefaultTransactionIsolation(isolationLevel);
    }
    return new PoolingDataSource<>(connectionPool);
}
Also used : ConnectionFactory(org.apache.commons.dbcp2.ConnectionFactory) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory) PoolingDataSource(org.apache.commons.dbcp2.PoolingDataSource) DriverManagerConnectionFactory(org.apache.commons.dbcp2.DriverManagerConnectionFactory) GenericObjectPoolConfig(org.apache.commons.pool2.impl.GenericObjectPoolConfig) PoolableConnection(org.apache.commons.dbcp2.PoolableConnection) GenericObjectPool(org.apache.commons.pool2.impl.GenericObjectPool) PoolableConnectionFactory(org.apache.commons.dbcp2.PoolableConnectionFactory)

Aggregations

GenericObjectPoolConfig (org.apache.commons.pool2.impl.GenericObjectPoolConfig)222 Test (org.junit.Test)82 Jedis (redis.clients.jedis.Jedis)44 ShardedJedisPool (redis.clients.jedis.ShardedJedisPool)43 ShardedJedis (redis.clients.jedis.ShardedJedis)41 JedisPool (redis.clients.jedis.JedisPool)37 GenericObjectPool (org.apache.commons.pool2.impl.GenericObjectPool)32 JedisSentinelPool (redis.clients.jedis.JedisSentinelPool)21 Bean (org.springframework.context.annotation.Bean)20 ArrayList (java.util.ArrayList)17 JedisShardInfo (redis.clients.jedis.JedisShardInfo)17 Test (org.junit.jupiter.api.Test)15 URI (java.net.URI)12 LettuceConnectionFactory (org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory)12 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)12 RedisStandaloneConfiguration (org.springframework.data.redis.connection.RedisStandaloneConfiguration)11 IOException (java.io.IOException)10 Test (org.testng.annotations.Test)10 PooledObject (org.apache.commons.pool2.PooledObject)9 DefaultPooledObject (org.apache.commons.pool2.impl.DefaultPooledObject)9