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

com.lambdaworks.redis.pubsub.PubSubCommandBuilder 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 static com.lambdaworks.redis.protocol.CommandKeyword.CHANNELS;
import static com.lambdaworks.redis.protocol.CommandKeyword.NUMSUB;
import static com.lambdaworks.redis.protocol.CommandType.*;

import java.util.List;
import java.util.Map;

import com.lambdaworks.redis.codec.RedisCodec;
import com.lambdaworks.redis.internal.LettuceAssert;
import com.lambdaworks.redis.output.CommandOutput;
import com.lambdaworks.redis.output.IntegerOutput;
import com.lambdaworks.redis.output.KeyListOutput;
import com.lambdaworks.redis.output.MapOutput;
import com.lambdaworks.redis.protocol.BaseRedisCommandBuilder;
import com.lambdaworks.redis.protocol.Command;
import com.lambdaworks.redis.protocol.CommandArgs;
import com.lambdaworks.redis.protocol.CommandType;

/**
 * Dedicated pub/sub command builder to build pub/sub commands.
 * 
 * @author Mark Paluch
 * @since 4.2
 */
@SuppressWarnings("varargs")
class PubSubCommandBuilder extends BaseRedisCommandBuilder {

    static final String MUST_NOT_BE_EMPTY = "must not be empty";

    PubSubCommandBuilder(RedisCodec codec) {
        super(codec);
    }

    Command publish(K channel, V message) {
        CommandArgs args = new PubSubCommandArgs<>(codec).addKey(channel).addValue(message);
        return createCommand(PUBLISH, new IntegerOutput<>(codec), args);
    }

    Command> pubsubChannels(K pattern) {
        CommandArgs args = new PubSubCommandArgs<>(codec).add(CHANNELS).addKey(pattern);
        return createCommand(PUBSUB, new KeyListOutput<>(codec), args);
    }

    @SafeVarargs
    final Command> pubsubNumsub(K... patterns) {
        LettuceAssert.notEmpty(patterns, "patterns " + MUST_NOT_BE_EMPTY);

        CommandArgs args = new PubSubCommandArgs<>(codec).add(NUMSUB).addKeys(patterns);
        return createCommand(PUBSUB, new MapOutput<>((RedisCodec) codec), args);
    }

    @SafeVarargs
    final Command psubscribe(K... patterns) {
        LettuceAssert.notEmpty(patterns, "patterns " + MUST_NOT_BE_EMPTY);

        return pubSubCommand(PSUBSCRIBE, new PubSubOutput<>(codec), patterns);
    }

    @SafeVarargs
    final Command punsubscribe(K... patterns) {
        return pubSubCommand(PUNSUBSCRIBE, new PubSubOutput<>(codec), patterns);
    }

    @SafeVarargs
    final Command subscribe(K... channels) {
        LettuceAssert.notEmpty(channels, "channels " + MUST_NOT_BE_EMPTY);

        return pubSubCommand(SUBSCRIBE, new PubSubOutput<>(codec), channels);
    }

    @SafeVarargs
    final Command unsubscribe(K... channels) {
        return pubSubCommand(UNSUBSCRIBE, new PubSubOutput<>(codec), channels);
    }

     Command pubSubCommand(CommandType type, CommandOutput output, K... keys) {
        return new Command<>(type, output, new PubSubCommandArgs<>(codec).addKeys(keys));
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy