Search in sources :

Example 6 with ChannelName

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

the class RedissonReactiveSubscription method unsubscribe.

@Override
public Mono<Void> unsubscribe(ByteBuffer... channels) {
    monosListener.acquire();
    return Mono.defer(() -> {
        List<CompletableFuture<?>> futures = new ArrayList<>(channels.length);
        for (ByteBuffer channel : channels) {
            ChannelName cn = toChannelName(channel);
            CompletableFuture<Codec> f = subscribeService.unsubscribe(cn, PubSubType.UNSUBSCRIBE);
            f = f.whenComplete((res, e) -> {
                synchronized (RedissonReactiveSubscription.this.channels) {
                    PubSubConnectionEntry entry = RedissonReactiveSubscription.this.channels.get(cn);
                    if (!entry.hasListeners(cn)) {
                        RedissonReactiveSubscription.this.channels.remove(cn);
                    }
                }
            });
            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) Codec(org.redisson.client.codec.Codec) ByteArrayCodec(org.redisson.client.codec.ByteArrayCodec) PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ChannelName(org.redisson.client.ChannelName) ByteBuffer(java.nio.ByteBuffer)

Example 7 with ChannelName

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

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

the class RedissonSubscription method doSubscribe.

@Override
protected void doSubscribe(byte[]... channels) {
    List<CompletableFuture<?>> list = new ArrayList<>();
    for (byte[] channel : channels) {
        CompletableFuture<PubSubConnectionEntry> f = subscribeService.subscribe(ByteArrayCodec.INSTANCE, new ChannelName(channel), new BaseRedisPubSubListener() {

            @Override
            public void onMessage(CharSequence ch, Object message) {
                if (!Arrays.equals(((ChannelName) ch).getName(), channel)) {
                    return;
                }
                byte[] m = toBytes(message);
                DefaultMessage msg = new DefaultMessage(((ChannelName) ch).getName(), m);
                getListener().onMessage(msg, null);
            }
        });
        list.add(f);
    }
    for (CompletableFuture<?> future : list) {
        commandExecutor.get(future);
    }
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) PubSubConnectionEntry(org.redisson.pubsub.PubSubConnectionEntry) ChannelName(org.redisson.client.ChannelName) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener)

Example 9 with ChannelName

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

the class RedissonSubscription method doPsubscribe.

@Override
protected void doPsubscribe(byte[]... patterns) {
    List<CompletableFuture<?>> list = new ArrayList<>();
    for (byte[] channel : patterns) {
        CompletableFuture<Collection<PubSubConnectionEntry>> f = subscribeService.psubscribe(new ChannelName(channel), ByteArrayCodec.INSTANCE, new BaseRedisPubSubListener() {

            @Override
            public void onPatternMessage(CharSequence pattern, CharSequence ch, Object message) {
                if (!Arrays.equals(((ChannelName) pattern).getName(), channel)) {
                    return;
                }
                byte[] m = toBytes(message);
                DefaultMessage msg = new DefaultMessage(((ChannelName) ch).getName(), m);
                getListener().onMessage(msg, ((ChannelName) pattern).getName());
            }
        });
        list.add(f);
    }
    for (CompletableFuture<?> future : list) {
        commandExecutor.get(future);
    }
}
Also used : DefaultMessage(org.springframework.data.redis.connection.DefaultMessage) ArrayList(java.util.ArrayList) CompletableFuture(java.util.concurrent.CompletableFuture) ChannelName(org.redisson.client.ChannelName) Collection(java.util.Collection) BaseRedisPubSubListener(org.redisson.client.BaseRedisPubSubListener)

Example 10 with ChannelName

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

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