Search in sources :

Example 1 with BaseRedisPubSubListener

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

the class MasterSlaveConnectionManager method unsubscribe.

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

        @Override
        public boolean onStatus(PubSubType type, String channel) {
            if (type == PubSubType.UNSUBSCRIBE && 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 2 with BaseRedisPubSubListener

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

the class MasterSlaveConnectionManager method punsubscribe.

@Override
public RFuture<Codec> punsubscribe(final String channelName, boolean temporaryDown) {
    final PubSubConnectionEntry entry = name2PubSubConnection.remove(channelName);
    if (entry == null) {
        return null;
    }
    freePubSubConnections.remove(entry);
    final Codec entryCodec = entry.getConnection().getChannels().get(channelName);
    if (temporaryDown) {
        final RPromise<Codec> result = newPromise();
        entry.punsubscribe(channelName, new BaseRedisPubSubListener() {

            @Override
            public boolean onStatus(PubSubType type, String channel) {
                if (type == PubSubType.PUNSUBSCRIBE && channel.equals(channelName)) {
                    result.trySuccess(entryCodec);
                    return true;
                }
                return false;
            }
        });
        return result;
    }
    entry.punsubscribe(channelName, null);
    return newSucceededFuture(entryCodec);
}
Also used : Codec(org.redisson.client.codec.Codec) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 3 with BaseRedisPubSubListener

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

the class MasterSlaveConnectionManager method unsubscribe.

@Override
public RFuture<Codec> unsubscribe(final String channelName, boolean temporaryDown) {
    final PubSubConnectionEntry entry = name2PubSubConnection.remove(channelName);
    if (entry == null) {
        return null;
    }
    freePubSubConnections.remove(entry);
    final Codec entryCodec = entry.getConnection().getChannels().get(channelName);
    if (temporaryDown) {
        final RPromise<Codec> result = newPromise();
        entry.unsubscribe(channelName, new BaseRedisPubSubListener() {

            @Override
            public boolean onStatus(PubSubType type, String channel) {
                if (type == PubSubType.UNSUBSCRIBE && channel.equals(channelName)) {
                    result.trySuccess(entryCodec);
                    return true;
                }
                return false;
            }
        });
        return result;
    }
    entry.unsubscribe(channelName, null);
    return newSucceededFuture(entryCodec);
}
Also used : Codec(org.redisson.client.codec.Codec) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 4 with BaseRedisPubSubListener

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

the class PubSubConnectionEntry method unsubscribe.

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

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

Example 5 with BaseRedisPubSubListener

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

the class SentinelConnectionManager method registerSentinel.

private RFuture<RedisPubSubConnection> registerSentinel(final SentinelServersConfig cfg, final URL addr, final MasterSlaveServersConfig c) {
    RedisClient client = createClient(addr.getHost(), addr.getPort(), c.getConnectTimeout(), c.getRetryInterval() * c.getRetryAttempts());
    RedisClient oldClient = sentinels.putIfAbsent(addr.getHost() + ":" + addr.getPort(), client);
    if (oldClient != null) {
        return newSucceededFuture(null);
    }
    RFuture<RedisPubSubConnection> pubsubFuture = client.connectPubSubAsync();
    pubsubFuture.addListener(new FutureListener<RedisPubSubConnection>() {

        @Override
        public void operationComplete(Future<RedisPubSubConnection> future) throws Exception {
            if (!future.isSuccess()) {
                log.warn("Can't connect to sentinel: {}:{}", addr.getHost(), addr.getPort());
                return;
            }
            RedisPubSubConnection pubsub = future.getNow();
            pubsub.addListener(new BaseRedisPubSubListener() {

                @Override
                public void onMessage(String channel, Object msg) {
                    if ("+sentinel".equals(channel)) {
                        onSentinelAdded(cfg, (String) msg, c);
                    }
                    if ("+slave".equals(channel)) {
                        onSlaveAdded(addr, (String) msg);
                    }
                    if ("+sdown".equals(channel)) {
                        onNodeDown(addr, (String) msg);
                    }
                    if ("-sdown".equals(channel)) {
                        onNodeUp(addr, (String) msg);
                    }
                    if ("+switch-master".equals(channel)) {
                        onMasterChange(cfg, addr, (String) msg);
                    }
                }

                @Override
                public boolean onStatus(PubSubType type, String channel) {
                    if (type == PubSubType.SUBSCRIBE) {
                        log.debug("subscribed to channel: {} from Sentinel {}:{}", channel, addr.getHost(), addr.getPort());
                    }
                    return true;
                }
            });
            pubsub.subscribe(StringCodec.INSTANCE, "+switch-master", "+sdown", "-sdown", "+slave", "+sentinel");
            log.info("sentinel: {}:{} added", addr.getHost(), addr.getPort());
        }
    });
    return pubsubFuture;
}
Also used : RedisClient(org.redisson.client.RedisClient) RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) RedisConnectionException(org.redisson.client.RedisConnectionException)

Aggregations

BaseRedisPubSubListener (org.redisson.client.BaseRedisPubSubListener)10 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)7 Codec (org.redisson.client.codec.Codec)4 ChannelName (org.redisson.client.ChannelName)3 ArrayList (java.util.ArrayList)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 PubSubConnectionEntry (org.redisson.pubsub.PubSubConnectionEntry)2 DefaultMessage (org.springframework.data.redis.connection.DefaultMessage)2 Collection (java.util.Collection)1 RedisClient (org.redisson.client.RedisClient)1 RedisConnectionException (org.redisson.client.RedisConnectionException)1 RedisPubSubConnection (org.redisson.client.RedisPubSubConnection)1