Java Code Examples for org.redisson.api.RedissonClient#shutdown()

The following examples show how to use org.redisson.api.RedissonClient#shutdown() . 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: RedissonBoundedBlockingQueueTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testPollAsyncCancel() {
    Config config = createConfig();
    config.useSingleServer().setConnectionMinimumIdleSize(1).setConnectionPoolSize(1);

    RedissonClient redisson = Redisson.create(config);
    redisson.getKeys().flushall();
    
    RBoundedBlockingQueue<Integer> queue1 = redisson.getBoundedBlockingQueue("queue:pollany");
    assertThat(queue1.trySetCapacity(15)).isTrue();
    for (int i = 0; i < 10; i++) {
        RFuture<Integer> f = queue1.pollAsync(1, TimeUnit.SECONDS);
        f.cancel(true);
    }
    assertThat(queue1.add(1)).isTrue();
    assertThat(queue1.add(2)).isTrue();
    assertThat(queue1.size()).isEqualTo(2);
    
    redisson.shutdown();
}
 
Example 2
Source File: StreamExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    RStream<String, String> stream = redisson.getStream("test");
    stream.createGroup("testGroup");
    
    StreamMessageId id1 = stream.add("1", "1");
    StreamMessageId id2 = stream.add("2", "2");
    
    // contains 2 elements
    Map<StreamMessageId, Map<String, String>> map1 = stream.readGroup("testGroup", "consumer1");

    // ack messages
    stream.ack("testGroup", id1, id2);
    
    StreamMessageId id3 = stream.add("3", "3");
    StreamMessageId id4 = stream.add("4", "4");
    
    // contains next 2 elements
    Map<StreamMessageId, Map<String, String>> map2 = stream.readGroup("testGroup", "consumer2");

    PendingResult pi = stream.listPending("testGroup");
    
    redisson.shutdown();
}
 
Example 3
Source File: TopicExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    CountDownLatch latch = new CountDownLatch(1);
    
    RTopic topic = redisson.getTopic("topic2");
    topic.addListener(String.class, new MessageListener<String>() {
        @Override
        public void onMessage(CharSequence channel, String msg) {
            latch.countDown();
        }
    });
    
    topic.publish("msg");
    latch.await();
    
    redisson.shutdown();
}
 
Example 4
Source File: RateLimiterExamples.java    From redisson-examples with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RRateLimiter limiter = redisson.getRateLimiter("myLimiter");
    // one permit per 2 seconds
    limiter.trySetRate(RateType.OVERALL, 1, 2, RateIntervalUnit.SECONDS);
    
    CountDownLatch latch = new CountDownLatch(2);
    limiter.acquire(1);
    latch.countDown();

    Thread t = new Thread(() -> {
        limiter.acquire(1);
        
        latch.countDown();
    });
    t.start();
    t.join();
    
    latch.await();
    
    redisson.shutdown();
}
 
