|
|
|
@ -18,37 +18,28 @@ package org.redisson.client;
|
|
|
|
|
import java.util.concurrent.ConcurrentLinkedQueue;
|
|
|
|
|
|
|
|
|
|
import org.redisson.client.handler.RedisData;
|
|
|
|
|
import org.redisson.client.protocol.Codec;
|
|
|
|
|
import org.redisson.client.protocol.Decoder;
|
|
|
|
|
import org.redisson.client.protocol.RedisCommand;
|
|
|
|
|
import org.redisson.client.protocol.RedisCommands;
|
|
|
|
|
import org.redisson.client.protocol.StringCodec;
|
|
|
|
|
import org.redisson.client.protocol.pubsub.MultiDecoder;
|
|
|
|
|
import org.redisson.client.protocol.pubsub.PubSubMessage;
|
|
|
|
|
import org.redisson.client.protocol.pubsub.PubSubMessageDecoder;
|
|
|
|
|
import org.redisson.client.protocol.pubsub.PubSubPatternMessage;
|
|
|
|
|
import org.redisson.client.protocol.pubsub.PubSubPatternMessageDecoder;
|
|
|
|
|
import org.redisson.client.protocol.pubsub.PubSubStatusDecoder;
|
|
|
|
|
import org.redisson.client.protocol.pubsub.PubSubStatusMessage;
|
|
|
|
|
|
|
|
|
|
import io.netty.channel.Channel;
|
|
|
|
|
import io.netty.channel.ChannelFuture;
|
|
|
|
|
import io.netty.util.AttributeKey;
|
|
|
|
|
import io.netty.util.concurrent.Future;
|
|
|
|
|
import io.netty.util.concurrent.Promise;
|
|
|
|
|
|
|
|
|
|
public class RedisPubSubConnection {
|
|
|
|
|
public class RedisPubSubConnection extends RedisConnection {
|
|
|
|
|
|
|
|
|
|
public static final AttributeKey<RedisPubSubConnection> CONNECTION = AttributeKey.valueOf("connection");
|
|
|
|
|
|
|
|
|
|
final ConcurrentLinkedQueue<RedisPubSubListener<Object>> listeners = new ConcurrentLinkedQueue<RedisPubSubListener<Object>>();
|
|
|
|
|
|
|
|
|
|
final Channel channel;
|
|
|
|
|
final RedisClient redisClient;
|
|
|
|
|
|
|
|
|
|
public RedisPubSubConnection(RedisClient redisClient, Channel channel) {
|
|
|
|
|
this.redisClient = redisClient;
|
|
|
|
|
this.channel = channel;
|
|
|
|
|
super(redisClient, channel);
|
|
|
|
|
|
|
|
|
|
channel.attr(CONNECTION).set(this);
|
|
|
|
|
}
|
|
|
|
@ -57,6 +48,10 @@ public class RedisPubSubConnection {
|
|
|
|
|
listeners.add(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void removeListener(RedisPubSubListener listener) {
|
|
|
|
|
listeners.remove(listener);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void onMessage(PubSubMessage message) {
|
|
|
|
|
for (RedisPubSubListener<Object> redisPubSubListener : listeners) {
|
|
|
|
|
redisPubSubListener.onMessage(message.getChannel(), message.getValue());
|
|
|
|
@ -78,28 +73,17 @@ public class RedisPubSubConnection {
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Future<PubSubStatusMessage> unsubscribe(String ... channel) {
|
|
|
|
|
return async(null, RedisCommands.UNSUBSCRIBE, channel);
|
|
|
|
|
return async((MultiDecoder)null, RedisCommands.UNSUBSCRIBE, channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public Future<PubSubStatusMessage> punsubscribe(String ... channel) {
|
|
|
|
|
return async(null, RedisCommands.PUNSUBSCRIBE, channel);
|
|
|
|
|
return async((MultiDecoder)null, RedisCommands.PUNSUBSCRIBE, channel);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// public <T, R> Future<R> async(Codec encoder, RedisCommand<T> command, Object ... params) {
|
|
|
|
|
// Promise<R> promise = redisClient.getBootstrap().group().next().<R>newPromise();
|
|
|
|
|
// channel.writeAndFlush(new RedisData<T, R>(promise, encoder, command, params));
|
|
|
|
|
// return promise;
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
public <T, R> Future<R> async(MultiDecoder<Object> nextDecoder, RedisCommand<T> command, Object ... params) {
|
|
|
|
|
public <T, R> Future<R> async(MultiDecoder<Object> messageDecoder, RedisCommand<T> command, Object ... params) {
|
|
|
|
|
Promise<R> promise = redisClient.getBootstrap().group().next().<R>newPromise();
|
|
|
|
|
channel.writeAndFlush(new RedisData<T, R>(promise, nextDecoder, null, command, params));
|
|
|
|
|
channel.writeAndFlush(new RedisData<T, R>(promise, messageDecoder, null, command, params));
|
|
|
|
|
return promise;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public ChannelFuture closeAsync() {
|
|
|
|
|
return channel.close();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|