org.redisson.api.RPatternTopic Java Examples

The following examples show how to use org.redisson.api.RPatternTopic. 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: SpringNamespaceObjectTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRObjects() {
    Object bean = context.getBean(key);
    assertTrue(cls.isInstance(bean));
    if (RObject.class.isAssignableFrom(cls)) {
        assertEquals(parentKey == null ? key : parentKey, RObject.class.cast(bean).getName());
    }
    if (RTopic.class.isAssignableFrom(cls)) {
        assertEquals(key, RTopic.class.cast(bean).getChannelNames().get(0));
    }
    if (RPatternTopic.class.isAssignableFrom(cls)) {
        assertEquals(key, RPatternTopic.class.cast(bean).getPatternNames().get(0));
    }
    if (RLiveObject.class.isAssignableFrom(cls)) {
        assertEquals(key, RLiveObject.class.cast(bean).getLiveObjectId());
    }
}
 
Example #2
Source File: RedissonTopicPatternTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiType() throws InterruptedException {
    RPatternTopic topic1 = redisson.getPatternTopic("topic1.*");
    AtomicInteger str = new AtomicInteger(); 
    topic1.addListener(String.class, (pattern, channel, msg) -> {
        str.incrementAndGet();
    });
    AtomicInteger i = new AtomicInteger();
    topic1.addListener(Integer.class, (pattern, channel, msg) -> {
        i.incrementAndGet();
    });
    
    redisson.getTopic("topic1.str").publish("123");
    redisson.getTopic("topic1.int").publish(123);
    
    Thread.sleep(500);
    
    Assert.assertEquals(1, i.get());
    Assert.assertEquals(1, str.get());
}
 
Example #3
Source File: RedissonTopicPatternTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testUnsubscribe() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(1);

    RPatternTopic topic1 = redisson.getPatternTopic("topic1.*");
    int listenerId = topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assert.fail();
    });
    topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assert.assertTrue(pattern.equals("topic1.*"));
        Assert.assertTrue(channel.equals("topic1.t3"));
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });
    topic1.removeListener(listenerId);

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

    Assert.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));
}
 
Example #4
Source File: RedissonTopicPatternTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testListenerRemove() throws InterruptedException {
    RedissonClient redisson1 = BaseTest.createInstance();
    RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
    final CountDownLatch l = new CountDownLatch(1);
    topic1.addListener(new BasePatternStatusListener() {
        @Override
        public void onPUnsubscribe(String pattern) {
            Assert.assertEquals("topic.*", pattern);
            l.countDown();
        }
    });
    int id = topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assert.fail();
    });

    RedissonClient redisson2 = BaseTest.createInstance();
    RTopic topic2 = redisson2.getTopic("topic.t1");
    topic1.removeListener(id);
    topic2.publish(new Message("123"));

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example #5
Source File: RedissonObject.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected final <T extends ObjectListener> int addListener(String name, T listener, BiConsumer<T, String> consumer) {
    RPatternTopic topic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, name);
    return topic.addListener(String.class, (pattern, channel, msg) -> {
        if (msg.equals(getName())) {
            consumer.accept(listener, msg);
        }
    });
}
 
Example #6
Source File: RedissonObject.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected final <T extends ObjectListener> RFuture<Integer> addListenerAsync(String name, T listener, BiConsumer<T, String> consumer) {
    RPatternTopic topic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, name);
    return topic.addListenerAsync(String.class, (pattern, channel, msg) -> {
        if (msg.equals(getName())) {
            consumer.accept(listener, msg);
        }
    });
}
 
Example #7
Source File: RedissonObject.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void removeListener(int listenerId) {
    RPatternTopic expiredTopic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, "__keyevent@*:expired");
    expiredTopic.removeListener(listenerId);

    RPatternTopic deletedTopic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, "__keyevent@*:del");
    deletedTopic.removeListener(listenerId);
}
 
Example #8
Source File: RedissonObject.java    From redisson with Apache License 2.0 5 votes vote down vote up
protected final void removeListenersAsync(int listenerId, CountableListener<Void> listener) {
    RPatternTopic expiredTopic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, "__keyevent@*:expired");
    expiredTopic.removeListenerAsync(listenerId).onComplete(listener);

    RPatternTopic deletedTopic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, "__keyevent@*:del");
    deletedTopic.removeListenerAsync(listenerId).onComplete(listener);
}
 
Example #9
Source File: RedissonBucket.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public void removeListener(int listenerId) {
    RPatternTopic expiredTopic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, "__keyevent@*:set");
    expiredTopic.removeListener(listenerId);

    super.removeListener(listenerId);
}
 
Example #10
Source File: RedissonBucket.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Override
public RFuture<Void> removeListenerAsync(int listenerId) {
    RPromise<Void> result = new RedissonPromise<>();
    CountableListener<Void> listener = new CountableListener<>(result, null, 3);

    RPatternTopic setTopic = new RedissonPatternTopic(StringCodec.INSTANCE, commandExecutor, "__keyevent@*:set");
    setTopic.removeListenerAsync(listenerId).onComplete(listener);
    removeListenersAsync(listenerId, listener);
    return result;
}
 
Example #11
Source File: RedissonTopicPatternTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testLazyUnsubscribe() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(1);

    RedissonClient redisson1 = BaseTest.createInstance();
    RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
    int listenerId = topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assert.fail();
    });

    Thread.sleep(1000);
    topic1.removeListener(listenerId);
    Thread.sleep(1000);

    RedissonClient redisson2 = BaseTest.createInstance();
    RPatternTopic topic2 = redisson2.getPatternTopic("topic.*");
    topic2.addListener(Message.class, (pattern, channel, msg) -> {
        Assert.assertTrue(pattern.equals("topic.*"));
        Assert.assertTrue(channel.equals("topic.t1"));
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });

    RTopic topic3 = redisson2.getTopic("topic.t1");
    topic3.publish(new Message("123"));

    Assert.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example #12
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RPatternTopic getPatternTopic(String pattern) {
  return redissonClient.getPatternTopic(pattern);
}
 
Example #13
Source File: TracingRedissonClient.java    From java-redis-client with Apache License 2.0 4 votes vote down vote up
@Override
public RPatternTopic getPatternTopic(String pattern,
    Codec codec) {
  return redissonClient.getPatternTopic(pattern, codec);
}
 
Example #14
Source File: RedissonTopicPatternTest.java    From redisson with Apache License 2.0 4 votes vote down vote up
@Test
public void test() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(5);

    final CountDownLatch statusRecieved = new CountDownLatch(1);
    RedissonClient redisson1 = BaseTest.createInstance();
    RPatternTopic topic1 = redisson1.getPatternTopic("topic.*");
    topic1.addListener(new BasePatternStatusListener() {
        @Override
        public void onPSubscribe(String pattern) {
            Assert.assertEquals("topic.*", pattern);
            statusRecieved.countDown();
        }
    });
    topic1.addListener(Message.class, (pattern, channel, msg) -> {
        Assert.assertEquals(new Message("123"), msg);
        messageRecieved.countDown();
    });

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

    RTopic topicz = redisson2.getTopic("topicz.t1");
    topicz.publish(new Message("789")); // this message doesn't get
                                        // delivered, and would fail the
                                        // assertion

    RTopic topict2 = redisson2.getTopic("topic.t2");
    topict2.publish(new Message("123"));

    statusRecieved.await();
    Assert.assertTrue(messageRecieved.await(5, TimeUnit.SECONDS));

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example #15
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();
}