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

@@ -19,18 +19,30 @@ public abstract class JedisPubSub {
public abstract void onPSubscribe(String pattern, int subscribedChannels); public abstract void onPSubscribe(String pattern, int subscribedChannels);
protected void unsubscribe() { public void unsubscribe() {
client.unsubscribe(); client.unsubscribe();
} }
protected void unsubscribe(String... channels) { public void unsubscribe(String... channels) {
client.unsubscribe(channels); client.unsubscribe(channels);
} }
protected void subscribe(String... channels) { public void subscribe(String... channels) {
client.subscribe(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() { public boolean isSubscribed() {
return subscribedChannels > 0; return subscribedChannels > 0;
} }
@@ -74,11 +86,7 @@ public abstract class JedisPubSub {
} while (isSubscribed()); } while (isSubscribed());
} }
protected void punsubscribe() { public int getSubscribedChannels() {
client.punsubscribe(); return subscribedChannels;
}
protected void punsubscribe(String... patterns) {
client.punsubscribe(patterns);
} }
} }

View File

@@ -12,7 +12,7 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
@Test @Test
public void subscribe() throws UnknownHostException, IOException, public void subscribe() throws UnknownHostException, IOException,
InterruptedException { InterruptedException {
new Thread(new Runnable() { Thread t = new Thread(new Runnable() {
public void run() { public void run() {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
@@ -23,7 +23,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
fail(ex.getMessage()); fail(ex.getMessage());
} }
} }
}).start(); });
t.start();
jedis.subscribe(new JedisPubSub() { jedis.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) { public void onMessage(String channel, String message) {
assertEquals("foo", channel); assertEquals("foo", channel);
@@ -51,12 +52,13 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
String message) { String message) {
} }
}, "foo"); }, "foo");
t.join();
} }
@Test @Test
public void subscribeMany() throws UnknownHostException, IOException, public void subscribeMany() throws UnknownHostException, IOException,
InterruptedException { InterruptedException {
new Thread(new Runnable() { Thread t = new Thread(new Runnable() {
public void run() { public void run() {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
@@ -69,7 +71,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
fail(ex.getMessage()); fail(ex.getMessage());
} }
} }
}).start(); });
t.start();
jedis.subscribe(new JedisPubSub() { jedis.subscribe(new JedisPubSub() {
public void onMessage(String channel, String message) { public void onMessage(String channel, String message) {
unsubscribe(channel); unsubscribe(channel);
@@ -91,12 +94,13 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
String message) { String message) {
} }
}, "foo", "bar"); }, "foo", "bar");
t.join();
} }
@Test @Test
public void psubscribe() throws UnknownHostException, IOException, public void psubscribe() throws UnknownHostException, IOException,
InterruptedException { InterruptedException {
new Thread(new Runnable() { Thread t = new Thread(new Runnable() {
public void run() { public void run() {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
@@ -107,7 +111,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
fail(ex.getMessage()); fail(ex.getMessage());
} }
} }
}).start(); });
t.start();
jedis.psubscribe(new JedisPubSub() { jedis.psubscribe(new JedisPubSub() {
public void onMessage(String channel, String message) { public void onMessage(String channel, String message) {
} }
@@ -136,12 +141,13 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
punsubscribe(); punsubscribe();
} }
}, "foo.*"); }, "foo.*");
t.join();
} }
@Test @Test
public void psubscribeMany() throws UnknownHostException, IOException, public void psubscribeMany() throws UnknownHostException, IOException,
InterruptedException { InterruptedException {
new Thread(new Runnable() { Thread t = new Thread(new Runnable() {
public void run() { public void run() {
try { try {
Jedis j = createJedis(); Jedis j = createJedis();
@@ -154,7 +160,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
fail(ex.getMessage()); fail(ex.getMessage());
} }
} }
}).start(); });
t.start();
jedis.psubscribe(new JedisPubSub() { jedis.psubscribe(new JedisPubSub() {
public void onMessage(String channel, String message) { public void onMessage(String channel, String message) {
} }
@@ -176,5 +183,53 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
punsubscribe(pattern); punsubscribe(pattern);
} }
}, "foo.*", "bar.*"); }, "foo.*", "bar.*");
t.join();
}
@Test
public void subscribeLazily() throws UnknownHostException, IOException,
InterruptedException {
final JedisPubSub pubsub = new JedisPubSub() {
public void onMessage(String channel, String message) {
unsubscribe(channel);
}
public void onSubscribe(String channel, 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 onPMessage(String pattern, String channel,
String message) {
punsubscribe(pattern);
}
};
Thread t = new Thread(new Runnable() {
public void run() {
try {
Jedis j = createJedis();
Thread.sleep(1000);
pubsub.subscribe("bar");
pubsub.psubscribe("bar.*");
j.publish("foo", "exit");
j.publish("bar", "exit");
j.publish("bar.123", "exit");
j.disconnect();
} catch (Exception ex) {
fail(ex.getMessage());
}
}
});
t.start();
jedis.subscribe(pubsub, "foo");
t.join();
} }
} }