Search in sources :

Example 6 with PubSubStatusMessage

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

the class RedisPubSubConnection method punsubscribe.

public void punsubscribe(final String... channels) {
    synchronized (this) {
        for (String ch : channels) {
            patternChannels.remove(ch);
            punsubscibedChannels.add(ch);
        }
    }
    ChannelFuture future = async((MultiDecoder) null, RedisCommands.PUNSUBSCRIBE, channels);
    future.addListener(new FutureListener<Void>() {

        @Override
        public void operationComplete(Future<Void> future) throws Exception {
            if (!future.isSuccess()) {
                for (String channel : channels) {
                    removeDisconnectListener(channel);
                    onMessage(new PubSubStatusMessage(PubSubType.PUNSUBSCRIBE, channel));
                }
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage)

Example 7 with PubSubStatusMessage

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

the class PublishSubscribeService method unsubscribe.

private CompletableFuture<Codec> unsubscribe(ChannelName channelName, MasterSlaveEntry e, PubSubType topicType) {
    if (connectionManager.isShuttingDown()) {
        return CompletableFuture.completedFuture(null);
    }
    CompletableFuture<Codec> result = new CompletableFuture<>();
    AsyncSemaphore lock = getSemaphore(channelName);
    lock.acquire(() -> {
        PubSubConnectionEntry entry = name2PubSubConnection.remove(new PubSubKey(channelName, e));
        if (entry == null) {
            lock.release();
            result.complete(null);
            return;
        }
        freePubSubLock.acquire(() -> {
            PubSubEntry ee = entry2PubSubConnection.getOrDefault(e, new PubSubEntry());
            Queue<PubSubConnectionEntry> freePubSubConnections = ee.getEntries();
            freePubSubConnections.remove(entry);
            freePubSubLock.release();
            Codec entryCodec;
            if (topicType == PubSubType.PUNSUBSCRIBE) {
                entryCodec = entry.getConnection().getPatternChannels().get(channelName);
            } else {
                entryCodec = entry.getConnection().getChannels().get(channelName);
            }
            AtomicBoolean executed = new AtomicBoolean();
            RedisPubSubListener<Object> listener = new BaseRedisPubSubListener() {

                @Override
                public boolean onStatus(PubSubType type, CharSequence channel) {
                    if (type == topicType && channel.equals(channelName)) {
                        executed.set(true);
                        lock.release();
                        result.complete(entryCodec);
                        return true;
                    }
                    return false;
                }
            };
            ChannelFuture future;
            if (topicType == PubSubType.PUNSUBSCRIBE) {
                future = entry.punsubscribe(channelName, listener);
            } else {
                future = entry.unsubscribe(channelName, listener);
            }
            future.addListener((ChannelFutureListener) f -> {
                if (!f.isSuccess()) {
                    return;
                }
                connectionManager.newTimeout(timeout -> {
                    if (executed.get()) {
                        return;
                    }
                    entry.getConnection().onMessage(new PubSubStatusMessage(topicType, channelName));
                }, config.getTimeout(), TimeUnit.MILLISECONDS);
            });
        });
    });
    return result;
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) Timeout(io.netty.util.Timeout) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) java.util(java.util) Logger(org.slf4j.Logger) Codec(org.redisson.client.codec.Codec) ConnectionManager(org.redisson.connection.ConnectionManager) java.util.concurrent(java.util.concurrent) org.redisson.client(org.redisson.client) PubSubPatternStatusListener(org.redisson.PubSubPatternStatusListener) LoggerFactory(org.slf4j.LoggerFactory) MasterSlaveServersConfig(org.redisson.config.MasterSlaveServersConfig) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Collectors(java.util.stream.Collectors) ChannelFuture(io.netty.channel.ChannelFuture) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ChannelFutureListener(io.netty.channel.ChannelFutureListener) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Codec(org.redisson.client.codec.Codec) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Aggregations

PubSubStatusMessage (org.redisson.client.protocol.pubsub.PubSubStatusMessage)7 ChannelFuture (io.netty.channel.ChannelFuture)4 ChannelFutureListener (io.netty.channel.ChannelFutureListener)2 Timeout (io.netty.util.Timeout)2 java.util (java.util)2 java.util.concurrent (java.util.concurrent)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 Collectors (java.util.stream.Collectors)2 PubSubPatternStatusListener (org.redisson.PubSubPatternStatusListener)2 org.redisson.client (org.redisson.client)2 RedisPubSubConnection (org.redisson.client.RedisPubSubConnection)2 Codec (org.redisson.client.codec.Codec)2 PubSubMessage (org.redisson.client.protocol.pubsub.PubSubMessage)2 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)2 MasterSlaveServersConfig (org.redisson.config.MasterSlaveServersConfig)2 ConnectionManager (org.redisson.connection.ConnectionManager)2 MasterSlaveEntry (org.redisson.connection.MasterSlaveEntry)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2