Java Code Examples for io.lettuce.core.api.StatefulRedisConnection#close()

The following examples show how to use io.lettuce.core.api.StatefulRedisConnection#close() . 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: RedisQueue.java    From NetDiscovery with Apache License 2.0 7 votes vote down vote up
@Override
protected void pushWhenNoDuplicate(Request request) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        commands.lpush(request.getSpiderName(), request.getUrl());

        if (hasExtraRequestInfo(request)) {
            String field = DigestUtils.sha1Hex(request.getUrl());
            String value = SerializableUtils.toJson(request);
            commands.hset((ITEM_PREFIX + request.getUrl()), field, value);
        }

    } finally {
        connection.close();
    }

}
 
Example 2
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 3
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isDuplicate(Request request) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();

    try {
        if (request.isCheckDuplicate()) {
            return commands.sadd(getSetKey(request), request.getUrl()) == 0;
        } else {
            commands.sadd(getSetKey(request), request.getUrl());
            return false;
        }
    } finally {
        connection.close();
    }
}
 
Example 4
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 5
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 6
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 7
Source File: RedisPriorityQueue.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
@Override
protected void pushWhenNoDuplicate(Request request) {

    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();

    try {
        if (request.getPriority() > 0) {
            commands.zadd(getZsetPriorityKey(request.getSpiderName()), request.getPriority(), request.getUrl());
        } else {
            commands.lpush(getQueueNormalKey(request.getSpiderName()), request.getUrl());
        }

        setExtrasInItem(commands, request);
    } finally {
        connection.close();
    }
}
 
Example 8
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized Request poll(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        String url = commands.lpop(getQueueKey(spiderName));
        if (url == null) {
            return null;
        }

        String key = ITEM_PREFIX + url;
        String field = DigestUtils.sha1Hex(url);
        String result = commands.hget(key, field);

        if (result != null) {

            return SerializableUtils.fromJson(result, Request.class);
        }

        return new Request(url);
    } finally {
        connection.close();
    }
}
 
Example 9
Source File: RedisPriorityQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Request poll(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        String url = getRequest(commands, spiderName);
        if (Preconditions.isBlank(url)) {
            return null;
        }

        return getExtrasInItem(commands, url, spiderName);
    } finally {
        connection.close();
    }
}
 
Example 10
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 11
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public int getTotalRequests(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        Long size = commands.scard(getSetKey(spiderName));
        return size.intValue();
    } finally {
        connection.close();
    }
}
 
Example 12
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 13
Source File: RedisQueue.java    From NetDiscovery with Apache License 2.0 5 votes vote down vote up
@Override
public int getLeftRequests(String spiderName) {
    StatefulRedisConnection<String, String> connection = redisClient.connect();
    RedisCommands<String, String> commands = connection.sync();
    try {
        Long size = commands.llen(getQueueKey(spiderName));
        return size.intValue();
    } finally {
        connection.close();
    }
}
 
Example 14
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 15
Source File: TracingLettuce52Test.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Test
public void async() throws Exception {
  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").get(15, TimeUnit.SECONDS));

  assertEquals("value2", commands.get("key2").get(15, TimeUnit.SECONDS));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 16
Source File: TracingLettuce50Test.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Test
public void async() throws Exception {
  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").get(15, TimeUnit.SECONDS));

  assertEquals("value2", commands.get("key2").get(15, TimeUnit.SECONDS));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}
 
Example 17
Source File: TracingLettuce51Test.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Test
public void async() throws Exception {
  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").get(15, TimeUnit.SECONDS));

  assertEquals("value2", commands.get("key2").get(15, TimeUnit.SECONDS));

  connection.close();

  client.shutdown();

  List<MockSpan> spans = mockTracer.finishedSpans();
  assertEquals(2, spans.size());
}