|
|
@ -17,171 +17,171 @@ import com.fr.third.redis.clients.jedis.util.SafeEncoder; |
|
|
|
|
|
|
|
|
|
|
|
public abstract class JedisPubSub { |
|
|
|
public abstract class JedisPubSub { |
|
|
|
|
|
|
|
|
|
|
|
private static final String JEDIS_SUBSCRIPTION_MESSAGE = "JedisPubSub is not subscribed to a Jedis instance."; |
|
|
|
private static final String JEDIS_SUBSCRIPTION_MESSAGE = "JedisPubSub is not subscribed to a Jedis instance."; |
|
|
|
private int subscribedChannels = 0; |
|
|
|
private int subscribedChannels = 0; |
|
|
|
private volatile Client client; |
|
|
|
private volatile Client client; |
|
|
|
|
|
|
|
|
|
|
|
public JedisPubSub() { |
|
|
|
public JedisPubSub() { |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public JedisPubSub(Client client) { |
|
|
|
public JedisPubSub(Client client) { |
|
|
|
this.client = client; |
|
|
|
this.client = client; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void onMessage(String channel, String message) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void onPMessage(String pattern, String channel, String message) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void onSubscribe(String channel, int subscribedChannels) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void onUnsubscribe(String channel, int subscribedChannels) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void onPUnsubscribe(String pattern, int subscribedChannels) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void onPSubscribe(String pattern, int subscribedChannels) { |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void onPong(String pattern) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void unsubscribe() { |
|
|
|
|
|
|
|
if (client == null) { |
|
|
|
|
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client.unsubscribe(); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void unsubscribe(String... channels) { |
|
|
|
public void onMessage(String channel, String message) { |
|
|
|
if (client == null) { |
|
|
|
} |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client.unsubscribe(channels); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void subscribe(String... channels) { |
|
|
|
public void onPMessage(String pattern, String channel, String message) { |
|
|
|
if (client == null) { |
|
|
|
} |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client.subscribe(channels); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void psubscribe(String... patterns) { |
|
|
|
public void onSubscribe(String channel, int subscribedChannels) { |
|
|
|
if (client == null) { |
|
|
|
} |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client.psubscribe(patterns); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void punsubscribe() { |
|
|
|
public void onUnsubscribe(String channel, int subscribedChannels) { |
|
|
|
if (client == null) { |
|
|
|
} |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client.punsubscribe(); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void punsubscribe(String... patterns) { |
|
|
|
public void onPUnsubscribe(String pattern, int subscribedChannels) { |
|
|
|
if (client == null) { |
|
|
|
} |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client.punsubscribe(patterns); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public void ping() { |
|
|
|
|
|
|
|
if (client == null) { |
|
|
|
|
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
client.ping(); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public boolean isSubscribed() { |
|
|
|
public void onPSubscribe(String pattern, int subscribedChannels) { |
|
|
|
return subscribedChannels > 0; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public void onPong(String pattern) { |
|
|
|
public void proceedWithPatterns(Client client, String... patterns) { |
|
|
|
|
|
|
|
this.client = client; |
|
|
|
} |
|
|
|
client.psubscribe(patterns); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
public void unsubscribe() { |
|
|
|
process(client); |
|
|
|
if (client == null) { |
|
|
|
} |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
|
|
|
|
} |
|
|
|
public void proceed(Client client, String... channels) { |
|
|
|
client.unsubscribe(); |
|
|
|
this.client = client; |
|
|
|
client.flush(); |
|
|
|
client.subscribe(channels); |
|
|
|
} |
|
|
|
client.flush(); |
|
|
|
|
|
|
|
process(client); |
|
|
|
public void unsubscribe(String... channels) { |
|
|
|
} |
|
|
|
if (client == null) { |
|
|
|
|
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
private void process(Client client) { |
|
|
|
} |
|
|
|
|
|
|
|
client.unsubscribe(channels); |
|
|
|
do { |
|
|
|
client.flush(); |
|
|
|
List<Object> reply = client.getRawObjectMultiBulkReply(); |
|
|
|
} |
|
|
|
final Object firstObj = reply.get(0); |
|
|
|
|
|
|
|
if (!(firstObj instanceof byte[])) { |
|
|
|
public void subscribe(String... channels) { |
|
|
|
throw new JedisException("Unknown message type: " + firstObj); |
|
|
|
if (client == null) { |
|
|
|
} |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
final byte[] resp = (byte[]) firstObj; |
|
|
|
} |
|
|
|
if (Arrays.equals(SUBSCRIBE.raw, resp)) { |
|
|
|
client.subscribe(channels); |
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
client.flush(); |
|
|
|
final byte[] bchannel = (byte[]) reply.get(1); |
|
|
|
} |
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
|
|
|
|
onSubscribe(strchannel, subscribedChannels); |
|
|
|
public void psubscribe(String... patterns) { |
|
|
|
} else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) { |
|
|
|
if (client == null) { |
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
final byte[] bchannel = (byte[]) reply.get(1); |
|
|
|
} |
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
client.psubscribe(patterns); |
|
|
|
onUnsubscribe(strchannel, subscribedChannels); |
|
|
|
client.flush(); |
|
|
|
} else if (Arrays.equals(MESSAGE.raw, resp)) { |
|
|
|
} |
|
|
|
final byte[] bchannel = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final byte[] bmesg = (byte[]) reply.get(2); |
|
|
|
public void punsubscribe() { |
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
if (client == null) { |
|
|
|
final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg); |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
onMessage(strchannel, strmesg); |
|
|
|
} |
|
|
|
} else if (Arrays.equals(PMESSAGE.raw, resp)) { |
|
|
|
client.punsubscribe(); |
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
client.flush(); |
|
|
|
final byte[] bchannel = (byte[]) reply.get(2); |
|
|
|
} |
|
|
|
final byte[] bmesg = (byte[]) reply.get(3); |
|
|
|
|
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
public void punsubscribe(String... patterns) { |
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
if (client == null) { |
|
|
|
final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg); |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
onPMessage(strpattern, strchannel, strmesg); |
|
|
|
} |
|
|
|
} else if (Arrays.equals(PSUBSCRIBE.raw, resp)) { |
|
|
|
client.punsubscribe(patterns); |
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
client.flush(); |
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
} |
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
|
|
|
|
onPSubscribe(strpattern, subscribedChannels); |
|
|
|
public void ping() { |
|
|
|
} else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) { |
|
|
|
if (client == null) { |
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
throw new JedisConnectionException(JEDIS_SUBSCRIPTION_MESSAGE); |
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
} |
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
client.ping(); |
|
|
|
onPUnsubscribe(strpattern, subscribedChannels); |
|
|
|
client.flush(); |
|
|
|
} else if (Arrays.equals(PONG.raw, resp)) { |
|
|
|
} |
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
public boolean isSubscribed() { |
|
|
|
onPong(strpattern); |
|
|
|
return subscribedChannels > 0; |
|
|
|
} else { |
|
|
|
} |
|
|
|
throw new JedisException("Unknown message type: " + firstObj); |
|
|
|
|
|
|
|
} |
|
|
|
public void proceedWithPatterns(Client client, String... patterns) { |
|
|
|
} while (isSubscribed()); |
|
|
|
this.client = client; |
|
|
|
|
|
|
|
client.psubscribe(patterns); |
|
|
|
/* Invalidate instance since this thread is no longer listening */ |
|
|
|
client.flush(); |
|
|
|
this.client = null; |
|
|
|
process(client); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public int getSubscribedChannels() { |
|
|
|
public void proceed(Client client, String... channels) { |
|
|
|
return subscribedChannels; |
|
|
|
this.client = client; |
|
|
|
} |
|
|
|
client.subscribe(channels); |
|
|
|
|
|
|
|
client.flush(); |
|
|
|
|
|
|
|
process(client); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private void process(Client client) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
do { |
|
|
|
|
|
|
|
List<Object> reply = client.getRawObjectMultiBulkReply(); |
|
|
|
|
|
|
|
final Object firstObj = reply.get(0); |
|
|
|
|
|
|
|
if (!(firstObj instanceof byte[])) { |
|
|
|
|
|
|
|
throw new JedisException("Unknown message type: " + firstObj); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
final byte[] resp = (byte[]) firstObj; |
|
|
|
|
|
|
|
if (Arrays.equals(SUBSCRIBE.raw, resp)) { |
|
|
|
|
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
|
|
|
|
final byte[] bchannel = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
|
|
|
|
onSubscribe(strchannel, subscribedChannels); |
|
|
|
|
|
|
|
} else if (Arrays.equals(UNSUBSCRIBE.raw, resp)) { |
|
|
|
|
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
|
|
|
|
final byte[] bchannel = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
|
|
|
|
onUnsubscribe(strchannel, subscribedChannels); |
|
|
|
|
|
|
|
} else if (Arrays.equals(MESSAGE.raw, resp)) { |
|
|
|
|
|
|
|
final byte[] bchannel = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final byte[] bmesg = (byte[]) reply.get(2); |
|
|
|
|
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
|
|
|
|
final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg); |
|
|
|
|
|
|
|
onMessage(strchannel, strmesg); |
|
|
|
|
|
|
|
} else if (Arrays.equals(PMESSAGE.raw, resp)) { |
|
|
|
|
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final byte[] bchannel = (byte[]) reply.get(2); |
|
|
|
|
|
|
|
final byte[] bmesg = (byte[]) reply.get(3); |
|
|
|
|
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
|
|
|
|
final String strchannel = (bchannel == null) ? null : SafeEncoder.encode(bchannel); |
|
|
|
|
|
|
|
final String strmesg = (bmesg == null) ? null : SafeEncoder.encode(bmesg); |
|
|
|
|
|
|
|
onPMessage(strpattern, strchannel, strmesg); |
|
|
|
|
|
|
|
} else if (Arrays.equals(PSUBSCRIBE.raw, resp)) { |
|
|
|
|
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
|
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
|
|
|
|
onPSubscribe(strpattern, subscribedChannels); |
|
|
|
|
|
|
|
} else if (Arrays.equals(PUNSUBSCRIBE.raw, resp)) { |
|
|
|
|
|
|
|
subscribedChannels = ((Long) reply.get(2)).intValue(); |
|
|
|
|
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
|
|
|
|
onPUnsubscribe(strpattern, subscribedChannels); |
|
|
|
|
|
|
|
} else if (Arrays.equals(PONG.raw, resp)) { |
|
|
|
|
|
|
|
final byte[] bpattern = (byte[]) reply.get(1); |
|
|
|
|
|
|
|
final String strpattern = (bpattern == null) ? null : SafeEncoder.encode(bpattern); |
|
|
|
|
|
|
|
onPong(strpattern); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
throw new JedisException("Unknown message type: " + firstObj); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} while (isSubscribed()); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Invalidate instance since this thread is no longer listening */ |
|
|
|
|
|
|
|
this.client = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public int getSubscribedChannels() { |
|
|
|
|
|
|
|
return subscribedChannels; |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |