Search in sources :

Example 6 with PubSubType

use of org.redisson.client.protocol.pubsub.PubSubType in project redisson by redisson.

the class MasterSlaveConnectionManager method punsubscribe.

public Codec punsubscribe(final String channelName, final AsyncSemaphore lock) {
    final PubSubConnectionEntry entry = name2PubSubConnection.remove(channelName);
    if (entry == null) {
        lock.release();
        return null;
    }
    Codec entryCodec = entry.getConnection().getPatternChannels().get(channelName);
    entry.punsubscribe(channelName, new BaseRedisPubSubListener() {

        @Override
        public boolean onStatus(PubSubType type, String channel) {
            if (type == PubSubType.PUNSUBSCRIBE && channel.equals(channelName)) {
                if (entry.release() == 1) {
                    freePubSubConnections.add(entry);
                }
                lock.release();
                return true;
            }
            return false;
        }
    });
    return entryCodec;
}
Also used : Codec(org.redisson.client.codec.Codec) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 7 with PubSubType

use of org.redisson.client.protocol.pubsub.PubSubType in project redisson by redisson.

the class PubSubConnectionEntry method punsubscribe.

public void punsubscribe(final String channel, final RedisPubSubListener<?> listener) {
    conn.addListener(new BaseRedisPubSubListener() {

        @Override
        public boolean onStatus(PubSubType type, String ch) {
            if (type == PubSubType.PUNSUBSCRIBE && channel.equals(ch)) {
                conn.removeListener(this);
                removeListeners(channel);
                if (listener != null) {
                    listener.onStatus(type, channel);
                }
                return true;
            }
            return false;
        }
    });
    conn.punsubscribe(channel);
}
Also used : BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 8 with PubSubType

use of org.redisson.client.protocol.pubsub.PubSubType 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

PubSubType (org.redisson.client.protocol.pubsub.PubSubType)8 BaseRedisPubSubListener (org.redisson.client.BaseRedisPubSubListener)7 Codec (org.redisson.client.codec.Codec)4 RedisClient (org.redisson.client.RedisClient)2 RedisPubSubConnection (org.redisson.client.RedisPubSubConnection)2 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.Test)1 RedisConnectionException (org.redisson.client.RedisConnectionException)1