Java Code Examples for org.redisson.client.RedisClient#connect()

The following examples show how to use org.redisson.client.RedisClient#connect() . 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: RedissonConnectionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public RedisSentinelConnection getSentinelConnection() {
    if (!redisson.getConfig().isSentinelConfig()) {
        throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
    }
    
    SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
    for (RedisClient client : manager.getSentinels()) {
        org.redisson.client.RedisConnection connection = client.connect();
        try {
            String res = connection.sync(RedisCommands.PING);
            if ("pong".equalsIgnoreCase(res)) {
                return new RedissonSentinelConnection(connection);
            }
        } catch (Exception e) {
            log.warn("Can't connect to " + client, e);
            connection.closeAsync();
        }
    }
    
    throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
 
Example 2
Source File: SpringNamespaceWikiTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRedisClient() throws Exception {
    RedisRunner.RedisProcess run = new RedisRunner()
            .requirepass("do_not_use_if_it_is_not_set")
            .nosave()
            .randomDir()
            .run();
    try {
        ClassPathXmlApplicationContext context
                = new ClassPathXmlApplicationContext("classpath:org/redisson/spring/support/namespace_wiki_redis_client.xml");
        RedisClient redisClient = context.getBean(RedisClient.class);
        RedisConnection connection = redisClient.connect();
        Map<String, String> info = connection.sync(RedisCommands.INFO_ALL);
        assertThat(info, notNullValue());
        assertThat(info, not(info.isEmpty()));
        ((ConfigurableApplicationContext) context).close();
    } finally {
        run.stop();
    }
}
 
