now it is possible to subscribe and unsubscribe on a JedisPubSub instance
This commit is contained in:
@@ -19,18 +19,30 @@ public abstract class JedisPubSub {
|
||||
|
||||
public abstract void onPSubscribe(String pattern, int subscribedChannels);
|
||||
|
||||
protected void unsubscribe() {
|
||||
public void unsubscribe() {
|
||||
client.unsubscribe();
|
||||
}
|
||||
|
||||
protected void unsubscribe(String... channels) {
|
||||
public void unsubscribe(String... channels) {
|
||||
client.unsubscribe(channels);
|
||||
}
|
||||
|
||||
protected void subscribe(String... 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;
|
||||
}
|
||||
@@ -74,11 +86,7 @@ public abstract class JedisPubSub {
|
||||
} while (isSubscribed());
|
||||
}
|
||||
|
||||
protected void punsubscribe() {
|
||||
client.punsubscribe();
|
||||
}
|
||||
|
||||
protected void punsubscribe(String... patterns) {
|
||||
client.punsubscribe(patterns);
|
||||
public int getSubscribedChannels() {
|
||||
return subscribedChannels;
|
||||
}
|
||||
}
|
||||
@@ -12,7 +12,7 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
@Test
|
||||
public void subscribe() throws UnknownHostException, IOException,
|
||||
InterruptedException {
|
||||
new Thread(new Runnable() {
|
||||
Thread t = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
@@ -23,7 +23,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
});
|
||||
t.start();
|
||||
jedis.subscribe(new JedisPubSub() {
|
||||
public void onMessage(String channel, String message) {
|
||||
assertEquals("foo", channel);
|
||||
@@ -51,12 +52,13 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
String message) {
|
||||
}
|
||||
}, "foo");
|
||||
t.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void subscribeMany() throws UnknownHostException, IOException,
|
||||
InterruptedException {
|
||||
new Thread(new Runnable() {
|
||||
Thread t = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
@@ -69,7 +71,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
});
|
||||
t.start();
|
||||
jedis.subscribe(new JedisPubSub() {
|
||||
public void onMessage(String channel, String message) {
|
||||
unsubscribe(channel);
|
||||
@@ -91,12 +94,13 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
String message) {
|
||||
}
|
||||
}, "foo", "bar");
|
||||
t.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void psubscribe() throws UnknownHostException, IOException,
|
||||
InterruptedException {
|
||||
new Thread(new Runnable() {
|
||||
Thread t = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
@@ -107,7 +111,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
});
|
||||
t.start();
|
||||
jedis.psubscribe(new JedisPubSub() {
|
||||
public void onMessage(String channel, String message) {
|
||||
}
|
||||
@@ -136,12 +141,13 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
punsubscribe();
|
||||
}
|
||||
}, "foo.*");
|
||||
t.join();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void psubscribeMany() throws UnknownHostException, IOException,
|
||||
InterruptedException {
|
||||
new Thread(new Runnable() {
|
||||
Thread t = new Thread(new Runnable() {
|
||||
public void run() {
|
||||
try {
|
||||
Jedis j = createJedis();
|
||||
@@ -154,7 +160,8 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
fail(ex.getMessage());
|
||||
}
|
||||
}
|
||||
}).start();
|
||||
});
|
||||
t.start();
|
||||
jedis.psubscribe(new JedisPubSub() {
|
||||
public void onMessage(String channel, String message) {
|
||||
}
|
||||
@@ -176,5 +183,53 @@ public class PublishSubscribeCommandsTest extends JedisCommandTestBase {
|
||||
punsubscribe(pattern);
|
||||
}
|
||||
}, "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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user