Example usage for org.springframework.data.redis.connection.lettuce LettuceConnectionFactory getConnection

List of usage examples for org.springframework.data.redis.connection.lettuce LettuceConnectionFactory getConnection

Introduction

In this page you can find the example usage for org.springframework.data.redis.connection.lettuce LettuceConnectionFactory getConnection.

Prototype

public RedisConnection getConnection() 

Source Link

Usage

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test
public void testSelectDb() {

    LettuceConnectionFactory factory2 = new LettuceConnectionFactory(SettingsUtils.getHost(),
            SettingsUtils.getPort());//from   w  ww  . j  a v  a  2 s . c o  m
    factory2.setClientResources(LettuceTestClientResources.getSharedClientResources());
    factory2.setShutdownTimeout(0);
    factory2.setDatabase(1);
    factory2.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory2);

    StringRedisConnection connection2 = new DefaultStringRedisConnection(factory2.getConnection());
    connection2.flushDb();
    // put an item in database 0
    connection.set("sometestkey", "sometestvalue");
    try {
        // there should still be nothing in database 1
        assertEquals(Long.valueOf(0), connection2.dbSize());
    } finally {
        connection2.close();
        factory2.destroy();
    }
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test
public void testCreateFactoryWithPool() {
    DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
    pool.setClientResources(LettuceTestClientResources.getSharedClientResources());
    pool.afterPropertiesSet();//from  ww w.  jav  a  2  s  . c  om
    LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
    factory2.setShutdownTimeout(0);
    factory2.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory2);

    RedisConnection conn2 = factory2.getConnection();
    conn2.close();
    factory2.destroy();
    pool.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Ignore("Uncomment this test to manually check connection reuse in a pool scenario")
@Test/*w ww . ja v  a2s.  c  o m*/
public void testLotsOfConnections() throws InterruptedException {
    // Running a netstat here should show only the 8 conns from the pool (plus 2 from setUp and 1 from factory2
    // afterPropertiesSet for shared conn)
    DefaultLettucePool pool = new DefaultLettucePool(SettingsUtils.getHost(), SettingsUtils.getPort());
    pool.afterPropertiesSet();
    final LettuceConnectionFactory factory2 = new LettuceConnectionFactory(pool);
    factory2.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory2);

    for (int i = 1; i < 1000; i++) {
        Thread th = new Thread(() -> factory2.getConnection().bRPop(50000, "foo".getBytes()));
        th.start();
    }
    Thread.sleep(234234234);
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-431
public void dbIndexShouldBePropagatedCorrectly() {

    LettuceConnectionFactory factory = new LettuceConnectionFactory();
    factory.setClientResources(LettuceTestClientResources.getSharedClientResources());
    factory.setDatabase(2);//from  w w w. j a  v  a  2s  . c o m
    factory.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory);

    StringRedisConnection connectionToDbIndex2 = new DefaultStringRedisConnection(factory.getConnection());

    try {

        String key = "key-in-db-2";
        connectionToDbIndex2.set(key, "the wheel of time");

        assertThat(connection.get(key), nullValue());
        assertThat(connectionToDbIndex2.get(key), notNullValue());
    } finally {
        connectionToDbIndex2.close();
    }
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-462
public void factoryWorksWithoutClientResources() {

    LettuceConnectionFactory factory = new LettuceConnectionFactory();
    factory.setShutdownTimeout(0);//from w  ww  . ja v a 2  s. c o  m
    factory.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory);

    StringRedisConnection connection = new DefaultStringRedisConnection(factory.getConnection());

    try {
        assertThat(connection.ping(), is(equalTo("PONG")));
    } finally {
        connection.close();
    }
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-667
public void factoryCreatesPooledConnections() {

    GenericObjectPoolConfig poolConfig = new GenericObjectPoolConfig();

    LettuceClientConfiguration configuration = LettucePoolingClientConfiguration.builder()
            .poolConfig(poolConfig).clientResources(LettuceTestClientResources.getSharedClientResources())
            .shutdownTimeout(Duration.ZERO).build();

    LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(),
            configuration);/*from w  w  w.j a v a 2  s .c  o  m*/
    factory.setShareNativeConnection(false);
    factory.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory);

    RedisConnection initial = factory.getConnection();
    Object initialNativeConnection = initial.getNativeConnection();

    initial.close();

    RedisConnection subsequent = factory.getConnection();
    Object subsequentNativeConnection = subsequent.getNativeConnection();

    subsequent.close();

    assertThat(initialNativeConnection, is(subsequentNativeConnection));

    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-687
