Java Code Examples for io.lettuce.core.RedisClient#connect()

The following examples show how to use io.lettuce.core.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: RedisBackedJobServiceTest.java    From feast with Apache License 2.0 7 votes vote down vote up
@Test
public void shouldRecoverIfRedisConnectionIsLost() {
  RedisClient client = RedisClient.create(RedisURI.create("localhost", REDIS_PORT));
  RedisBackedJobService jobService =
      new RedisBackedJobService(client.connect(new ByteArrayCodec()));
  jobService.get("does not exist");
  redis.stop();
  try {
    jobService.get("does not exist");
  } catch (Exception e) {
    // pass, this should fail, and return a broken connection to the pool
  }
  redis.start();
  jobService.get("does not exist");
  client.shutdown();
}
 
Example 2
Source File: TracingLettuce51Test.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void sync() {
  RedisClient client = RedisClient.create("redis://localhost");

  StatefulRedisConnection<String, String> connection =
      new TracingStatefulRedisConnection<>(client.connect(),
          new TracingConfiguration.Builder(mockTracer).build());
  RedisCommands<String, String> commands = connection.sync();

  assertEquals("OK", commands.set("key", "value"));
  assertEquals("value", commands.get("key"));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 3
Source File: TracingLettuce50Test.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void sync() {
  RedisClient client = RedisClient.create("redis://localhost");

  StatefulRedisConnection<String, String> connection =
      new TracingStatefulRedisConnection<>(client.connect(),
          new TracingConfiguration.Builder(mockTracer).build());
  RedisCommands<String, String> commands = connection.sync();

  assertEquals("OK", commands.set("key", "value"));
  assertEquals("value", commands.get("key"));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 4
Source File: LettuceController.java    From skywalking with Apache License 2.0 6 votes vote down vote up
@RequestMapping("/lettuce-case")
@ResponseBody
public String lettuceCase() {
    RedisClient redisClient = RedisClient.create("redis://" + address);
    StatefulRedisConnection<String, String> connection0 = redisClient.connect();
    RedisCommands<String, String> syncCommand = connection0.sync();
    syncCommand.get("key");

    StatefulRedisConnection<String, String> connection1 = redisClient.connect();
    RedisAsyncCommands<String, String> asyncCommands = connection1.async();
    asyncCommands.setAutoFlushCommands(false);
    List<RedisFuture<?>> futures = new ArrayList<>();
    futures.add(asyncCommands.set("key0", "value0"));
    futures.add(asyncCommands.set("key1", "value1"));
    asyncCommands.flushCommands();
    LettuceFutures.awaitAll(5, TimeUnit.SECONDS, futures.toArray(new RedisFuture[futures.size()]));

    connection0.close();
    connection1.close();
    redisClient.shutdown();
    return "Success";
}
 
Example 5
Source File: TracingLettuce52Test.java    From java-redis-client with Apache License 2.0 6 votes vote down vote up
@Test
public void sync() {
  RedisClient client = RedisClient.create("redis://localhost");

  StatefulRedisConnection<String, String> connection =
      new TracingStatefulRedisConnection<>(client.connect(),
          new TracingConfiguration.Builder(mockTracer).build());
  RedisCommands<String, String> commands = connection.sync();

  assertEquals("OK", commands.set("key", "value"));
  assertEquals("value", commands.get("key"));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 6
Source File: ProtobufRedisLoadingCache.java    From curiostack with MIT License 5 votes vote down vote up
private <K extends Message, V extends Message> RemoteCache<K, V> createRedisRemoteCache(
    String name, RedisClient redisClient, K keyPrototype, V valuePrototype) {
  StatefulRedisConnection<K, V> connection =
      redisClient.connect(
          new ProtobufRedisCodec<>(
              (name + ":").getBytes(StandardCharsets.UTF_8), keyPrototype, valuePrototype));
  return new RedisRemoteCache<>(connection.async(), name, meterRegistry);
}
 
Example 7
Source File: TracingLettuce51Test.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void async_continue_span() throws Exception {
  final MockSpan parent = mockTracer.buildSpan("test").start();
  try (Scope ignored = mockTracer.activateSpan(parent)) {
    Span activeSpan = mockTracer.activeSpan();

    RedisClient client = RedisClient.create("redis://localhost");

    StatefulRedisConnection<String, String> connection =
        new TracingStatefulRedisConnection<>(client.connect(),
            new TracingConfiguration.Builder(mockTracer).build());

    RedisAsyncCommands<String, String> commands = connection.async();

    assertEquals("OK",
        commands.set("key2", "value2").toCompletableFuture().thenApply(s -> {
          assertSame(activeSpan, mockTracer.activeSpan());
          return s;
        }).get(15, TimeUnit.SECONDS));

    connection.close();

    client.shutdown();
  }
  parent.finish();
  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 8
Source File: TracingLettuce50Test.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void async_continue_span() throws Exception {
  final MockSpan parent = mockTracer.buildSpan("test").start();
  try (Scope ignored = mockTracer.activateSpan(parent)) {
    Span activeSpan = mockTracer.activeSpan();

    RedisClient client = RedisClient.create("redis://localhost");

    StatefulRedisConnection<String, String> connection =
        new TracingStatefulRedisConnection<>(client.connect(),
            new TracingConfiguration.Builder(mockTracer).build());

    RedisAsyncCommands<String, String> commands = connection.async();

    assertEquals("OK",
        commands.set("key2", "value2").toCompletableFuture().thenApply(s -> {
          assertSame(activeSpan, mockTracer.activeSpan());
          return s;
        }).get(15, TimeUnit.SECONDS));

    connection.close();

    client.shutdown();
  }
  parent.finish();
  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 9
Source File: TracingLettuce52Test.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void async_continue_span() throws Exception {
  final MockSpan parent = mockTracer.buildSpan("test").start();
  try (Scope ignored = mockTracer.activateSpan(parent)) {
    Span activeSpan = mockTracer.activeSpan();

    RedisClient client = RedisClient.create("redis://localhost");

    StatefulRedisConnection<String, String> connection =
        new TracingStatefulRedisConnection<>(client.connect(),
            new TracingConfiguration.Builder(mockTracer).build());

    RedisAsyncCommands<String, String> commands = connection.async();

    assertEquals("OK",
        commands.set("key2", "value2").toCompletableFuture().thenApply(s -> {
          assertSame(activeSpan, mockTracer.activeSpan());
          return s;
        }).get(15, TimeUnit.SECONDS));

    connection.close();

    client.shutdown();
  }
  parent.finish();
  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 10
Source File: RequiresRedisServer.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
@Override
protected void before() throws Throwable {

	try (Socket socket = new Socket()) {
		socket.setTcpNoDelay(true);
		socket.setSoLinger(true, 0);
		socket.connect(new InetSocketAddress(host, port), timeout);
	} catch (Exception e) {
		throw new AssumptionViolatedException(String.format("Seems as Redis is not running at %s:%s.", host, port), e);
	}

	if (NO_VERSION.equals(requiredVersion)) {
		return;
	}

	RedisClient redisClient = RedisClient.create(ManagedClientResources.getClientResources(),
			RedisURI.create(host, port));

	try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {

		String infoServer = connection.sync().info("server");
		String redisVersion = LettuceConverters.stringToProps().convert(infoServer).getProperty("redis_version");
		Version runningVersion = Version.parse(redisVersion);

		if (runningVersion.isLessThan(requiredVersion)) {
			throw new AssumptionViolatedException(String
					.format("This test requires Redis version %s but you run version %s", requiredVersion, runningVersion));
		}

	} finally {
		redisClient.shutdown(Duration.ZERO, Duration.ZERO);
	}
}
 
Example 11
Source File: RedisServerExtensionTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void shouldHaveAccessToARedisServer(@RedisPort Integer port) {
  assertNotNull(port);
  assertTrue(port >= 32768, "Port must be more than 32768, was:" + port);
  RedisClient client = RedisClient.create(RedisURI.create(InetAddress.getLoopbackAddress().getHostAddress(), port));
  try (StatefulRedisConnection<String, String> conn = client.connect()) {
    assertTrue(conn.isOpen());
  }
}
 
Example 12
Source File: RequiresRedisSentinel.java    From spring-data-examples with Apache License 2.0 5 votes vote down vote up
private boolean isAvailable(RedisNode node) {

		RedisClient redisClient = RedisClient.create(ManagedClientResources.getClientResources(),
				RedisURI.create(node.getHost(), node.getPort()));

		try (StatefulRedisConnection<String, String> connection = redisClient.connect()) {
			connection.sync().ping();
		} catch (Exception e) {
			return false;
		} finally {
			redisClient.shutdown(Duration.ZERO, Duration.ZERO);
		}

		return true;
	}
 
Example 13
Source File: RedisServerExtensionTest.java    From cava with Apache License 2.0 5 votes vote down vote up
@Test
void shouldHaveAccessToARedisServer(@RedisPort Integer port) {
  assertNotNull(port);
  assertTrue(port >= 32768, "Port must be more than 32768, was:" + port);
  RedisClient client = RedisClient.create(RedisURI.create(InetAddress.getLoopbackAddress().getHostAddress(), port));
  try (StatefulRedisConnection<String, String> conn = client.connect()) {
    assertTrue(conn.isOpen());
  }
}
 
Example 14
Source File: LettuceBenchmark.java    From apm-agent-java with Apache License 2.0 5 votes vote down vote up
@Override
public void setUp(Blackhole blackhole) throws IOException {
    super.setUp(blackhole);
    int redisPort = getAvailablePort();
    server = RedisServer.builder()
        .setting("bind 127.0.0.1")
        .port(redisPort)
        .build();
    server.start();
    RedisClient client = RedisClient.create(RedisURI.create("localhost", redisPort));
    connection = client.connect();
    sync = connection.sync();
    sync.set("foo", "bar");
}
 
Example 15
Source File: RedisKeyValueStoreTest.java    From incubator-tuweni with Apache License 2.0 5 votes vote down vote up
@Test
void testPutAndGet(@RedisPort Integer redisPort) throws Exception {
  KeyValueStore<Bytes, Bytes> store = RedisKeyValueStore
      .open(redisPort, Function.identity(), Function.identity(), Function.identity(), Function.identity());
  AsyncCompletion completion = store.putAsync(Bytes.of(123), Bytes.of(10, 12, 13));
  completion.join();
  Bytes value = store.getAsync(Bytes.of(123)).get();
  assertNotNull(value);
  assertEquals(Bytes.of(10, 12, 13), value);
  RedisClient client =
      RedisClient.create(RedisURI.create(InetAddress.getLoopbackAddress().getHostAddress(), redisPort));
  try (StatefulRedisConnection<Bytes, Bytes> conn = client.connect(new RedisBytesCodec())) {
    assertEquals(Bytes.of(10, 12, 13), conn.sync().get(Bytes.of(123)));
  }
}
 
Example 16
Source File: RedisConfig.java    From Mastering-Distributed-Tracing with MIT License 4 votes vote down vote up
@Bean
public StatefulRedisConnection<String, String> redisConn() {
    RedisClient client = RedisClient.create("redis://localhost");
    return new TracingStatefulRedisConnection<>( //
            client.connect(), tracer, false);
}
 
Example 17
Source File: Lettuce5InstrumentationTest.java    From apm-agent-java with Apache License 2.0 4 votes vote down vote up
@Before
public void setUpLettuce() {
    RedisClient client = RedisClient.create(RedisURI.create("localhost", redisPort));
    connection = client.connect();
    reporter.disableDestinationAddressCheck();
}
 
Example 18
Source File: RedisBackedCache.java    From testcontainers-java with MIT License 4 votes vote down vote up
public RedisBackedCache(String hostname, Integer port) {
    RedisClient client = RedisClient.create(String.format("redis://%s:%d/0", hostname, port));
    connection = client.connect();
}
 
Example 19
Source File: RedisBackedCache.java    From testcontainers-java with MIT License 4 votes vote down vote up
public RedisBackedCache(String hostname, Integer port) {
    RedisClient client = RedisClient.create(String.format("redis://%s:%d/0", hostname, port));
    connection = client.connect();
}
 
Example 20
Source File: RedisBackedCache.java    From testcontainers-java with MIT License 4 votes vote down vote up
public RedisBackedCache(String hostname, Integer port) {
    RedisClient client = RedisClient.create(String.format("redis://%s:%d/0", hostname, port));
    connection = client.connect();
}