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

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

Introduction

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

Prototype

public LettuceConnectionFactory(RedisClusterConfiguration clusterConfiguration,
        LettuceClientConfiguration clientConfig) 

Source Link

Document

Constructs a new LettuceConnectionFactory instance using the given RedisClusterConfiguration and LettuceClientConfiguration .

Usage

From source file:org.springframework.boot.autoconfigure.data.redis.LettuceConnectionConfiguration.java

private LettuceConnectionFactory createLettuceConnectionFactory(
        LettuceClientConfiguration clientConfiguration) {
    if (getSentinelConfig() != null) {
        return new LettuceConnectionFactory(getSentinelConfig(), clientConfiguration);
    }/*from   w  w w  . ja v a  2  s .co m*/
    if (getClusterConfiguration() != null) {
        return new LettuceConnectionFactory(getClusterConfiguration(), clientConfiguration);
    }
    return new LettuceConnectionFactory(getStandaloneConfig(), clientConfiguration);
}

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

@Before
public void setUp() {

    factory = new LettuceConnectionFactory(SettingsUtils.getHost(), SettingsUtils.getPort());
    factory.setClientResources(LettuceTestClientResources.getSharedClientResources());
    factory.afterPropertiesSet();/*from  w w w  .j a  v  a 2 s.  c o m*/
    factory.setShutdownTimeout(0);
    connection = new DefaultStringRedisConnection(factory.getConnection());
}

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

@Test
public void testSelectDb() {

    LettuceConnectionFactory factory2 = new LettuceConnectionFactory(SettingsUtils.getHost(),
            SettingsUtils.getPort());// w  ww . java  2  s . co  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 // 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  2s.  co 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);//from w  ww.  ja  va  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();/*  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.jav a2  s .  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.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  ww . ja v  a2s.com
            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();
}

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

@Test // DATAREDIS-576
public void connectionAppliesClientName() {

    LettuceClientConfiguration configuration = LettuceClientConfiguration.builder()
            .clientResources(LettuceTestClientResources.getSharedClientResources()).clientName("clientName")
            .build();/*from  w w  w.  java 2 s .co m*/

    LettuceConnectionFactory factory = new LettuceConnectionFactory(new RedisStandaloneConfiguration(),
            configuration);
    factory.setShareNativeConnection(false);
    factory.afterPropertiesSet();

    ConnectionFactoryTracker.add(factory);

    RedisConnection connection = factory.getConnection();

    assertThat(connection.getClientName(), is(equalTo("clientName")));
    connection.close();
}