public void connectsThroughRedisSocket() {

    assumeTrue(EpollProvider.isAvailable() || KqueueProvider.isAvailable());
    assumeTrue(new File(SettingsUtils.getSocket()).exists());

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.create();

    LettuceConnectionFactory factory = new LettuceConnectionFactory(SettingsUtils.socketConfiguration(),
            configuration);//w  w w  . j  a v a 2  s.  c  o  m
    factory.setShareNativeConnection(false);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();
    assertThat(connection.ping(), is(equalTo("PONG")));

    connection.close();
    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-762, DATAREDIS-869
public void factoryUsesElastiCacheMasterReplicaConnections() {

    assumeThat(String.format("No slaves connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
            connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder().readFrom(ReadFrom.SLAVE)
            .build();/*from   www  . j  av a 2 s. c  om*/

    RedisStaticMasterReplicaConfiguration elastiCache = new RedisStaticMasterReplicaConfiguration(
            SettingsUtils.getHost()).node(SettingsUtils.getHost(), SettingsUtils.getPort() + 1);

    LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache, configuration);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();

    try {
        assertThat(connection.ping(), is(equalTo("PONG")));
        assertThat(connection.info().getProperty("role"), is(equalTo("slave")));
    } finally {
        connection.close();
    }

    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-762, DATAREDIS-869
public void factoryUsesElastiCacheMasterWithoutMaster() {

    assumeThat(/*from   w  w w  .  j a v  a  2s.c o m*/
            String.format("No replicas connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
            connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder()
            .readFrom(ReadFrom.MASTER).build();

    RedisStaticMasterReplicaConfiguration elastiCache = new RedisStaticMasterReplicaConfiguration(
            SettingsUtils.getHost(), SettingsUtils.getPort() + 1);

    LettuceConnectionFactory factory = new LettuceConnectionFactory(elastiCache, configuration);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();

    try {
        connection.ping();
        fail("Expected RedisException: Master is currently unknown");
    } catch (RedisSystemException e) {

        assertThat(e.getCause(), is(instanceOf(RedisException.class)));
        assertThat(e.getCause().getMessage(), containsString("Master is currently unknown"));
    } finally {
        connection.close();
    }

    factory.destroy();
}

From source file:org.springframework.data.redis.connection.lettuce.LettuceConnectionFactoryTests.java

@Test // DATAREDIS-580, DATAREDIS-869
public void factoryUsesMasterReplicaConnections() {

    assumeThat(//w w w. jav  a 2s.c  om
            String.format("No replicas connected to %s:%s.", SettingsUtils.getHost(), SettingsUtils.getPort()),
            connection.info("replication").getProperty("connected_slaves", "0").compareTo("0") > 0, is(true));

    LettuceClientConfiguration configuration = LettuceTestClientConfiguration.builder().readFrom(ReadFrom.SLAVE)
            .build();

    LettuceConnectionFactory factory = new LettuceConnectionFactory(SettingsUtils.standaloneConfiguration(),
            configuration);
    factory.afterPropertiesSet();

    RedisConnection connection = factory.getConnection();

    try {
        assertThat(connection.ping(), is(equalTo("PONG")));
        assertThat(connection.info().getProperty("role"), is(equalTo("slave")));
    } finally {
        connection.close();
    }

    factory.destroy();
}