Example 5
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void test() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(2);

    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic topic1 = redisson1.getTopic("topic");
    topic1.addListener(Message.class, (channel, msg) -> {
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });

    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic topic2 = redisson2.getTopic("topic");
    topic2.addListener(Message.class, (channel, msg) -> {
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    topic2.publish(new Message("123"));

    messageRecieved.await();

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example 6
Source File: RedissonRedLockTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testLockSuccess() throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    
    RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
    
    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    RLock lock3 = client2.getLock("lock3");
    
    testLock(lock1, lock2, lock3, lock1);
    testLock(lock1, lock2, lock3, lock2);
    testLock(lock1, lock2, lock3, lock3);
    
    client1.shutdown();
    client2.shutdown();
    
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
}
 
Example 7
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveByInstance() throws InterruptedException {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic topic1 = redisson.getTopic("topic1");
    MessageListener listener = new MessageListener() {
        @Override
        public void onMessage(CharSequence channel, Object msg) {
            Assert.fail();
        }
    };
    
    topic1.addListener(Message.class, listener);

    topic1 = redisson.getTopic("topic1");
    topic1.removeListener(listener);
    topic1.publish(new Message("123"));

    redisson.shutdown();
}
 
Example 8
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveAllListeners() throws InterruptedException {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic topic1 = redisson.getTopic("topic1");
    AtomicInteger counter = new AtomicInteger();
    
    for (int i = 0; i < 10; i++) {
        topic1.addListener(Message.class, (channel, msg) -> {
            counter.incrementAndGet();
        });
    }

    topic1 = redisson.getTopic("topic1");
    topic1.removeAllListeners();
    topic1.publish(new Message("123"));

    Thread.sleep(1000);
    assertThat(counter.get()).isZero();
    
    redisson.shutdown();
}
 
Example 9
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testCommandsOrdering() throws InterruptedException {
    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic topic1 = redisson1.getTopic("topic", LongCodec.INSTANCE);
    AtomicBoolean stringMessageReceived = new AtomicBoolean();
    topic1.addListener(Long.class, (channel, msg) -> {
        assertThat(msg).isEqualTo(123);
        stringMessageReceived.set(true);
    });
    topic1.publish(123L);

    await().atMost(Duration.ONE_SECOND).untilTrue(stringMessageReceived);

    redisson1.shutdown();
}
 
Example 10
Source File: ConcurrentRedissonSortedSetTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testAdd_SingleInstance() throws InterruptedException {
    final String name = "testAdd_SingleInstance";

    RedissonClient r = BaseTest.createInstance();
    RSortedSet<Integer> map = r.getSortedSet(name);
    map.clear();

    int length = 5000;
    final List<Integer> elements = new ArrayList<Integer>();
    for (int i = 1; i < length + 1; i++) {
        elements.add(i);
    }
    Collections.shuffle(elements);
    final AtomicInteger counter = new AtomicInteger(-1);
    testSingleInstanceConcurrency(length, rc -> {
        RSortedSet<Integer> set = rc.getSortedSet(name);
        int c = counter.incrementAndGet();
        Integer element = elements.get(c);
        Assert.assertTrue(set.add(element));
    });

    Collections.sort(elements);
    Integer[] p = elements.toArray(new Integer[elements.size()]);
    assertThat(map).containsExactly(p);

    map.clear();
    r.shutdown();
}
 
Example 11
Source File: ReadWriteLockExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    final RReadWriteLock lock = redisson.getReadWriteLock("lock");

    lock.writeLock().tryLock();

    Thread t = new Thread() {
        public void run() {
             RLock r = lock.readLock();
             r.lock();

             try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            r.unlock();
        };
    };

    t.start();
    t.join();

    lock.writeLock().unlock();

    t.join();
    
    redisson.shutdown();
}
 
Example 12
Source File: ScoredSortedSetExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    RScoredSortedSet<String> set = redisson.getScoredSortedSet("mySortedSet");
    set.add(10, "1");
    set.add(20, "2");
    set.add(30, "3");
    
    for (String string : set) {
        // iteration through bulk loaded values
    }
    
    Map<String, Double> newValues = new HashMap<>();
    newValues.put("4", 40D);
    newValues.put("5", 50D);
    newValues.put("6", 60D);
    int newValuesAmount = set.addAll(newValues);
    
    Double scoreResult = set.addScore("2", 10);
    set.contains("4");
    set.containsAll(Arrays.asList("3", "4", "5"));
    
    String firstValue = set.first();
    String lastValue = set.last();
    
    String polledFirst = set.pollFirst();
    String polledLast = set.pollLast();

    // use read method to fetch all objects
    Collection<String> allValues = set.readAll();
    
    redisson.shutdown();
}
 
