Search in sources :

Example 1 with PubSubStatusMessage

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

the class CommandDecoder method handlePublishSubscribe.

private void handlePublishSubscribe(CommandData<Object, Object> data, List<Object> parts, Channel channel, final Object result) {
    if (result instanceof PubSubStatusMessage) {
        String channelName = ((PubSubStatusMessage) result).getChannel();
        String operation = ((PubSubStatusMessage) result).getType().name().toLowerCase();
        PubSubKey key = new PubSubKey(channelName, operation);
        CommandData<Object, Object> d = pubSubChannels.get(key);
        if (Arrays.asList(RedisCommands.PSUBSCRIBE.getName(), RedisCommands.SUBSCRIBE.getName()).contains(d.getCommand().getName())) {
            pubSubChannels.remove(key);
            pubSubMessageDecoders.put(channelName, d.getMessageDecoder());
        }
        if (Arrays.asList(RedisCommands.PUNSUBSCRIBE.getName(), RedisCommands.UNSUBSCRIBE.getName()).contains(d.getCommand().getName())) {
            pubSubChannels.remove(key);
            pubSubMessageDecoders.remove(channelName);
        }
    }
    final RedisPubSubConnection pubSubConnection = RedisPubSubConnection.getFrom(channel);
    executor.execute(new Runnable() {

        @Override
        public void run() {
            if (result instanceof PubSubStatusMessage) {
                pubSubConnection.onMessage((PubSubStatusMessage) result);
            } else if (result instanceof PubSubMessage) {
                pubSubConnection.onMessage((PubSubMessage) result);
            } else {
                pubSubConnection.onMessage((PubSubPatternMessage) result);
            }
        }
    });
}
Also used : RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubMessage(org.redisson.client.protocol.pubsub.PubSubMessage)

Example 2 with PubSubStatusMessage

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

the class CommandPubSubDecoder method decodeResult.

@Override
protected void decodeResult(CommandData<Object, Object> data, List<Object> parts, Channel channel, Object result) throws IOException {
    try {
        if (config.getExecutor().isShutdown()) {
            return;
        }
    } catch (IllegalStateException e) {
    // arise in JBOSS. skipped
    }
    if (result instanceof Message) {
        checkpoint();
        RedisPubSubConnection pubSubConnection = RedisPubSubConnection.getFrom(channel);
        ChannelName channelName = ((Message) result).getChannel();
        if (result instanceof PubSubStatusMessage) {
            String operation = ((PubSubStatusMessage) result).getType().name().toLowerCase();
            PubSubKey key = new PubSubKey(channelName, operation);
            CommandData<Object, Object> d = commands.get(key);
            if (Arrays.asList(RedisCommands.PSUBSCRIBE.getName(), RedisCommands.SUBSCRIBE.getName()).contains(d.getCommand().getName())) {
                commands.remove(key);
                entries.put(channelName, new PubSubEntry(d.getMessageDecoder()));
            }
            if (Arrays.asList(RedisCommands.PUNSUBSCRIBE.getName(), RedisCommands.UNSUBSCRIBE.getName()).contains(d.getCommand().getName())) {
                commands.remove(key);
                if (result instanceof PubSubPatternMessage) {
                    channelName = ((PubSubPatternMessage) result).getPattern();
                }
                PubSubEntry entry = entries.remove(channelName);
                if (config.isKeepAlive()) {
                    enqueueMessage(result, pubSubConnection, entry);
                }
            }
        }
        if (config.isKeepAlive()) {
            if (result instanceof PubSubPatternMessage) {
                channelName = ((PubSubPatternMessage) result).getPattern();
            }
            PubSubEntry entry = entries.get(channelName);
            if (entry != null) {
                enqueueMessage(result, pubSubConnection, entry);
            }
        } else {
            config.getExecutor().execute(new Runnable() {

                @Override
                public void run() {
                    if (result instanceof PubSubStatusMessage) {
                        pubSubConnection.onMessage((PubSubStatusMessage) result);
                    } else if (result instanceof PubSubMessage) {
                        pubSubConnection.onMessage((PubSubMessage) result);
                    } else if (result instanceof PubSubPatternMessage) {
                        pubSubConnection.onMessage((PubSubPatternMessage) result);
                    }
                }
            });
        }
    } else {
        if (data != null && data.getCommand().getName().equals("PING")) {
            super.decodeResult(data, parts, channel, result);
        }
    }
}
Also used : RedisPubSubConnection(org.redisson.client.RedisPubSubConnection) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubPatternMessage(org.redisson.client.protocol.pubsub.PubSubPatternMessage) Message(org.redisson.client.protocol.pubsub.Message) PubSubMessage(org.redisson.client.protocol.pubsub.PubSubMessage) PubSubPatternMessage(org.redisson.client.protocol.pubsub.PubSubPatternMessage) ChannelName(org.redisson.client.ChannelName) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) PubSubMessage(org.redisson.client.protocol.pubsub.PubSubMessage)

Example 3 with PubSubStatusMessage

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

the class PublishSubscribeService method unsubscribe.

public CompletableFuture<Void> unsubscribe(PubSubType topicType, ChannelName channelName) {
    PubSubConnectionEntry entry = name2PubSubConnection.remove(createKey(channelName));
    if (entry == null || connectionManager.isShuttingDown()) {
        return CompletableFuture.completedFuture(null);
    }
    AtomicBoolean executed = new AtomicBoolean();
    CompletableFuture<Void> result = new CompletableFuture<>();
    BaseRedisPubSubListener listener = new BaseRedisPubSubListener() {

        @Override
        public boolean onStatus(PubSubType type, CharSequence channel) {
            if (type == topicType && channel.equals(channelName)) {
                executed.set(true);
                if (entry.release() == 1) {
                    MasterSlaveEntry msEntry = getEntry(channelName);
                    msEntry.returnPubSubConnection(entry.getConnection());
                }
                result.complete(null);
                return true;
            }
            return false;
        }
    };
    ChannelFuture future;
    if (topicType == PubSubType.UNSUBSCRIBE) {
        future = entry.unsubscribe(channelName, listener);
    } else {
        future = entry.punsubscribe(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) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) PubSubType(org.redisson.client.protocol.pubsub.PubSubType)

Example 4 with PubSubStatusMessage

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

the class RedisPubSubConnection method onDisconnect.

@Override
public void onDisconnect() {
    Set<String> channels = new HashSet<String>();
    Set<String> pchannels = new HashSet<String>();
    synchronized (this) {
        channels.addAll(unsubscibedChannels);
        pchannels.addAll(punsubscibedChannels);
    }
    for (String channel : channels) {
        onMessage(new PubSubStatusMessage(PubSubType.UNSUBSCRIBE, channel));
    }
    for (String channel : pchannels) {
        onMessage(new PubSubStatusMessage(PubSubType.PUNSUBSCRIBE, channel));
    }
}
Also used : PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage) HashSet(java.util.HashSet)

Example 5 with PubSubStatusMessage

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

the class RedisPubSubConnection method unsubscribe.

public void unsubscribe(final String... channels) {
    synchronized (this) {
        for (String ch : channels) {
            this.channels.remove(ch);
            unsubscibedChannels.add(ch);
        }
    }
    ChannelFuture future = async((MultiDecoder) null, RedisCommands.UNSUBSCRIBE, 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.UNSUBSCRIBE, channel));
                }
            }
        }
    });
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) PubSubStatusMessage(org.redisson.client.protocol.pubsub.PubSubStatusMessage)

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