org.redisson.RedissonPatternTopic Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of redisson-all Show documentation
Show all versions of redisson-all Show documentation
Easy Redis Java client and Real-Time Data Platform. Valkey compatible. Sync/Async/RxJava3/Reactive API. Client side caching. Over 50 Redis based Java objects and services: JCache API, Apache Tomcat, Hibernate, Spring, Set, Multimap, SortedSet, Map, List, Queue, Deque, Semaphore, Lock, AtomicLong, Map Reduce, Bloom filter, Scheduler, RPC
/**
* Copyright (c) 2013-2024 Nikita Koksharov
*
* 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 org.redisson;
import org.redisson.api.RFuture;
import org.redisson.api.RPatternTopic;
import org.redisson.api.listener.PatternMessageListener;
import org.redisson.api.listener.PatternStatusListener;
import org.redisson.client.ChannelName;
import org.redisson.client.RedisPubSubListener;
import org.redisson.client.codec.Codec;
import org.redisson.client.codec.StringCodec;
import org.redisson.client.protocol.RedisCommands;
import org.redisson.client.protocol.pubsub.PubSubType;
import org.redisson.command.CommandAsyncExecutor;
import org.redisson.misc.CompletableFutureWrapper;
import org.redisson.pubsub.PubSubConnectionEntry;
import org.redisson.pubsub.PublishSubscribeService;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
/**
* Distributed topic implementation. Messages are delivered to all message listeners across Redis cluster.
*
* @author Nikita Koksharov
*
*/
public class RedissonPatternTopic implements RPatternTopic {
final PublishSubscribeService subscribeService;
final CommandAsyncExecutor commandExecutor;
private final String name;
private final ChannelName channelName;
private final Codec codec;
protected RedissonPatternTopic(CommandAsyncExecutor commandExecutor, String name) {
this(commandExecutor.getServiceManager().getCfg().getCodec(), commandExecutor, name);
}
public RedissonPatternTopic(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
this.commandExecutor = commandExecutor;
this.name = name;
this.channelName = new ChannelName(name);
this.codec = commandExecutor.getServiceManager().getCodec(codec);
this.subscribeService = commandExecutor.getConnectionManager().getSubscribeService();
}
@Override
public int addListener(PatternStatusListener listener) {
return addListener(new PubSubPatternStatusListener(listener, name));
}
@Override
public int addListener(Class type, PatternMessageListener listener) {
PubSubPatternMessageListener pubSubListener = new PubSubPatternMessageListener(type, listener, name);
return addListener(pubSubListener);
}
private int addListener(RedisPubSubListener> pubSubListener) {
CompletableFuture> future = subscribeService.psubscribe(channelName, codec, pubSubListener);
commandExecutor.get(future);
return System.identityHashCode(pubSubListener);
}
@Override
public RFuture addListenerAsync(PatternStatusListener listener) {
PubSubPatternStatusListener pubSubListener = new PubSubPatternStatusListener(listener, name);
return addListenerAsync(pubSubListener);
}
@Override
public RFuture addListenerAsync(Class type, PatternMessageListener listener) {
PubSubPatternMessageListener pubSubListener = new PubSubPatternMessageListener(type, listener, name);
return addListenerAsync(pubSubListener);
}
private RFuture addListenerAsync(RedisPubSubListener> pubSubListener) {
CompletableFuture> future = subscribeService.psubscribe(channelName, codec, pubSubListener);
CompletableFuture f = future.thenApply(res -> {
return System.identityHashCode(pubSubListener);
});
return new CompletableFutureWrapper<>(f);
}
@Override
public RFuture removeListenerAsync(Integer... ids) {
CompletableFuture f = subscribeService.removeListenerAsync(PubSubType.PUNSUBSCRIBE, channelName, ids);
return new CompletableFutureWrapper<>(f);
}
@Override
public void removeListener(Integer... ids) {
commandExecutor.get(removeListenerAsync(ids).toCompletableFuture());
}
@Override
public void removeAllListeners() {
commandExecutor.get(removeAllListenersAsync());
}
@Override
public RFuture removeAllListenersAsync() {
CompletableFuture f = subscribeService.removeAllListenersAsync(PubSubType.PUNSUBSCRIBE, channelName);
return new CompletableFutureWrapper<>(f);
}
@Override
public void removeListener(PatternMessageListener> listener) {
CompletableFuture future = subscribeService.removeListenerAsync(PubSubType.PUNSUBSCRIBE, channelName, listener);
commandExecutor.get(future);
}
@Override
public List getPatternNames() {
return Collections.singletonList(name);
}
@Override
public RFuture> getActiveTopicsAsync() {
return commandExecutor.writeAsync(name, StringCodec.INSTANCE, RedisCommands.PUBSUB_CHANNELS, name);
}
@Override
public List getActiveTopics() {
return commandExecutor.get(getActiveTopicsAsync());
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy