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