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

The following examples show how to use org.redisson.api.RedissonClient#getTopic() . 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 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 2
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 3
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 4
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 5
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 6
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 7
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 8
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 6 votes vote down vote up
@Test
public void testCountListeners() {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic topic1 = redisson.getTopic("topic", LongCodec.INSTANCE);
    assertThat(topic1.countListeners()).isZero();
    int id = topic1.addListener(Long.class, (channel, msg) -> {
    });
    assertThat(topic1.countListeners()).isOne();

    RTopic topic2 = redisson.getTopic("topic2", LongCodec.INSTANCE);
    assertThat(topic2.countListeners()).isZero();
    int id2 = topic2.addListener(Long.class, (channel, msg) -> {
    });
    assertThat(topic2.countListeners()).isOne();

    topic1.removeListener(id);
    assertThat(topic1.countListeners()).isZero();

    topic2.removeListener(id2);
    assertThat(topic2.countListeners()).isZero();

    redisson.shutdown();
}
 
Example 9
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 10
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 11
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 12
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 13
Source File: RedissonTopicTest.java    From redisson with Apache License 2.0 5 votes vote down vote up
@Test
public void testCountSubscribers() {
    RedissonClient redisson = BaseTest.createInstance();
    RTopic topic1 = redisson.getTopic("topic", LongCodec.INSTANCE);
    assertThat(topic1.countSubscribers()).isZero();
    int id = topic1.addListener(Long.class, (channel, msg) -> {
    });
    assertThat(topic1.countSubscribers()).isOne();
    topic1.removeListener(id);
    assertThat(topic1.countSubscribers()).isZero();

    redisson.shutdown();
}
 
Example 14
Source File: RedissonBaseAdder.java    From redisson with Apache License 2.0 5 votes vote down vote up
public RedissonBaseAdder(CommandAsyncExecutor connectionManager, String name, RedissonClient redisson) {
    super(connectionManager, name);
    
    topic = redisson.getTopic(suffixName(getName(), "topic"), LongCodec.INSTANCE);
    semaphore = redisson.getSemaphore(suffixName(getName(), "semaphore"));
    listenerId = topic.addListener(Long.class, new MessageListener<Long>() {
        
        @Override
        public void onMessage(CharSequence channel, Long msg) {
            if (msg == SUM_MSG) {
                RFuture<T> addAndGetFuture = addAndGetAsync();
                addAndGetFuture.onComplete((res, e) -> {
                    if (e != null) {
                        log.error("Can't increase sum", e);
                        return;
                    }
                    
                    semaphore.releaseAsync().onComplete((r, ex) -> {
                        if (ex != null) {
                            log.error("Can't release semaphore", ex);
                            return;
                        }
                    });
                });
            }
            
            if (msg == CLEAR_MSG) {
                doReset();
                semaphore.releaseAsync().onComplete((res, e) -> {
                    if (e != null) {
                        log.error("Can't release semaphore", e);
                    }
                });
            }
        }

    });
    
}
 
Example 15
Source File: CaffeineRedisCache.java    From t-io with Apache License 2.0 5 votes vote down vote up
private static void init(RedissonClient redisson) {
	if (!inited) {
		synchronized (CaffeineRedisCache.class) {
			if (!inited) {
				topic = redisson.getTopic(CACHE_CHANGE_TOPIC);
				topic.addListener(CacheChangedVo.class, new MessageListener<CacheChangedVo>() {
					@Override
					public void onMessage(CharSequence channel, CacheChangedVo cacheChangedVo) {
						String clientid = cacheChangedVo.getClientId();
						if (StrUtil.isBlank(clientid)) {
							log.error("clientid is null");
							return;
						}
						if (Objects.equals(CacheChangedVo.CLIENTID, clientid)) {
							log.debug("自己发布的消息,{}", clientid);
							return;
						}

						String cacheName = cacheChangedVo.getCacheName();
						CaffeineRedisCache caffeineRedisCache = CaffeineRedisCache.getCache(cacheName);
						if (caffeineRedisCache == null) {
							log.info("不能根据cacheName[{}]找到CaffeineRedisCache对象", cacheName);
							return;
						}

						CacheChangeType type = cacheChangedVo.getType();
						if (type == CacheChangeType.PUT || type == CacheChangeType.UPDATE || type == CacheChangeType.REMOVE) {
							String key = cacheChangedVo.getKey();
							caffeineRedisCache.localCache.remove(key);
						} else if (type == CacheChangeType.CLEAR) {
							caffeineRedisCache.localCache.clear();
						}
					}
				});
				inited = true;
			}
		}
	}
}
 
Example 16
Source File: GuavaRedisCache.java    From t-io with Apache License 2.0 5 votes vote down vote up
private static void init(RedissonClient redisson) {
	if (!inited) {
		synchronized (GuavaRedisCache.class) {
			if (!inited) {
				topic = redisson.getTopic(CACHE_CHANGE_TOPIC);
				topic.addListener(CacheChangedVo.class, new MessageListener<CacheChangedVo>() {
					@Override
					public void onMessage(CharSequence channel, CacheChangedVo cacheChangedVo) {
						String clientid = cacheChangedVo.getClientId();
						if (StrUtil.isBlank(clientid)) {
							log.error("clientid is null");
							return;
						}
						if (Objects.equals(CacheChangedVo.CLIENTID, clientid)) {
							log.debug("自己发布的消息,{}", clientid);
							return;
						}

						String cacheName = cacheChangedVo.getCacheName();
						GuavaRedisCache guavaRedisCache = GuavaRedisCache.getCache(cacheName);
						if (guavaRedisCache == null) {
							log.info("不能根据cacheName[{}]找到GuavaRedisCache对象", cacheName);
							return;
						}

						CacheChangeType type = cacheChangedVo.getType();
						if (type == CacheChangeType.PUT || type == CacheChangeType.UPDATE || type == CacheChangeType.REMOVE) {
							String key = cacheChangedVo.getKey();
							guavaRedisCache.guavaCache.remove(key);
						} else if (type == CacheChangeType.CLEAR) {
							guavaRedisCache.guavaCache.clear();
						}
					}
				});
				inited = true;
			}
		}
	}
}
 
Example 17
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 18
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 19
Source File: RedissonTioClusterTopic.java    From t-io with Apache License 2.0 4 votes vote down vote up
public RedissonTioClusterTopic(String channel, RedissonClient redisson) {
	this.redisson = redisson;
	this.rtopic = redisson.getTopic(TioClusterConfig.TIO_CLUSTER_TOPIC + channel);
}
 
Example 20
Source File: RedisJobRepositoryImpl.java    From earth-frost with Apache License 2.0 4 votes vote down vote up
public RedisJobRepositoryImpl(RedissonClient redissonClient) {
  this.redissonClient = redissonClient;
  this.workerTopic = redissonClient.getTopic(Container.WORKER_REGISTER);
}