All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.lambdaworks.redis.pubsub.RedisPubSubReactiveCommandsImpl Maven / Gradle / Ivy

Go to download

Advanced and thread-safe Java Redis client for synchronous, asynchronous, and reactive usage. Supports Cluster, Sentinel, Pipelining, Auto-Reconnect, Codecs and much more.

The newest version!
/*
 * Copyright 2011-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.lambdaworks.redis.pubsub;

import java.util.Map;

import com.lambdaworks.redis.RedisReactiveCommandsImpl;
import com.lambdaworks.redis.codec.RedisCodec;
import com.lambdaworks.redis.pubsub.api.reactive.ChannelMessage;
import com.lambdaworks.redis.pubsub.api.reactive.PatternMessage;
import com.lambdaworks.redis.pubsub.api.reactive.RedisPubSubReactiveCommands;

import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;

/**
 * A reactive and thread-safe API for a Redis pub/sub connection.
 * 
 * @param  Key type.
 * @param  Value type.
 * @author Mark Paluch
 */
public class RedisPubSubReactiveCommandsImpl extends RedisReactiveCommandsImpl
        implements RedisPubSubReactiveCommands {

    private PubSubCommandBuilder commandBuilder;

    /**
     * Initialize a new connection.
     *
     * @param connection the connection .
     * @param codec Codec used to encode/decode keys and values.
     */
    public RedisPubSubReactiveCommandsImpl(StatefulRedisPubSubConnection connection, RedisCodec codec) {
        super(connection, codec);
        this.connection = connection;
        this.commandBuilder = new PubSubCommandBuilder<>(codec);
    }

    /**
     * Add a new listener.
     * 
     * @param listener Listener.
     */
    @Override
    public void addListener(RedisPubSubListener listener) {
        getStatefulConnection().addListener(listener);
    }

    @Override
    public Flux> observePatterns() {
        return observePatterns(FluxSink.OverflowStrategy.BUFFER);
    }

    @Override
    public Flux> observePatterns(FluxSink.OverflowStrategy overflowStrategy) {
        return Flux.create(sink -> {

            RedisPubSubAdapter listener = new RedisPubSubAdapter() {

                @Override
                public void message(K pattern, K channel, V message) {
                    sink.next(new PatternMessage<>(pattern, channel, message));
                }
            };

            addListener(listener);
            sink.setCancellation(() -> {
                removeListener(listener);
            });

        }, overflowStrategy);
    }

    @Override
    public Flux> observeChannels() {
        return observeChannels(FluxSink.OverflowStrategy.BUFFER);
    }

    @Override
    public Flux> observeChannels(FluxSink.OverflowStrategy overflowStrategy) {

        return Flux.create(sink -> {

            RedisPubSubAdapter listener = new RedisPubSubAdapter() {

                @Override
                public void message(K channel, V message) {
                    sink.next(new ChannelMessage<>(channel, message));
                }
            };

            addListener(listener);

            sink.setCancellation(() -> {
                removeListener(listener);
            });

        }, overflowStrategy);
    }

    /**
     * Remove an existing listener.
     * 
     * @param listener Listener.
     */
    @Override
    public void removeListener(RedisPubSubListener listener) {
        getStatefulConnection().removeListener(listener);
    }

    @Override
    public Mono psubscribe(K... patterns) {
        return createMono(() -> commandBuilder.psubscribe(patterns)).then();
    }

    @Override
    public Mono punsubscribe(K... patterns) {
        return createFlux(() -> commandBuilder.punsubscribe(patterns)).then();
    }

    @Override
    public Mono subscribe(K... channels) {
        return createFlux(() -> commandBuilder.subscribe(channels)).then();
    }

    @Override
    public Mono unsubscribe(K... channels) {
        return createFlux(() -> commandBuilder.unsubscribe(channels)).then();
    }

    @Override
    public Mono publish(K channel, V message) {
        return createMono(() -> commandBuilder.publish(channel, message));
    }

    @Override
    public Flux pubsubChannels(K channel) {
        return createDissolvingFlux(() -> commandBuilder.pubsubChannels(channel));
    }

    @Override
    public Mono> pubsubNumsub(K... channels) {
        return createMono(() -> commandBuilder.pubsubNumsub(channels));
    }

    @Override
    @SuppressWarnings("unchecked")
    public StatefulRedisPubSubConnection getStatefulConnection() {
        return (StatefulRedisPubSubConnection) super.getStatefulConnection();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy