Search in sources :

Example 1 with PubSubPatternStatusListener

use of org.redisson.PubSubPatternStatusListener in project redisson by redisson.

the class PublishSubscribeService method psubscribe.

public CompletableFuture<Collection<PubSubConnectionEntry>> psubscribe(ChannelName channelName, Codec codec, RedisPubSubListener<?>... listeners) {
    if (isMultiEntity(channelName)) {
        Collection<MasterSlaveEntry> entrySet = connectionManager.getEntrySet();
        AtomicInteger statusCounter = new AtomicInteger(entrySet.size());
        RedisPubSubListener[] ls = Arrays.stream(listeners).map(l -> {
            if (l instanceof PubSubPatternStatusListener) {
                return new PubSubPatternStatusListener((PubSubPatternStatusListener) l) {

                    @Override
                    public boolean onStatus(PubSubType type, CharSequence channel) {
                        if (statusCounter.decrementAndGet() == 0) {
                            return super.onStatus(type, channel);
                        }
                        return true;
                    }
                };
            }
            return l;
        }).toArray(RedisPubSubListener[]::new);
        List<CompletableFuture<PubSubConnectionEntry>> futures = new ArrayList<>();
        for (MasterSlaveEntry entry : entrySet) {
            CompletableFuture<PubSubConnectionEntry> future = subscribe(PubSubType.PSUBSCRIBE, codec, channelName, entry, ls);
            futures.add(future);
        }
        CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        return future.thenApply(r -> {
            return futures.stream().map(v -> v.getNow(null)).collect(Collectors.toList());
        });
    }
    CompletableFuture<PubSubConnectionEntry> f = subscribe(PubSubType.PSUBSCRIBE, codec, channelName, getEntry(channelName), listeners);
    return f.thenApply(res -> Collections.singletonList(res));
}
Also used : 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) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) MasterSlaveEntry(org.redisson.connection.MasterSlaveEntry) PubSubType(org.redisson.client.protocol.pubsub.PubSubType) PubSubPatternStatusListener(org.redisson.PubSubPatternStatusListener)

Example 2 with PubSubPatternStatusListener

use of org.redisson.PubSubPatternStatusListener in project redisson by redisson.

the class RedissonPatternTopicReactive method addListener.

@Override
public Publisher<Integer> addListener(PatternStatusListener listener) {
    RPromise<Integer> promise = commandExecutor.getConnectionManager().newPromise();
    addListener(new PubSubPatternStatusListener(listener, name), promise);
    return new NettyFuturePublisher<Integer>(promise);
}
Also used : PubSubPatternStatusListener(org.redisson.PubSubPatternStatusListener)

Aggregations

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