Example 3
Source File: RedissonConnectionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public RedisSentinelConnection getSentinelConnection() {
    if (!redisson.getConfig().isSentinelConfig()) {
        throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
    }
    
    SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
    for (RedisClient client : manager.getSentinels()) {
        org.redisson.client.RedisConnection connection = client.connect();
        try {
            String res = connection.sync(RedisCommands.PING);
            if ("pong".equalsIgnoreCase(res)) {
                return new RedissonSentinelConnection(connection);
            }
        } catch (Exception e) {
            log.warn("Can't connect to " + client, e);
            connection.closeAsync();
        }
    }
    
    throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
 
Example 4
Source File: RedissonConnectionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public RedisSentinelConnection getSentinelConnection() {
    if (!redisson.getConfig().isSentinelConfig()) {
        throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
    }
    
    SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
    for (RedisClient client : manager.getSentinels()) {
        org.redisson.client.RedisConnection connection = client.connect();
        try {
            String res = connection.sync(RedisCommands.PING);
            if ("pong".equalsIgnoreCase(res)) {
                return new RedissonSentinelConnection(connection);
            }
        } catch (Exception e) {
            log.warn("Can't connect to " + client, e);
            connection.closeAsync();
        }
    }
    
    throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
 
Example 5
Source File: RedissonConnectionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public RedisSentinelConnection getSentinelConnection() {
    if (!redisson.getConfig().isSentinelConfig()) {
        throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
    }
    
    SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
    for (RedisClient client : manager.getSentinels()) {
        org.redisson.client.RedisConnection connection = client.connect();
        try {
            String res = connection.sync(RedisCommands.PING);
            if ("pong".equalsIgnoreCase(res)) {
                return new RedissonSentinelConnection(connection);
            }
        } catch (Exception e) {
            log.warn("Can't connect to " + client, e);
            connection.closeAsync();
        }
    }
    
    throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
 
Example 6
Source File: RedissonConnectionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public RedisSentinelConnection getSentinelConnection() {
    if (!redisson.getConfig().isSentinelConfig()) {
        throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
    }
    
    SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
    for (RedisClient client : manager.getSentinels()) {
        org.redisson.client.RedisConnection connection = client.connect();
        try {
            String res = connection.sync(RedisCommands.PING);
            if ("pong".equalsIgnoreCase(res)) {
                return new RedissonSentinelConnection(connection);
            }
        } catch (Exception e) {
            log.warn("Can't connect to " + client, e);
            connection.closeAsync();
        }
    }
    
    throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
 
Example 7
Source File: RedissonConnectionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public RedisSentinelConnection getSentinelConnection() {
    if (!redisson.getConfig().isSentinelConfig()) {
        throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
    }
    
    SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
    for (RedisClient client : manager.getSentinels()) {
        org.redisson.client.RedisConnection connection = client.connect();
        try {
            String res = connection.sync(RedisCommands.PING);
            if ("pong".equalsIgnoreCase(res)) {
                return new RedissonSentinelConnection(connection);
            }
        } catch (Exception e) {
            log.warn("Can't connect to " + client, e);
            connection.closeAsync();
        }
    }
    
    throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
 
Example 8
Source File: RedissonConnectionFactory.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Override
public RedisSentinelConnection getSentinelConnection() {
    if (!redisson.getConfig().isSentinelConfig()) {
        throw new InvalidDataAccessResourceUsageException("Redisson is not in Sentinel mode");
    }
    
    SentinelConnectionManager manager = ((SentinelConnectionManager)((Redisson)redisson).getConnectionManager());
    for (RedisClient client : manager.getSentinels()) {
        org.redisson.client.RedisConnection connection = client.connect();
        try {
            String res = connection.sync(RedisCommands.PING);
            if ("pong".equalsIgnoreCase(res)) {
                return new RedissonSentinelConnection(connection);
            }
        } catch (Exception e) {
            log.warn("Can't connect to " + client, e);
            connection.closeAsync();
        }
    }
    
    throw new InvalidDataAccessResourceUsageException("Sentinels are not found");
}
 
Example 9
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}
 
Example 10
Source File: RedissonIntegrationTest.java    From tutorials with MIT License 5 votes vote down vote up
@Test
public void givenLowLevelRedisCommands_thenExecuteLowLevelCommandsOnRedis(){
    RedisClientConfig redisClientConfig = new RedisClientConfig();
    redisClientConfig.setAddress("localhost", 6379);
    RedisClient client = RedisClient.create(redisClientConfig);
    RedisConnection conn = client.connect();
    conn.sync(StringCodec.INSTANCE, RedisCommands.SET, "test", 0);

    String testValue = conn.sync(StringCodec.INSTANCE, RedisCommands.GET, "test");

    conn.closeAsync();
    client.shutdown();

    assertTrue(testValue.equals("0"));
}
 
Example 11
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}
 
Example 12
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public void restart(int startTimeout) {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    
    Executors.newScheduledThreadPool(1).schedule(() -> {
        try {
            redisProcess = runner.run().redisProcess;
        } catch (Exception e) {
            e.printStackTrace();
        }
    }, startTimeout, TimeUnit.SECONDS);
}
 
Example 13
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}
 
Example 14
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}
 
Example 15
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}
 
Example 16
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}
 
Example 17
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}
 
Example 18
Source File: RedisRunner.java    From redisson with Apache License 2.0 5 votes vote down vote up
public int stop() {
    if (runner.isNosave() && !runner.isRandomDir()) {
        RedisClient c = createDefaultRedisClientInstance();
        RedisConnection connection = c.connect();
        try {
            connection.async(new RedisStrictCommand<Void>("SHUTDOWN", "NOSAVE", new VoidReplayConvertor()))
                    .await(3, TimeUnit.SECONDS);
        } catch (InterruptedException interruptedException) {
            //shutdown via command failed, lets wait and kill it later.
        }
        c.shutdown();
        connection.closeAsync().syncUninterruptibly();
    }
    Process p = redisProcess;
    p.destroy();
    boolean normalTermination = false;
    try {
        normalTermination = p.waitFor(5, TimeUnit.SECONDS);
    } catch (InterruptedException ex) {
        //OK lets hurry up by force kill;
    }
    if (!normalTermination) {
        p = p.destroyForcibly();
    }
    cleanup();
    int exitCode = p.exitValue();
    return exitCode == 1 && RedissonRuntimeEnvironment.isWindows ? 0 : exitCode;
}