Example usage for org.apache.commons.pool2.impl GenericObjectPool borrowObject

List of usage examples for org.apache.commons.pool2.impl GenericObjectPool borrowObject

Introduction

In this page you can find the example usage for org.apache.commons.pool2.impl GenericObjectPool borrowObject.

Prototype

@Override
public T borrowObject() throws Exception 

Source Link

Document

Equivalent to #borrowObject(long) borrowObject ( #getMaxWaitMillis() ).

Usage

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void genericPoolUsingWrappingShouldPropagateExceptionsCorrectly() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    StatefulRedisConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();
    sync.set(key, value);/*from www  .  j av a2s  .  c om*/

    try {
        sync.hgetall(key);
        fail("Missing RedisCommandExecutionException");
    } catch (RedisCommandExecutionException e) {
        assertThat(e).hasMessageContaining("WRONGTYPE");
    }

    connection.close();
    pool.close();
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void wrappedConnectionShouldUseWrappers() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    StatefulRedisConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisClusterConnectionImpl.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAsyncCommands.class)
            .isNotInstanceOf(RedisAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisReactiveCommands.class)
            .isNotInstanceOf(RedisReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisConnectionImpl.class).isSameAs(connection);

    connection.close();// ww  w  .j av a 2  s  .co  m
    pool.close();
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void wrappedObjectClosedAfterReturn() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig(), true);

    StatefulRedisConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();
    sync.ping();//from w  w w. j av  a 2s . co  m

    connection.close();

    try {
        connection.isMulti();
        fail("Missing RedisException");
    } catch (RedisException e) {
        assertThat(e).hasMessageContaining("deallocated");
    }

    pool.close();
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

@Test
public void genericPoolShouldWorkWithWrappedConnections() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    borrowAndReturn(pool);//from   w  w  w.jav  a 2s.  c o m
    borrowAndClose(pool);
    borrowAndCloseTryWithResources(pool);

    pool.returnObject(pool.borrowObject().sync().getStatefulConnection());
    pool.returnObject(pool.borrowObject().async().getStatefulConnection());

    pool.close();
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

@Test
public void softReferencePoolShouldWorkWithWrappedConnections() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    borrowAndReturn(pool);//  w w w. ja  v  a 2  s .c  om
    borrowAndClose(pool);
    borrowAndCloseTryWithResources(pool);

    pool.returnObject(pool.borrowObject().sync().getStatefulConnection());
    pool.returnObject(pool.borrowObject().async().getStatefulConnection());

    pool.close();
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

@Test
public void tryWithResourcesReturnsConnectionToPool() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    StatefulRedisConnection<String, String> usedConnection = null;
    try (StatefulRedisConnection<String, String> connection = pool.borrowObject()) {

        RedisCommands<String, String> sync = connection.sync();
        sync.ping();// ww  w  . j  a v a 2 s .  com

        usedConnection = connection;
    }

    try {
        usedConnection.isMulti();
        fail("Missing RedisException");
    } catch (RedisException e) {
        assertThat(e).hasMessageContaining("deallocated");
    }

    pool.close();
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void wrappedClusterConnectionShouldUseWrappers() throws Exception {

    RedisClusterClient redisClusterClient = RedisClusterClient.create(TestClientResources.get(),
            RedisURI.create(TestSettings.host(), 7379));

    GenericObjectPool<StatefulRedisClusterConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(redisClusterClient::connect, new GenericObjectPoolConfig());

    StatefulRedisClusterConnection<String, String> connection = pool.borrowObject();
    RedisAdvancedClusterCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisClusterConnection.class)
            .isNotInstanceOf(StatefulRedisClusterConnectionImpl.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisAdvancedClusterCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAdvancedClusterAsyncCommands.class)
            .isNotInstanceOf(RedisAdvancedClusterAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisAdvancedClusterReactiveCommands.class)
            .isNotInstanceOf(RedisAdvancedClusterReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisClusterConnection.class)
            .isNotInstanceOf(StatefulRedisClusterConnectionImpl.class).isSameAs(connection);

    connection.close();// w  ww .  j a  v  a 2  s .  c  om
    pool.close();

    FastShutdown.shutdown(redisClusterClient);
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void genericPoolShouldWorkWithWrappedConnections() throws Exception {

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), new GenericObjectPoolConfig());

    borrowAndReturn(pool);//w  ww  .j  av  a  2 s  .  com
    borrowAndClose(pool);
    borrowAndCloseTryWithResources(pool);

    pool.returnObject(pool.borrowObject().sync().getStatefulConnection());
    pool.returnObject(pool.borrowObject().async().getStatefulConnection());

    assertThat(channels).hasSize(1);

    pool.close();

    Wait.untilTrue(channels::isEmpty).waitOrTimeout();

    assertThat(channels).isEmpty();
}

From source file:com.lambdaworks.redis.support.ConnectionPoolSupportTest.java

@Test
public void wrappedMasterSlaveConnectionShouldUseWrappers() throws Exception {

    GenericObjectPool<StatefulRedisMasterSlaveConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(// www  .  j  a  v  a 2  s .c  om
                    () -> MasterSlave.connect(client, new StringCodec(), RedisURI.create(host, port)),
                    new GenericObjectPoolConfig());

    StatefulRedisMasterSlaveConnection<String, String> connection = pool.borrowObject();
    RedisCommands<String, String> sync = connection.sync();

    assertThat(connection).isInstanceOf(StatefulRedisMasterSlaveConnection.class);
    assertThat(Proxy.isProxyClass(connection.getClass())).isTrue();

    assertThat(sync).isInstanceOf(RedisCommands.class);
    assertThat(connection.async()).isInstanceOf(RedisAsyncCommands.class)
            .isNotInstanceOf(RedisAsyncCommandsImpl.class);
    assertThat(connection.reactive()).isInstanceOf(RedisReactiveCommands.class)
            .isNotInstanceOf(RedisReactiveCommandsImpl.class);
    assertThat(sync.getStatefulConnection()).isInstanceOf(StatefulRedisConnection.class)
            .isNotInstanceOf(StatefulRedisConnectionImpl.class).isSameAs(connection);

    sync.close();
    pool.close();
}

From source file:io.lettuce.core.support.ConnectionPoolSupportTest.java

@Test
public void genericPoolShouldCloseConnectionsAboveMaxIdleSize() throws Exception {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();
    poolConfig.setMaxIdle(2);//from w w w  .  j  a v a2s  .co m

    GenericObjectPool<StatefulRedisConnection<String, String>> pool = ConnectionPoolSupport
            .createGenericObjectPool(() -> client.connect(), poolConfig);

    borrowAndReturn(pool);
    borrowAndClose(pool);
    borrowAndCloseTryWithResources(pool);

    StatefulRedisConnection<String, String> c1 = pool.borrowObject();
    StatefulRedisConnection<String, String> c2 = pool.borrowObject();
    StatefulRedisConnection<String, String> c3 = pool.borrowObject();

    assertThat(channels).hasSize(3);

    pool.returnObject(c1);
    pool.returnObject(c2);
    pool.returnObject(c3);

    assertThat(channels).hasSize(2);

    pool.close();

    Wait.untilTrue(channels::isEmpty).waitOrTimeout();

    assertThat(channels).isEmpty();
}