Example 13
Source File: LexSortedSetExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();
    
    RLexSortedSet set = redisson.getLexSortedSet("sortedSet");
    set.add("1");
    set.add("2");
    set.add("3");
    
    for (String string : set) {
        // iteration through bulk loaded values
    }

    Set<String> newValues = new HashSet<>();
    newValues.add("4");
    newValues.add("5");
    newValues.add("6");
    set.addAll(newValues);
    
    set.contains("4");
    set.containsAll(Arrays.asList("3", "4", "5"));
    
    String firstValue = set.first();
    String lastValue = set.last();
    
    String polledFirst = set.pollFirst();
    String polledLast = set.pollLast();
    
    redisson.shutdown();
    
    // use read method to fetch all objects
    Collection<String> allValues = set.readAll();
    
    redisson.shutdown();
}
 
Example 14
Source File: BoundedBlockingQueueExamples.java    From redisson-examples with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RBoundedBlockingQueue<String> queue = redisson.getBoundedBlockingQueue("myQueue");
    queue.add("1");
    queue.add("2");
    queue.add("3");
    queue.add("4");
    queue.add("5");
    
    queue.trySetCapacity(5);
    
    Thread t = new Thread(() -> {
        try {
            String element = queue.take();
            
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    });
    
    t.start();
    
    queue.put("6");
    
    redisson.shutdown();
}
 
Example 15
Source File: RedissonBlockingQueueTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testPollAsyncReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    
    RBlockingQueue<Integer> queue1 = getQueue(redisson);
    RFuture<Integer> f = queue1.pollAsync(10, TimeUnit.SECONDS);
    f.await(1, TimeUnit.SECONDS);
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    queue1.put(123);
    
    // check connection rotation
    for (int i = 0; i < 10; i++) {
        queue1.put(i);
    }
    assertThat(queue1.size()).isEqualTo(10);
    
    Integer result = f.get(1, TimeUnit.SECONDS);
    assertThat(result).isEqualTo(123);
    
    redisson.shutdown();
    runner.stop();
}
 
Example 16
Source File: TracingRedissonTest.java    From java-redis-client with Apache License 2.0 5 votes vote down vote up
@Test
public void test_config_span_name() throws Exception {
  Config config = new Config();
  config.useSingleServer().setAddress("redis://127.0.0.1:6379");

  RedissonClient customClient = new TracingRedissonClient(Redisson.create(config),
      new TracingConfiguration.Builder(tracer)
          .traceWithActiveSpanOnly(true)
          .withSpanNameProvider(operation -> "Redis." + operation)
          .build());

  final MockSpan parent = tracer.buildSpan("test").start();
  try (Scope ignore = tracer.activateSpan(parent)) {
    RMap<String, String> map = customClient.getMap("map_config_span_name");
    map.getAsync("key").get(15, TimeUnit.SECONDS);
  }
  parent.finish();

  await().atMost(15, TimeUnit.SECONDS).until(reportedSpansSize(), equalTo(2));

  List<MockSpan> spans = tracer.finishedSpans();
  assertEquals(2, spans.size());
  MockSpan redisSpan = spans.get(0);
  assertEquals("Redis.getAsync", redisSpan.operationName());

  assertNull(tracer.activeSpan());
  customClient.shutdown();
}
 
Example 17
Source File: RedissonCodecTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testListOfStrings() {
    Config config = createConfig();
    config.setCodec(new JsonJacksonCodec());
    RedissonClient redisson = Redisson.create(config);

    RMap<String, List<String>> map = redisson.getMap("list of strings", jsonListOfStringCodec);
    map.put("foo", new ArrayList<String>(Arrays.asList("bar")));

    RMap<String, List<String>> map2 = redisson.getMap("list of strings", jsonListOfStringCodec);

    assertThat(map2).isEqualTo(map);
    
    redisson.shutdown();
}
 
