Java Code Examples for org.redisson.api.RTopic#publish()

The following examples show how to use org.redisson.api.RTopic#publish() . 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: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testListenerRemove() throws InterruptedException {
    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic topic1 = redisson1.getTopic("topic");
    int id = topic1.addListener(Message.class, (channel, msg) -> {
        Assert.fail();
    });

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

    Thread.sleep(1000);

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example 2
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 3
Source File: RedissonSessionRepository.java    From redisson with Apache License 2.0 6 votes vote down vote up
RedissonSession() {
    this.delegate = new MapSession();
    map = redisson.getMap(keyPrefix + delegate.getId(), new CompositeCodec(StringCodec.INSTANCE, redisson.getConfig().getCodec()));

    Map<String, Object> newMap = new HashMap<String, Object>(3);
    newMap.put("session:creationTime", delegate.getCreationTime().toEpochMilli());
    newMap.put("session:lastAccessedTime", delegate.getLastAccessedTime().toEpochMilli());
    newMap.put("session:maxInactiveInterval", delegate.getMaxInactiveInterval().getSeconds());
    map.putAll(newMap);

    updateExpiration();
    
    String channelName = getEventsChannelName(delegate.getId());
    RTopic topic = redisson.getTopic(channelName, StringCodec.INSTANCE);
    topic.publish(delegate.getId());
}
 
Example 4
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 5
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testLazyUnsubscribe() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(1);

    RedissonClient redisson1 = BaseTest.createInstance();
    RTopic topic1 = redisson1.getTopic("topic");
    int listenerId = topic1.addListener(Message.class, (channel, msg) -> {
        Assert.fail();
    });
    Thread.sleep(1000);
    topic1.removeListener(listenerId);
    Thread.sleep(1000);

    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"));

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

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example 6
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testSyncCommands() throws InterruptedException {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic topic = redisson.getTopic("system_bus");
    RSet<String> redissonSet = redisson.getSet("set1");
    CountDownLatch latch = new CountDownLatch(1);
    topic.addListener(String.class, (channel, msg) -> {
        for (int j = 0; j < 1000; j++) {
            redissonSet.contains("" + j);
        }
        latch.countDown();
    });
    
    topic.publish("sometext");
    
    latch.await();
    redisson.shutdown();
}
 
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: 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 9
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 10
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testRemoveAllListeners2() throws InterruptedException {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic topic1 = redisson.getTopic("topic1");
    AtomicInteger counter = new AtomicInteger();
    
    for (int j = 0; j < 100; j++) {
        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 11
Source File: DataPublisherRedisImpl.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
public void doPublish(String clientId, String dataKey, EventBaseDTO data) {
    RQueue<EventBaseDTO> dataList = redissonClient.getQueue(dataKey);
    boolean result = dataList.offer(data);
    log.info("推送结果{},推送信息,{}",result, data);
    String notifier = NOTIFIER.concat(clientId);
    RTopic<String> rTopic = redissonClient.getTopic(notifier);
    rTopic.publish(dataKey);
}
 
Example 12
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 13
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testHeavyLoad() throws InterruptedException {
    final CountDownLatch messageRecieved = new CountDownLatch(1000);

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

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

    int count = 10000;
    for (int i = 0; i < count; i++) {
        topic2.publish(new Message("123"));
    }

    messageRecieved.await();

    Thread.sleep(1000);

    Assert.assertEquals(count, counter.get());

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example 14
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testInnerPublish() throws InterruptedException {

    RedissonClient redisson1 = BaseTest.createInstance();
    final RTopic topic1 = redisson1.getTopic("topic1");
    final CountDownLatch messageRecieved = new CountDownLatch(3);
    int listenerId = topic1.addListener(Message.class, (channel, msg) -> {
        Assert.assertEquals(msg, new Message("test"));
        messageRecieved.countDown();
    });

    RedissonClient redisson2 = BaseTest.createInstance();
    final RTopic topic2 = redisson2.getTopic("topic2");
    topic2.addListener(Message.class, (channel, msg) -> {
        messageRecieved.countDown();
        Message m = new Message("test");
        if (!msg.equals(m)) {
            topic1.publish(m);
            topic2.publish(m);
        }
    });
    topic2.publish(new Message("123"));

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

    redisson1.shutdown();
    redisson2.shutdown();
}
 
Example 15
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 16
Source File: TimeoutTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
public void testPubSub() throws InterruptedException, ExecutionException {
    RTopic topic = redisson.getTopic("simple");
    topic.addListener(String.class, new MessageListener<String>() {
        @Override
        public void onMessage(CharSequence channel, String msg) {
            System.out.println("msg: " + msg);
        }
    });
    for (int i = 0; i < 100; i++) {
        Thread.sleep(1000);
        topic.publish("test" + i);
    }
}
 
Example 17
Source File: RedisPlayground.java    From synapse with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldRetrieveMessagesFromTopic() {
    RTopic topic = redisson.getTopic("test-topic");
    topic.addListener(String.class, (channel, message) -> {
        LOG.info("Received message={} from channel={}", message, channel);
    });
    topic.publish("some message");
    topic.removeAllListeners();
}
 
Example 18
Source File: RedisTopicEventChannel.java    From jstarcraft-core with Apache License 2.0 5 votes vote down vote up
@Override
public void triggerEvent(Object event) {
    Class type = event.getClass();
    // TODO 需要防止路径冲突
    RTopic topic = getTopic(type);
    byte[] bytes = codec.encode(type, event);
    topic.publish(bytes);
}
 
Example 19
Source File: MQAop.java    From redisson-spring-boot-starter with GNU Lesser General Public License v3.0 5 votes vote down vote up
@Around("aspect(mq)")
public Object aroundAdvice(ProceedingJoinPoint proceedingJoinPoint, MQPublish mq) {
    try {
        Object obj = proceedingJoinPoint.proceed();
        RTopic topic = redissonClient.getTopic(mq.name());
        topic.publish(obj);
        return obj;
    } catch (Throwable e) {
        throw new RuntimeException(e);
    }

}
 
Example 20
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();
}