com.github.lontime.shaded.org.redisson.RedissonPatternTopic Maven / Gradle / Ivy
The newest version!
/**
* Copyright (c) 2013-2021 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 com.github.lontime.shaded.org.redisson;
import com.github.lontime.shaded.org.redisson.api.RFuture;
import com.github.lontime.shaded.org.redisson.api.RPatternTopic;
import com.github.lontime.shaded.org.redisson.api.listener.PatternMessageListener;
import com.github.lontime.shaded.org.redisson.api.listener.PatternStatusListener;
import com.github.lontime.shaded.org.redisson.client.ChannelName;
import com.github.lontime.shaded.org.redisson.client.RedisPubSubListener;
import com.github.lontime.shaded.org.redisson.client.RedisTimeoutException;
import com.github.lontime.shaded.org.redisson.client.codec.Codec;
import com.github.lontime.shaded.org.redisson.client.protocol.pubsub.PubSubType;
import com.github.lontime.shaded.org.redisson.command.CommandAsyncExecutor;
import com.github.lontime.shaded.org.redisson.config.MasterSlaveServersConfig;
import com.github.lontime.shaded.org.redisson.misc.CompletableFutureWrapper;
import com.github.lontime.shaded.org.redisson.pubsub.AsyncSemaphore;
import com.github.lontime.shaded.org.redisson.pubsub.PubSubConnectionEntry;
import com.github.lontime.shaded.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.getConnectionManager().getCodec(), commandExecutor, name);
}
protected RedissonPatternTopic(Codec codec, CommandAsyncExecutor commandExecutor, String name) {
this.commandExecutor = commandExecutor;
this.name = name;
this.channelName = new ChannelName(name);
this.codec = 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);
}
protected void acquire(AsyncSemaphore semaphore) {
MasterSlaveServersConfig config = commandExecutor.getConnectionManager().getConfig();
int timeout = config.getTimeout() + config.getRetryInterval() * config.getRetryAttempts();
if (!semaphore.tryAcquire(timeout)) {
throw new RedisTimeoutException("Remove listeners operation timeout: (" + timeout + "ms) for " + name + " topic");
}
}
@Override
public RFuture removeListenerAsync(int listenerId) {
CompletableFuture f = subscribeService.removeListenerAsync(PubSubType.PUNSUBSCRIBE, channelName, listenerId);
return new CompletableFutureWrapper<>(f);
}
@Override
public void removeListener(int listenerId) {
commandExecutor.get(removeListenerAsync(listenerId).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);
}
}