Search in sources :

Example 1 with ChannelName

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

the class CommandEncoder method encode.

private ByteBuf encode(Object in) {
    if (in instanceof byte[]) {
        return Unpooled.wrappedBuffer((byte[]) in);
    }
    if (in instanceof ByteBuf) {
        return (ByteBuf) in;
    }
    if (in instanceof ChannelName) {
        return Unpooled.wrappedBuffer(((ChannelName) in).getName());
    }
    String payload = in.toString();
    ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(ByteBufUtil.utf8MaxBytes(payload));
    ByteBufUtil.writeUtf8(buf, payload);
    return buf;
}
Also used : ChannelName(org.redisson.client.ChannelName) ByteBuf(io.netty.buffer.ByteBuf)

Example 2 with ChannelName

use of org.redisson.client.ChannelName 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 ChannelName

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

the class PublishSubscribe method subscribe.

public CompletableFuture<E> subscribe(String entryName, String channelName) {
    AsyncSemaphore semaphore = service.getSemaphore(new ChannelName(channelName));
    CompletableFuture<E> newPromise = new CompletableFuture<>();
    int timeout = service.getConnectionManager().getConfig().getTimeout();
    Timeout lockTimeout = service.getConnectionManager().newTimeout(t -> {
        newPromise.completeExceptionally(new RedisTimeoutException("Unable to acquire subscription lock after " + timeout + "ms. " + "Increase 'subscriptionsPerConnection' and/or 'subscriptionConnectionPoolSize' parameters."));
    }, timeout, TimeUnit.MILLISECONDS);
    semaphore.acquire(() -> {
        if (!lockTimeout.cancel()) {
            semaphore.release();
            return;
        }
        E entry = entries.get(entryName);
        if (entry != null) {
            entry.acquire();
            semaphore.release();
            entry.getPromise().whenComplete((r, e) -> {
                if (e != null) {
                    newPromise.completeExceptionally(e);
                    return;
                }
                newPromise.complete(r);
            });
            return;
        }
        E value = createEntry(newPromise);
        value.acquire();
        E oldValue = entries.putIfAbsent(entryName, value);
        if (oldValue != null) {
            oldValue.acquire();
            semaphore.release();
            oldValue.getPromise().whenComplete((r, e) -> {
                if (e != null) {
                    newPromise.completeExceptionally(e);
                    return;
                }
                newPromise.complete(r);
            });
            return;
        }
        RedisPubSubListener<Object> listener = createListener(channelName, value);
        CompletableFuture<PubSubConnectionEntry> s = service.subscribe(LongCodec.INSTANCE, channelName, semaphore, listener);
        s.whenComplete((r, e) -> {
            if (e != null) {
                value.getPromise().completeExceptionally(e);
                return;
            }
            value.getPromise().complete(value);
        });
    });
    return newPromise;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) ChannelName(org.redisson.client.ChannelName) RedisTimeoutException(org.redisson.client.RedisTimeoutException) Timeout(io.netty.util.Timeout)

Example 4 with ChannelName

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

the class PublishSubscribe method unsubscribe.

public void unsubscribe(E entry, String entryName, String channelName) {
    AsyncSemaphore semaphore = service.getSemaphore(new ChannelName(channelName));
    semaphore.acquire(() -> {
        if (entry.release() == 0) {
            entries.remove(entryName);
            service.unsubscribe(PubSubType.UNSUBSCRIBE, new ChannelName(channelName)).whenComplete((r, e) -> {
                semaphore.release();
            });
        } else {
            semaphore.release();
        }
    });
}
Also used : ChannelName(org.redisson.client.ChannelName)

Example 5 with ChannelName

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

the class RedissonReactiveSubscription method receive.

@Override
public Flux<Message<ByteBuffer, ByteBuffer>> receive() {
    if (flux.get() != null) {
        return flux.get();
    }
    Flux<Message<ByteBuffer, ByteBuffer>> f = Flux.create(emitter -> {
        emitter.onRequest(n -> {
            monosListener.addListener(() -> {
                BaseRedisPubSubListener listener = new BaseRedisPubSubListener() {

                    @Override
                    public void onPatternMessage(CharSequence pattern, CharSequence channel, Object message) {
                        if (!patterns.containsKey(new ChannelName(pattern.toString()))) {
                            return;
                        }
                        emitter.next(new PatternMessage<>(ByteBuffer.wrap(pattern.toString().getBytes()), ByteBuffer.wrap(channel.toString().getBytes()), ByteBuffer.wrap((byte[]) message)));
                    }

                    @Override
                    public void onMessage(CharSequence channel, Object msg) {
                        if (!channels.containsKey(new ChannelName(channel.toString()))) {
                            return;
                        }
                        emitter.next(new ChannelMessage<>(ByteBuffer.wrap(channel.toString().getBytes()), ByteBuffer.wrap((byte[]) msg)));
                    }
                };
                disposable = () -> {
                    for (Entry<ChannelName, PubSubConnectionEntry> entry : channels.entrySet()) {
                        entry.getValue().removeListener(entry.getKey(), listener);
                    }
                    for (Entry<ChannelName, Collection<PubSubConnectionEntry>> entry : patterns.entrySet()) {
                        for (PubSubConnectionEntry pubSubConnectionEntry : entry.getValue()) {
                            pubSubConnectionEntry.removeListener(entry.getKey(), listener);
                        }
                    }
                };
                for (Entry<ChannelName, PubSubConnectionEntry> entry : channels.entrySet()) {
                    entry.getValue().addListener(entry.getKey(), listener);
                }
                for (Entry<ChannelName, Collection<PubSubConnectionEntry>> entry : patterns.entrySet()) {
                    for (PubSubConnectionEntry pubSubConnectionEntry : entry.getValue()) {
                        pubSubConnectionEntry.addListener(entry.getKey(), listener);
                    }
                }
                emitter.onDispose(disposable);
            });
        });
    });
    if (flux.compareAndSet(null, f)) {
        return f;
    }
    return flux.get();
}
Also used : PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ChannelName(org.redisson.client.ChannelName) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener)

Aggregations

ChannelName (org.redisson.client.ChannelName)14 CompletableFuture (java.util.concurrent.CompletableFuture)7 BaseRedisPubSubListener (org.redisson.client.BaseRedisPubSubListener)7 PubSubConnectionEntry (org.redisson.pubsub.PubSubConnectionEntry)6 ByteBuffer (java.nio.ByteBuffer)4 StandardCharsets (java.nio.charset.StandardCharsets)4 java.util (java.util)4 Entry (java.util.Map.Entry)4 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Collectors (java.util.stream.Collectors)4 RedisPubSubListener (org.redisson.client.RedisPubSubListener)4 ByteArrayCodec (org.redisson.client.codec.ByteArrayCodec)4 Codec (org.redisson.client.codec.Codec)4 PubSubType (org.redisson.client.protocol.pubsub.PubSubType)4 ConnectionManager (org.redisson.connection.ConnectionManager)4 PublishSubscribeService (org.redisson.pubsub.PublishSubscribeService)4 ReactiveSubscription (org.springframework.data.redis.connection.ReactiveSubscription)4 SubscriptionListener (org.springframework.data.redis.connection.SubscriptionListener)4 Disposable (reactor.core.Disposable)4