Search in sources :

Example 1 with SubscriptionListener

use of org.springframework.data.redis.connection.SubscriptionListener in project redisson by redisson.

the class RedissonReactiveSubscription method pSubscribe.

@Override
public Mono<Void> pSubscribe(ByteBuffer... patterns) {
    monosListener.acquire();
    return Mono.defer(() -> {
        List<CompletableFuture<?>> futures = new ArrayList<>();
        for (ByteBuffer channel : patterns) {
            ChannelName cn = toChannelName(channel);
            CompletableFuture<Collection<PubSubConnectionEntry>> f = subscribeService.psubscribe(cn, ByteArrayCodec.INSTANCE, subscriptionListener);
            f = f.whenComplete((res, e) -> RedissonReactiveSubscription.this.patterns.put(cn, res));
            futures.add(f);
        }
        CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        future = future.whenComplete((r, e) -> {
            monosListener.release();
        });
        return Mono.fromFuture(future);
    });
}
Also used : PubSubType(org.redisson.client.protocol.pubsub.PubSubType) PublishSubscribeService(org.redisson.pubsub.PublishSubscribeService) java.util(java.util) Disposable(reactor.core.Disposable) Codec(org.redisson.client.codec.Codec) PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ConnectionManager(org.redisson.connection.ConnectionManager) ByteArrayCodec(org.redisson.client.codec.ByteArrayCodec) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Mono(reactor.core.publisher.Mono) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) RedisPubSubListener(org.redisson.client.RedisPubSubListener) SubscriptionListener(org.springframework.data.redis.connection.SubscriptionListener) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) Flux(reactor.core.publisher.Flux) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) Entry(java.util.Map.Entry) ChannelName(org.redisson.client.ChannelName) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelName(org.redisson.client.ChannelName) ByteBuffer(java.nio.ByteBuffer)

Example 2 with SubscriptionListener

use of org.springframework.data.redis.connection.SubscriptionListener in project redisson by redisson.

the class RedissonReactiveSubscription method subscribe.

@Override
public Mono<Void> subscribe(ByteBuffer... channels) {
    monosListener.acquire();
    return Mono.defer(() -> {
        List<CompletableFuture<?>> futures = new ArrayList<>();
        for (ByteBuffer channel : channels) {
            ChannelName cn = toChannelName(channel);
            CompletableFuture<PubSubConnectionEntry> f = subscribeService.subscribe(ByteArrayCodec.INSTANCE, cn, subscriptionListener);
            f = f.whenComplete((res, e) -> RedissonReactiveSubscription.this.channels.put(cn, res));
            futures.add(f);
        }
        CompletableFuture<Void> future = CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
        future = future.whenComplete((r, e) -> {
            monosListener.release();
        });
        return Mono.fromFuture(future);
    });
}
Also used : PubSubType(org.redisson.client.protocol.pubsub.PubSubType) PublishSubscribeService(org.redisson.pubsub.PublishSubscribeService) java.util(java.util) Disposable(reactor.core.Disposable) Codec(org.redisson.client.codec.Codec) PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ConnectionManager(org.redisson.connection.ConnectionManager) ByteArrayCodec(org.redisson.client.codec.ByteArrayCodec) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Mono(reactor.core.publisher.Mono) CompletableFuture(java.util.concurrent.CompletableFuture) AtomicReference(java.util.concurrent.atomic.AtomicReference) Collectors(java.util.stream.Collectors) ByteBuffer(java.nio.ByteBuffer) StandardCharsets(java.nio.charset.StandardCharsets) RedisPubSubListener(org.redisson.client.RedisPubSubListener) SubscriptionListener(org.springframework.data.redis.connection.SubscriptionListener) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener) Flux(reactor.core.publisher.Flux) ReactiveSubscription(org.springframework.data.redis.connection.ReactiveSubscription) Entry(java.util.Map.Entry) ChannelName(org.redisson.client.ChannelName) CompletableFuture(java.util.concurrent.CompletableFuture) PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ChannelName(org.redisson.client.ChannelName) ByteBuffer(java.nio.ByteBuffer)

Aggregations

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