Search in sources :

Example 16 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonTopicTest method testCommandsOrdering.

@Test
public void testCommandsOrdering() throws InterruptedException {
    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic<Long> topic1 = redisson1.getTopic("topic", LongCodec.INSTANCE);
    AtomicBoolean stringMessageReceived = new AtomicBoolean();
    topic1.addListener((channel, msg) -> {
        assertThat(msg).isEqualTo(123);
        stringMessageReceived.set(true);
    });
    topic1.publish(123L);
    Awaitility.await().atMost(Duration.ONE_SECOND).untilTrue(stringMessageReceived);
    redisson1.shutdown();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) Test(org.junit.Test)

Example 17 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonTopicTest method testSyncCommands.

@Test
public void testSyncCommands() throws InterruptedException {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic<String> topic = redisson.getTopic("system_bus");
    RSet<String> redissonSet = redisson.getSet("set1");
    CountDownLatch latch = new CountDownLatch(1);
    topic.addListener((channel, msg) -> {
        for (int j = 0; j < 1000; j++) {
            System.out.println("start: " + j);
            redissonSet.contains("" + j);
            System.out.println("end: " + j);
        }
        latch.countDown();
    });
    topic.publish("sometext");
    latch.await();
    redisson.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 18 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonTopicTest method testRemoveAllListeners.

@Test
public void testRemoveAllListeners() throws InterruptedException {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic<Message> topic1 = redisson.getTopic("topic1");
    for (int i = 0; i < 10; i++) {
        topic1.addListener((channel, msg) -> {
            Assert.fail();
        });
    }
    topic1 = redisson.getTopic("topic1");
    topic1.removeAllListeners();
    topic1.publish(new Message("123"));
    redisson.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) Test(org.junit.Test)

Example 19 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonTopicTest method testReattach.

@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();
    RTopic<Integer> topic = redisson.getTopic("topic");
    topic.addListener(new MessageListener<Integer>() {

        @Override
        public void onMessage(String 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("topic").publish(1);
    await().atMost(5, TimeUnit.SECONDS).untilTrue(executed);
    redisson.shutdown();
    runner.stop();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedissonClient(org.redisson.api.RedissonClient) RedisProcess(org.redisson.RedisRunner.RedisProcess) Config(org.redisson.config.Config) Test(org.junit.Test)

Example 20 with RedissonClient

use of org.redisson.api.RedissonClient in project redisson by redisson.

the class RedissonTopicTest method testLazyUnsubscribe.

@Test
public void testLazyUnsubscribe() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(1);
    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic<Message> topic1 = redisson1.getTopic("topic");
    int listenerId = topic1.addListener((channel, msg) -> {
        Assert.fail();
    });
    Thread.sleep(1000);
    topic1.removeListener(listenerId);
    Thread.sleep(1000);
    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic<Message> topic2 = redisson2.getTopic("topic");
    topic2.addListener((channel, msg) -> {
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    topic2.publish(new Message("123"));
    Assert.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));
    redisson1.shutdown();
    redisson2.shutdown();
}
Also used : RedissonClient(org.redisson.api.RedissonClient) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

RedissonClient (org.redisson.api.RedissonClient)95 Test (org.junit.Test)87 Config (org.redisson.config.Config)44 RedisProcess (org.redisson.RedisRunner.RedisProcess)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)22 CountDownLatch (java.util.concurrent.CountDownLatch)15 ExecutorService (java.util.concurrent.ExecutorService)12 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)10 RLock (org.redisson.api.RLock)10 IOException (java.io.IOException)9 ExecutionException (java.util.concurrent.ExecutionException)9 NotSerializableException (java.io.NotSerializableException)8 RemoteServiceAckTimeoutException (org.redisson.remote.RemoteServiceAckTimeoutException)8 RemoteServiceTimeoutException (org.redisson.remote.RemoteServiceTimeoutException)8 ArrayList (java.util.ArrayList)4 HashMap (java.util.HashMap)4 RemoteInvocationOptions (org.redisson.api.RemoteInvocationOptions)4 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)3 BasePatternStatusListener (org.redisson.api.listener.BasePatternStatusListener)3 SerializationCodec (org.redisson.codec.SerializationCodec)3