use of org.redisson.client.protocol.pubsub.PubSubStatusMessage in project redisson by redisson.
the class RedisPubSubConnection method punsubscribe.
public void punsubscribe(final String... channels) {
synchronized (this) {
for (String ch : channels) {
patternChannels.remove(ch);
punsubscibedChannels.add(ch);
}
}
ChannelFuture future = async((MultiDecoder) null, RedisCommands.PUNSUBSCRIBE, channels);
future.addListener(new FutureListener<Void>() {
@Override
public void operationComplete(Future<Void> future) throws Exception {
if (!future.isSuccess()) {
for (String channel : channels) {
removeDisconnectListener(channel);
onMessage(new PubSubStatusMessage(PubSubType.PUNSUBSCRIBE, channel));
}
}
}
});
}
use of org.redisson.client.protocol.pubsub.PubSubStatusMessage in project redisson by redisson.
the class PublishSubscribeService method unsubscribe.
private CompletableFuture<Codec> unsubscribe(ChannelName channelName, MasterSlaveEntry e, PubSubType topicType) {
if (connectionManager.isShuttingDown()) {
return CompletableFuture.completedFuture(null);
}
CompletableFuture<Codec> result = new CompletableFuture<>();
AsyncSemaphore lock = getSemaphore(channelName);
lock.acquire(() -> {
PubSubConnectionEntry entry = name2PubSubConnection.remove(new PubSubKey(channelName, e));
if (entry == null) {
lock.release();
result.complete(null);
return;
}
freePubSubLock.acquire(() -> {
PubSubEntry ee = entry2PubSubConnection.getOrDefault(e, new PubSubEntry());
Queue<PubSubConnectionEntry> freePubSubConnections = ee.getEntries();
freePubSubConnections.remove(entry);
freePubSubLock.release();
Codec entryCodec;
if (topicType == PubSubType.PUNSUBSCRIBE) {
entryCodec = entry.getConnection().getPatternChannels().get(channelName);
} else {
entryCodec = entry.getConnection().getChannels().get(channelName);
}
AtomicBoolean executed = new AtomicBoolean();
RedisPubSubListener<Object> listener = new BaseRedisPubSubListener() {
@Override
public boolean onStatus(PubSubType type, CharSequence channel) {
if (type == topicType && channel.equals(channelName)) {
executed.set(true);
lock.release();
result.complete(entryCodec);
return true;
}
return false;
}
};
ChannelFuture future;
if (topicType == PubSubType.PUNSUBSCRIBE) {
future = entry.punsubscribe(channelName, listener);
} else {
future = entry.unsubscribe(channelName, listener);
}
future.addListener((ChannelFutureListener) f -> {
if (!f.isSuccess()) {
return;
}
connectionManager.newTimeout(timeout -> {
if (executed.get()) {
return;
}
entry.getConnection().onMessage(new PubSubStatusMessage(topicType, channelName));
}, config.getTimeout(), TimeUnit.MILLISECONDS);
});
});
});
return result;
}
Aggregations