
io.lettuce.core.pubsub.RedisPubSubReactiveCommandsImpl Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of lettuce-core Show documentation
Show all versions of lettuce-core Show documentation
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-Present, Redis Ltd. and Contributors
* All rights reserved.
*
* Licensed under the MIT License.
*
* This file contains contributions from third-party contributors
* 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
*
* https://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 io.lettuce.core.pubsub;
import java.util.Map;
import reactor.core.publisher.Flux;
import reactor.core.publisher.FluxSink;
import reactor.core.publisher.Mono;
import io.lettuce.core.RedisReactiveCommandsImpl;
import io.lettuce.core.codec.RedisCodec;
import io.lettuce.core.protocol.Command;
import io.lettuce.core.pubsub.api.reactive.ChannelMessage;
import io.lettuce.core.pubsub.api.reactive.PatternMessage;
import io.lettuce.core.pubsub.api.reactive.RedisPubSubReactiveCommands;
/**
* A reactive and thread-safe API for a Redis pub/sub connection.
*
* @param Key type.
* @param Value type.
* @author Mark Paluch
* @author Ali Takavci
* @since 5.0
*/
public class RedisPubSubReactiveCommandsImpl extends RedisReactiveCommandsImpl
implements RedisPubSubReactiveCommands {
private final 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, null);
this.commandBuilder = new PubSubCommandBuilder<>(codec);
}
@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));
}
};
StatefulRedisPubSubConnection statefulConnection = getStatefulConnection();
statefulConnection.addListener(listener);
sink.onDispose(() -> {
statefulConnection.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));
}
};
StatefulRedisPubSubConnection statefulConnection = getStatefulConnection();
statefulConnection.addListener(listener);
sink.onDispose(() -> {
statefulConnection.removeListener(listener);
});
}, overflowStrategy);
}
@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
© 2015 - 2025 Weber Informatics LLC | Privacy Policy