Search in sources :

Example 6 with RedisPubSubConnection

use of org.redisson.client.RedisPubSubConnection in project redisson by redisson.

the class RedisClientTest method testSubscribe.

@Test
public void testSubscribe() throws InterruptedException {
    RedisClient c = new RedisClient(RedisRunner.getDefaultRedisServerBindAddressAndPort());
    RedisPubSubConnection pubSubConnection = c.connectPubSub();
    final CountDownLatch latch = new CountDownLatch(2);
    pubSubConnection.addListener(new RedisPubSubListener<Object>() {

        @Override
        public boolean onStatus(PubSubType type, String channel) {
            assertThat(type).isEqualTo(PubSubType.SUBSCRIBE);
            assertThat(Arrays.asList("test1", "test2").contains(channel)).isTrue();
            latch.countDown();
            return true;
        }

        @Override
        public void onMessage(String channel, Object message) {
        }

        @Override
        public void onPatternMessage(String pattern, String channel, Object message) {
        }
    });
    pubSubConnection.subscribe(StringCodec.INSTANCE, "test1", "test2");
    latch.await(10, TimeUnit.SECONDS);
}
Also used : RedisClient(org.redisson.client.RedisClient) RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

RedisPubSubConnection (org.redisson.client.RedisPubSubConnection)6 RedisClient (org.redisson.client.RedisClient)2 RedisConnection (org.redisson.client.RedisConnection)2 PubSubMessage (org.redisson.client.protocol.pubsub.PubSubMessage)2 PubSubStatusMessage (org.redisson.client.protocol.pubsub.PubSubStatusMessage)2 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)2 ChannelFuture (io.netty.channel.ChannelFuture)1 ChannelFutureListener (io.netty.channel.ChannelFutureListener)1 InetSocketAddress (java.net.InetSocketAddress)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 BaseRedisPubSubListener (org.redisson.client.BaseRedisPubSubListener)1 ChannelName (org.redisson.client.ChannelName)1 RedisConnectionException (org.redisson.client.RedisConnectionException)1 Message (org.redisson.client.protocol.pubsub.Message)1 PubSubPatternMessage (org.redisson.client.protocol.pubsub.PubSubPatternMessage)1