@Override public void prepare(Map map, TopologyContext topologyContext, OutputCollector outputCollector) { // instantiate a redis connection RedisClient client = new RedisClient("localhost", 6379); // initiate the actual connection redis = client.connect(); }
@Test public void sentinelConnect() throws Exception { RedisClient client = new RedisClient(RedisURI.Builder.redis(TestSettings.host(), TestSettings.port()).build()); RedisSentinelAsyncCommands<String, String> connection = client.connectSentinelAsync(); assertThat(connection.ping().get()).isEqualTo("PONG"); connection.close(); FastShutdown.shutdown(client); }
public static void main(String[] args) { // Syntax: redis://[password@]host[:port][/databaseNumber] RedisClient redisClient = new RedisClient(RedisURI.create("redis://password@localhost:6379/0")); RedisConnection<String, String> connection = redisClient.connect(); System.out.println("Connected to Redis"); connection.set("foo", "bar"); String value = connection.get("foo"); System.out.println(value); connection.close(); redisClient.shutdown(); }
@Test public void sentinelConnectWrongMaster() throws Exception { RedisClient client = new RedisClient( RedisURI.Builder.sentinel(TestSettings.host(), 1234, "nonexistent") .withSentinel(TestSettings.host()) .build()); try { client.connect(); fail("missing RedisConnectionException"); } catch (RedisConnectionException e) { } FastShutdown.shutdown(client); }
public RedisConnection<String, String> connect() { RedisConnection<String, String> connection = redisClient.connect(); String password = getConfig().getString("redis.password"); if (password != null) connection.auth(password); return connection; }
@Override public void onDisable() { // Clean up if (redisClient != null) { redisClient.shutdown(); redisClient = null; } }
protected void initializeRedis() { ConfigurationSection config = getConfig().getConfigurationSection("redis"); String host = config.getString("host", "localhost"); int port = config.getInt("port", 6379); int timeout = config.getInt("timeout", 5000); redisClient = new RedisClient(host, port); redisClient.setDefaultTimeout(timeout, TimeUnit.MILLISECONDS); log("Connecting to Redis at %s:%d", host, port); log(" Ping? %s", redis().ping()); log(" Found %d keys", redis().dbsize()); }
@Test public void sentinelConnectWith() throws Exception { RedisClient client = new RedisClient( RedisURI.Builder.sentinel(TestSettings.host(), 1234, MASTER_ID) .withSentinel(TestSettings.host()) .build()); RedisSentinelAsyncCommands<String, String> sentinelConnection = client.connectSentinelAsync(); assertThat(sentinelConnection.ping().get()).isEqualTo("PONG"); sentinelConnection.close(); RedisConnection<String, String> connection2 = client.connect().sync(); assertThat(connection2.ping()).isEqualTo("PONG"); connection2.quit(); Wait.untilTrue(connection2::isOpen).waitOrTimeout(); assertThat(connection2.ping()).isEqualTo("PONG"); connection2.close(); FastShutdown.shutdown(client); }
@Override public void disconnect() throws IOException { client.shutdown(); connection = null; }
@Override public void connect() throws IOException { client = new RedisClient(host, port); connection = client.connect(); connection.select(dbIndex); }