now it is possible to subscribe and unsubscribe on a JedisPubSub instance

This commit is contained in:
Jonathan Leibiusky
2010-11-02 23:18:24 -03:00
parent 7a377fd8e9
commit 08f8432215
2 changed files with 241 additions and 178 deletions

View File

@@ -9,7 +9,7 @@ public abstract class JedisPubSub {
public abstract void onMessage(String channel, String message);
public abstract void onPMessage(String pattern, String channel,
String message);
String message);
public abstract void onSubscribe(String channel, int subscribedChannels);
@@ -19,66 +19,74 @@ public abstract class JedisPubSub {
public abstract void onPSubscribe(String pattern, int subscribedChannels);
protected void unsubscribe() {
client.unsubscribe();
public void unsubscribe() {
client.unsubscribe();
}
protected void unsubscribe(String... channels) {
client.unsubscribe(channels);
public void unsubscribe(String... channels) {
client.unsubscribe(channels);
}
protected void subscribe(String... channels) {
client.subscribe(channels);
public void subscribe(String... channels) {
client.subscribe(channels);
}
public void psubscribe(String... patterns) {
client.psubscribe(patterns);
}
public void punsubscribe() {
client.punsubscribe();
}
public void punsubscribe(String... patterns) {
client.punsubscribe(patterns);
}
public boolean isSubscribed() {
return subscribedChannels > 0;
return subscribedChannels > 0;
}
public void proceedWithPatterns(Client client, String... patterns) {
this.client = client;
client.psubscribe(patterns);
process(client);
this.client = client;
client.psubscribe(patterns);
process(client);
}
public void proceed(Client client, String... channels) {
this.client = client;
client.subscribe(channels);
process(client);
this.client = client;
client.subscribe(channels);
process(client);
}
private void process(Client client) {
do {
List<Object> reply = client.getObjectMultiBulkReply();
if (reply.get(0).equals("subscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onSubscribe((String) reply.get(1), subscribedChannels);
} else if (reply.get(0).equals("unsubscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onUnsubscribe((String) reply.get(1), subscribedChannels);
} else if (reply.get(0).equals("message")) {
onMessage((String) reply.get(1), (String) reply.get(2));
} else if (reply.get(0).equals("pmessage")) {
onPMessage((String) reply.get(1), (String) reply.get(2),
(String) reply.get(3));
} else if (reply.get(0).equals("psubscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onPSubscribe((String) reply.get(1), subscribedChannels);
} else if (reply.get(0).equals("punsubscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onPUnsubscribe((String) reply.get(1), subscribedChannels);
} else {
throw new JedisException("Unknown message type: "
+ reply.get(0));
}
} while (isSubscribed());
do {
List<Object> reply = client.getObjectMultiBulkReply();
if (reply.get(0).equals("subscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onSubscribe((String) reply.get(1), subscribedChannels);
} else if (reply.get(0).equals("unsubscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onUnsubscribe((String) reply.get(1), subscribedChannels);
} else if (reply.get(0).equals("message")) {
onMessage((String) reply.get(1), (String) reply.get(2));
} else if (reply.get(0).equals("pmessage")) {
onPMessage((String) reply.get(1), (String) reply.get(2),
(String) reply.get(3));
} else if (reply.get(0).equals("psubscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onPSubscribe((String) reply.get(1), subscribedChannels);
} else if (reply.get(0).equals("punsubscribe")) {
subscribedChannels = ((Integer) reply.get(2)).intValue();
onPUnsubscribe((String) reply.get(1), subscribedChannels);
} else {
throw new JedisException("Unknown message type: "
+ reply.get(0));
}
} while (isSubscribed());
}
protected void punsubscribe() {
client.punsubscribe();
public int getSubscribedChannels() {
return subscribedChannels;
}
protected void punsubscribe(String... patterns) {
client.punsubscribe(patterns);
}
}
}