use of org.redisson.client.protocol.pubsub.PubSubStatusMessage in project redisson by redisson.
the class CommandDecoder method handlePublishSubscribe.
private void handlePublishSubscribe(CommandData<Object, Object> data, List<Object> parts, Channel channel, final Object result) {
if (result instanceof PubSubStatusMessage) {
String channelName = ((PubSubStatusMessage) result).getChannel();
String operation = ((PubSubStatusMessage) result).getType().name().toLowerCase();
PubSubKey key = new PubSubKey(channelName, operation);
CommandData<Object, Object> d = pubSubChannels.get(key);
if (Arrays.asList(RedisCommands.PSUBSCRIBE.getName(), RedisCommands.SUBSCRIBE.getName()).contains(d.getCommand().getName())) {
pubSubChannels.remove(key);
pubSubMessageDecoders.put(channelName, d.getMessageDecoder());
}
if (Arrays.asList(RedisCommands.PUNSUBSCRIBE.getName(), RedisCommands.UNSUBSCRIBE.getName()).contains(d.getCommand().getName())) {
pubSubChannels.remove(key);
pubSubMessageDecoders.remove(channelName);
}
}
final RedisPubSubConnection pubSubConnection = RedisPubSubConnection.getFrom(channel);
executor.execute(new Runnable() {
@Override
public void run() {
if (result instanceof PubSubStatusMessage) {
pubSubConnection.onMessage((PubSubStatusMessage) result);
} else if (result instanceof PubSubMessage) {
pubSubConnection.onMessage((PubSubMessage) result);
} else {
pubSubConnection.onMessage((PubSubPatternMessage) result);
}
}
});
}
use of org.redisson.client.protocol.pubsub.PubSubStatusMessage in project redisson by redisson.
the class CommandPubSubDecoder method decodeResult.
@Override
protected void decodeResult(CommandData<Object, Object> data, List<Object> parts, Channel channel, Object result) throws IOException {
try {
if (config.getExecutor().isShutdown()) {
return;
}
} catch (IllegalStateException e) {
// arise in JBOSS. skipped
}
if (result instanceof Message) {
checkpoint();
RedisPubSubConnection pubSubConnection = RedisPubSubConnection.getFrom(channel);
ChannelName channelName = ((Message) result).getChannel();
if (result instanceof PubSubStatusMessage) {
String operation = ((PubSubStatusMessage) result).getType().name().toLowerCase();
PubSubKey key = new PubSubKey(channelName, operation);
CommandData<Object, Object> d = commands.get(key);
if (Arrays.asList(RedisCommands.PSUBSCRIBE.getName(), RedisCommands.SUBSCRIBE.getName()).contains(d.getCommand().getName())) {
commands.remove(key);
entries.put(channelName, new PubSubEntry(d.getMessageDecoder()));
}
if (Arrays.asList(RedisCommands.PUNSUBSCRIBE.getName(), RedisCommands.UNSUBSCRIBE.getName()).contains(d.getCommand().getName())) {
commands.remove(key);
if (result instanceof PubSubPatternMessage) {
channelName = ((PubSubPatternMessage) result).getPattern();
}
PubSubEntry entry = entries.remove(channelName);
if (config.isKeepAlive()) {
enqueueMessage(result, pubSubConnection, entry);
}
}
}
if (config.isKeepAlive()) {
if (result instanceof PubSubPatternMessage) {
channelName = ((PubSubPatternMessage) result).getPattern();
}
PubSubEntry entry = entries.get(channelName);
if (entry != null) {
enqueueMessage(result, pubSubConnection, entry);
}
} else {
config.getExecutor().execute(new Runnable() {
@Override
public void run() {
if (result instanceof PubSubStatusMessage) {
pubSubConnection.onMessage((PubSubStatusMessage) result);
} else if (result instanceof PubSubMessage) {
pubSubConnection.onMessage((PubSubMessage) result);
} else if (result instanceof PubSubPatternMessage) {
pubSubConnection.onMessage((PubSubPatternMessage) result);
}
}
});
}
} else {
if (data != null && data.getCommand().getName().equals("PING")) {
super.decodeResult(data, parts, channel, result);
}
}
}
use of org.redisson.client.protocol.pubsub.PubSubStatusMessage in project redisson by redisson.
the class PublishSubscribeService method unsubscribe.
public CompletableFuture<Void> unsubscribe(PubSubType topicType, ChannelName channelName) {
PubSubConnectionEntry entry = name2PubSubConnection.remove(createKey(channelName));
if (entry == null || connectionManager.isShuttingDown()) {
return CompletableFuture.completedFuture(null);
}
AtomicBoolean executed = new AtomicBoolean();
CompletableFuture<Void> result = new CompletableFuture<>();
BaseRedisPubSubListener listener = new BaseRedisPubSubListener() {
@Override
public boolean onStatus(PubSubType type, CharSequence channel) {
if (type == topicType && channel.equals(channelName)) {
executed.set(true);
if (entry.release() == 1) {
MasterSlaveEntry msEntry = getEntry(channelName);
msEntry.returnPubSubConnection(entry.getConnection());
}
result.complete(null);
return true;
}
return false;
}
};
ChannelFuture future;
if (topicType == PubSubType.UNSUBSCRIBE) {
future = entry.unsubscribe(channelName, listener);
} else {
future = entry.punsubscribe(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;
}
use of org.redisson.client.protocol.pubsub.PubSubStatusMessage in project redisson by redisson.
the class RedisPubSubConnection method onDisconnect.
@Override
public void onDisconnect() {
Set<String> channels = new HashSet<String>();
Set<String> pchannels = new HashSet<String>();
synchronized (this) {
channels.addAll(unsubscibedChannels);
pchannels.addAll(punsubscibedChannels);
}
for (String channel : channels) {
onMessage(new PubSubStatusMessage(PubSubType.UNSUBSCRIBE, channel));
}
for (String channel : pchannels) {
onMessage(new PubSubStatusMessage(PubSubType.PUNSUBSCRIBE, channel));
}
}
use of org.redisson.client.protocol.pubsub.PubSubStatusMessage in project redisson by redisson.
the class RedisPubSubConnection method unsubscribe.
public void unsubscribe(final String... channels) {
synchronized (this) {
for (String ch : channels) {
this.channels.remove(ch);
unsubscibedChannels.add(ch);
}
}
ChannelFuture future = async((MultiDecoder) null, RedisCommands.UNSUBSCRIBE, 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.UNSUBSCRIBE, channel));
}
}
}
});
}
Aggregations