Search in sources :

Example 1 with Message

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

Aggregations

ChannelName (org.redisson.client.ChannelName)1 RedisPubSubConnection (org.redisson.client.RedisPubSubConnection)1 Message (org.redisson.client.protocol.pubsub.Message)1 PubSubMessage (org.redisson.client.protocol.pubsub.PubSubMessage)1 PubSubPatternMessage (org.redisson.client.protocol.pubsub.PubSubPatternMessage)1 PubSubStatusMessage (org.redisson.client.protocol.pubsub.PubSubStatusMessage)1