Example 18
Source File: RedissonRedLockTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
private void testLockLeasetime(final long leaseTime, final TimeUnit unit) throws IOException, InterruptedException {
    RedisProcess redis1 = redisTestMultilockInstance();
    RedisProcess redis2 = redisTestMultilockInstance();
    
    RedissonClient client1 = createClient(redis1.getRedisServerAddressAndPort());
    RedissonClient client2 = createClient(redis2.getRedisServerAddressAndPort());
    
    RLock lock1 = client1.getLock("lock1");
    RLock lock2 = client1.getLock("lock2");
    RLock lock3 = client2.getLock("lock3");
    RLock lock4 = client2.getLock("lock4");
    RLock lock5 = client2.getLock("lock5");
    RLock lock6 = client2.getLock("lock6");
    RLock lock7 = client2.getLock("lock7");


    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3, lock4, lock5, lock6, lock7);
    
    ExecutorService executor = Executors.newFixedThreadPool(10);
    AtomicInteger counter = new AtomicInteger();
    for (int i = 0; i < 10; i++) {
        executor.submit(() -> {
            for (int j = 0; j < 5; j++) {
                try {
                    lock.lock(leaseTime, unit);
                    int nextValue = counter.get() + 1;
                    Thread.sleep(1000);
                    counter.set(nextValue);
                    lock.unlock();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }
    
    executor.shutdown();
    assertThat(executor.awaitTermination(2, TimeUnit.MINUTES)).isTrue();
    assertThat(counter.get()).isEqualTo(50);
    
    client1.shutdown();
    client2.shutdown();
    
    assertThat(redis1.stop()).isEqualTo(0);
    assertThat(redis2.stop()).isEqualTo(0);
}
 
Example 19
Source File: RedissonTopicPatternTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void testReattach() throws InterruptedException, IOException, ExecutionException, TimeoutException {
    RedisProcess runner = new RedisRunner()
            .nosave()
            .randomDir()
            .randomPort()
            .run();
    
    Config config = new Config();
    config.useSingleServer().setAddress(runner.getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    
    final AtomicBoolean executed = new AtomicBoolean();
    
    RPatternTopic topic = redisson.getPatternTopic("topic*");
    topic.addListener(Integer.class, new PatternMessageListener<Integer>() {
        @Override
        public void onMessage(CharSequence pattern, CharSequence channel, Integer msg) {
            if (msg == 1) {
                executed.set(true);
            }
        }
    });
    
    runner.stop();

    runner = new RedisRunner()
            .port(runner.getRedisServerPort())
            .nosave()
            .randomDir()
            .run();
    
    Thread.sleep(1000);

    redisson.getTopic("topic1").publish(1);
    
    await().atMost(5, TimeUnit.SECONDS).untilTrue(executed);
    
    redisson.shutdown();
    runner.stop();
}
 
Example 20
Source File: DequeExamples.java    From redisson-examples with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) {
    // connects to 127.0.0.1:6379 by default
    RedissonClient redisson = Redisson.create();

    RDeque<String> deque = redisson.getDeque("myQueue");
    deque.add("1");
    deque.add("2");
    deque.add("3");
    deque.add("4");
    
    deque.contains("1");
    deque.peek();
    deque.poll();
    deque.element();
    
    for (String string : deque) {
        // iteration through bulk loaded values
    }
    
    boolean removedValue = deque.remove("1");
    deque.removeAll(Arrays.asList("1", "2", "3"));
    deque.containsAll(Arrays.asList("4", "1", "0"));
    
    List<String> secondList = new ArrayList<>();
    secondList.add("4");
    secondList.add("5");
    deque.addAll(secondList);

    RQueue<String> secondQueue = redisson.getQueue("mySecondQueue");
    
    deque.pollLastAndOfferFirstTo(secondQueue.getName());
    
    deque.addLast("8");
    deque.addFirst("9");
    
    deque.addLast("30");
    deque.addFirst("80");
    
    String firstValue = deque.pollFirst();
    String lastValue = deque.pollLast();
    
    String peekFirstValue = deque.peekFirst();
    String peekLastValue = deque.peekLast();
    
    String firstRemoved = deque.removeFirst();
    String lastRemoved = deque.removeLast();
    
    redisson.shutdown();
}