Java Code Examples for org.redisson.api.RPatternTopic#addListener()
The following examples show how to use
org.redisson.api.RPatternTopic#addListener() .
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: RedissonTopicPatternTest.java From redisson with Apache License 2.0 | 6 votes |
@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 2
Source File: RedissonTopicPatternTest.java From redisson with Apache License 2.0 | 6 votes |
@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 3
Source File: RedissonTopicPatternTest.java From redisson with Apache License 2.0 | 6 votes |
@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 4
Source File: RedissonObject.java From redisson with Apache License 2.0 | 5 votes |
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 5
Source File: RedissonTopicPatternTest.java From redisson with Apache License 2.0 | 5 votes |
@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 6
Source File: RedissonTopicPatternTest.java From redisson with Apache License 2.0 | 4 votes |
@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 7
Source File: RedissonTopicPatternTest.java From redisson with Apache License 2.0 | 4 votes |
@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(); }