@Override
  protected void doSubscribe(String eventName, final SettableFuture<?> future) {
    List<SettableFuture<?>> futures = new ArrayList<SettableFuture<?>>();

    for (RedisConnection conn : connections) {
      SettableFuture<?> newFuture = SettableFuture.create();
      conn.subscribe(eventName, newFuture);
      futures.add(newFuture);
    }

    final ListenableFuture<?> combined = Futures.allAsList(futures);

    combined.addListener(
        new Runnable() {
          @Override
          public void run() {
            try {
              combined.get();
              future.set(null);
            } catch (InterruptedException e) {
              future.setException(e);
            } catch (ExecutionException e) {
              future.setException(e.getCause());
            }
          }
        },
        getExecutorService());
  }
  @Override
  public void stop() {
    super.stop();

    for (RedisConnection conn : connections) {
      conn.stop();
    }
  }
 @Test
 public void connectToRedisUsingSentinelWithReconnect() throws Exception {
   RedisConnection<String, String> connect = sentinelClient.connect().sync();
   connect.ping();
   connect.quit();
   connect.ping();
   connect.close();
 }
Exemple #4
0
  @Test
  public void test() throws Exception {
    RedisConnection<String, Object> connection = client.connect(new SerializedObjectCodec());
    List<String> list = list("one", "two");
    connection.set(key, list);
    assertThat(connection.get(key)).isEqualTo(list);

    connection.close();
  }
Exemple #5
0
  @Test
  public void testByteCodec() throws Exception {
    RedisConnection<byte[], byte[]> connection = client.connect(new ByteArrayCodec());
    String value = "üöäü+#";
    connection.set(key.getBytes(), value.getBytes());
    assertThat(connection.get(key.getBytes())).isEqualTo(value.getBytes());

    List<byte[]> keys = connection.keys(key.getBytes());
    assertThat(keys).contains(key.getBytes());
  }
Exemple #6
0
  @Test
  public void testGzipompressedJavaSerializer() throws Exception {
    RedisConnection<String, Object> connection =
        client.connect(
            CompressionCodec.valueCompressor(
                new SerializedObjectCodec(), CompressionCodec.CompressionType.GZIP));
    List<String> list = list("one", "two");
    connection.set(key, list);
    assertThat(connection.get(key)).isEqualTo(list);

    connection.close();
  }
  @Override
  protected boolean doPublish(String name, Event event, String eventString) throws IOException {
    if (connections.size() == 0) {
      return false;
    }

    RedisConnection conn = null;
    try {
      conn = connections.get(index);
    } catch (IndexOutOfBoundsException e) {
      if (connections.size() > 0) {
        conn = connections.get(0);
      }
    }

    index = (index + 1) % connections.size();

    if (conn != null) {
      return conn.publish(name, eventString);
    }

    return false;
  }
  public void reconnect() {
    List<RedisConnection> newConnections = new ArrayList<RedisConnection>();

    for (String host : REDIS_HOST.get().trim().split("\\s*,\\s*")) {
      String[] parts = host.split(":");
      if (parts.length > 2) {
        throw new IllegalArgumentException(
            "Invalid redis host [" + host + "] should be in host:port format");
      }

      String hostName = parts[0];
      int port = Protocol.DEFAULT_PORT;

      if (parts.length > 1) {
        try {
          port = Integer.parseInt(parts[1]);
        } catch (NumberFormatException e) {
          throw new IllegalArgumentException(
              "Invalid redis host [" + host + "] should be in host:port format", e);
        }
      }

      newConnections.add(new RedisConnection(this, hostName, port));
    }

    List<RedisConnection> oldConnections = connections;
    connections = newConnections;

    for (RedisConnection conn : newConnections) {
      getExecutorService().submit(conn);
    }

    for (RedisConnection conn : oldConnections) {
      conn.stop();
    }
  }
  @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
 protected void disconnect() {
   for (RedisConnection conn : connections) {
     conn.disconnect();
   }
 }
 @Override
 protected void doUnsubscribe(String eventName) {
   for (RedisConnection conn : connections) {
     conn.unsubscribe(eventName);
   }
 }