Search in sources :

Example 1 with PatternMessageListener

use of org.redisson.api.listener.PatternMessageListener in project redisson by redisson.

the class RedissonTopicPatternTest method testNonEventMessagesInCluster.

@Test
public void testNonEventMessagesInCluster() throws IOException, InterruptedException {
    RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slave1 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slave2 = new RedisRunner().randomPort().randomDir().nosave();
    RedisRunner slave3 = new RedisRunner().randomPort().randomDir().nosave();
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
    ClusterRunner.ClusterProcesses process = clusterRunner.run();
    Config config = new Config();
    config.useClusterServers().setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    AtomicInteger subscribeCounter = new AtomicInteger();
    RPatternTopic topic = redisson.getPatternTopic("my*", StringCodec.INSTANCE);
    topic.addListener(new PatternStatusListener() {

        @Override
        public void onPSubscribe(String pattern) {
            subscribeCounter.incrementAndGet();
        }

        @Override
        public void onPUnsubscribe(String pattern) {
            System.out.println("onPUnsubscribe: " + pattern);
        }
    });
    AtomicInteger counter = new AtomicInteger();
    PatternMessageListener<String> listener = (pattern, channel, msg) -> {
        counter.incrementAndGet();
    };
    topic.addListener(String.class, listener);
    for (int i = 0; i < 100; i++) {
        redisson.getTopic("my" + i).publish(123);
    }
    Awaitility.await().atMost(Duration.ofSeconds(2)).until(() -> counter.get() == 100);
    assertThat(subscribeCounter.get()).isEqualTo(1);
    redisson.shutdown();
    process.shutdown();
}
Also used : BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) StringCodec(org.redisson.client.codec.StringCodec) Config(org.redisson.config.Config) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedisProcess(org.redisson.RedisRunner.RedisProcess) ArrayList(java.util.ArrayList) RPatternTopic(org.redisson.api.RPatternTopic) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) RedissonClient(org.redisson.api.RedissonClient) ExecutorService(java.util.concurrent.ExecutorService) Awaitility.await(org.awaitility.Awaitility.await) RTopic(org.redisson.api.RTopic) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) PatternMessageListener(org.redisson.api.listener.PatternMessageListener) Assertions(org.junit.jupiter.api.Assertions) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Awaitility(org.awaitility.Awaitility) PatternStatusListener(org.redisson.api.listener.PatternStatusListener) RPatternTopic(org.redisson.api.RPatternTopic) Config(org.redisson.config.Config) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) PatternStatusListener(org.redisson.api.listener.PatternStatusListener) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Example 2 with PatternMessageListener

use of org.redisson.api.listener.PatternMessageListener in project redisson by redisson.

the class RedissonTopicPatternTest method testCluster.

@Test
public void testCluster() throws IOException, InterruptedException {
    RedisRunner master1 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner master2 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner master3 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner slave1 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner slave2 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    RedisRunner slave3 = new RedisRunner().randomPort().randomDir().nosave().notifyKeyspaceEvents(RedisRunner.KEYSPACE_EVENTS_OPTIONS.E, RedisRunner.KEYSPACE_EVENTS_OPTIONS.g);
    ClusterRunner clusterRunner = new ClusterRunner().addNode(master1, slave1).addNode(master2, slave2).addNode(master3, slave3);
    ClusterRunner.ClusterProcesses process = clusterRunner.run();
    Thread.sleep(3000);
    Config config = new Config();
    config.useClusterServers().setPingConnectionInterval(0).setLoadBalancer(new RandomLoadBalancer()).addNodeAddress(process.getNodes().stream().findAny().get().getRedisServerAddressAndPort());
    RedissonClient redisson = Redisson.create(config);
    AtomicInteger subscribeCounter = new AtomicInteger();
    RPatternTopic topic = redisson.getPatternTopic("__keyevent@*", StringCodec.INSTANCE);
    topic.addListener(new PatternStatusListener() {

        @Override
        public void onPSubscribe(String pattern) {
            subscribeCounter.incrementAndGet();
        }

        @Override
        public void onPUnsubscribe(String pattern) {
            System.out.println("onPUnsubscribe: " + pattern);
        }
    });
    AtomicInteger counter = new AtomicInteger();
    PatternMessageListener<String> listener = (pattern, channel, msg) -> {
        System.out.println("mes " + channel + " counter " + counter.get());
        counter.incrementAndGet();
    };
    topic.addListener(String.class, listener);
    for (int i = 0; i < 10; i++) {
        redisson.getBucket("" + i).set(i);
        redisson.getBucket("" + i).delete();
        Thread.sleep(7);
    }
    Awaitility.await().atMost(Duration.ofSeconds(2)).until(() -> counter.get() > 9);
    assertThat(subscribeCounter.get()).isEqualTo(1);
    redisson.shutdown();
    process.shutdown();
}
Also used : BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) StringCodec(org.redisson.client.codec.StringCodec) Config(org.redisson.config.Config) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) TimeoutException(java.util.concurrent.TimeoutException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RedisProcess(org.redisson.RedisRunner.RedisProcess) ArrayList(java.util.ArrayList) RPatternTopic(org.redisson.api.RPatternTopic) Future(java.util.concurrent.Future) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Duration(java.time.Duration) RedissonClient(org.redisson.api.RedissonClient) ExecutorService(java.util.concurrent.ExecutorService) Awaitility.await(org.awaitility.Awaitility.await) RTopic(org.redisson.api.RTopic) IOException(java.io.IOException) Executors(java.util.concurrent.Executors) Serializable(java.io.Serializable) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) PatternMessageListener(org.redisson.api.listener.PatternMessageListener) Assertions(org.junit.jupiter.api.Assertions) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Awaitility(org.awaitility.Awaitility) PatternStatusListener(org.redisson.api.listener.PatternStatusListener) RPatternTopic(org.redisson.api.RPatternTopic) Config(org.redisson.config.Config) RedissonClient(org.redisson.api.RedissonClient) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BasePatternStatusListener(org.redisson.api.listener.BasePatternStatusListener) PatternStatusListener(org.redisson.api.listener.PatternStatusListener) RandomLoadBalancer(org.redisson.connection.balancer.RandomLoadBalancer) Test(org.junit.jupiter.api.Test)

Aggregations

IOException (java.io.IOException)2 Serializable (java.io.Serializable)2 Duration (java.time.Duration)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 ExecutionException (java.util.concurrent.ExecutionException)2 ExecutorService (java.util.concurrent.ExecutorService)2 Executors (java.util.concurrent.Executors)2 Future (java.util.concurrent.Future)2 TimeUnit (java.util.concurrent.TimeUnit)2 TimeoutException (java.util.concurrent.TimeoutException)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)2 Awaitility (org.awaitility.Awaitility)2 Awaitility.await (org.awaitility.Awaitility.await)2 Assertions (org.junit.jupiter.api.Assertions)2 Test (org.junit.jupiter.api.Test)2 RedisProcess (org.redisson.RedisRunner.RedisProcess)2