Search in sources :

Example 6 with JedisPubSub

use of redis.clients.jedis.JedisPubSub in project jedis by xetorthio.

the class PublishSubscribeCommandsTest method unsubscribeWhenNotSusbscribed.

@Test(expected = JedisConnectionException.class)
public void unsubscribeWhenNotSusbscribed() throws InterruptedException {
    JedisPubSub pubsub = new JedisPubSub() {
    };
    pubsub.unsubscribe();
}
Also used : BinaryJedisPubSub(redis.clients.jedis.BinaryJedisPubSub) JedisPubSub(redis.clients.jedis.JedisPubSub) Test(org.junit.Test)

Example 7 with JedisPubSub

use of redis.clients.jedis.JedisPubSub in project jedis by xetorthio.

the class PublishSubscribeCommandsTest method pubSubNumSub.

@Test
public void pubSubNumSub() {
    final Map<String, String> expectedNumSub = new HashMap<String, String>();
    expectedNumSub.put("testchannel2", "1");
    expectedNumSub.put("testchannel1", "1");
    jedis.subscribe(new JedisPubSub() {

        private int count = 0;

        @Override
        public void onSubscribe(String channel, int subscribedChannels) {
            count++;
            if (count == 2) {
                Jedis otherJedis = createJedis();
                Map<String, String> numSub = otherJedis.pubsubNumSub("testchannel1", "testchannel2");
                assertEquals(expectedNumSub, numSub);
                unsubscribe();
            }
        }
    }, "testchannel1", "testchannel2");
}
Also used : Jedis(redis.clients.jedis.Jedis) HashMap(java.util.HashMap) BinaryJedisPubSub(redis.clients.jedis.BinaryJedisPubSub) JedisPubSub(redis.clients.jedis.JedisPubSub) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 8 with JedisPubSub

use of redis.clients.jedis.JedisPubSub in project jedis by xetorthio.

the class PublishSubscribeCommandsTest method subscribeLazily.

@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) {
            publishOne(channel, "exit");
            if (!channel.equals("bar")) {
                this.subscribe("bar");
                this.psubscribe("bar.*");
            }
        }

        public void onPSubscribe(String pattern, int subscribedChannels) {
            publishOne(pattern.replace("*", "123"), "exit");
        }

        public void onPMessage(String pattern, String channel, String message) {
            punsubscribe(pattern);
        }
    };
    jedis.subscribe(pubsub, "foo");
}
Also used : BinaryJedisPubSub(redis.clients.jedis.BinaryJedisPubSub) JedisPubSub(redis.clients.jedis.JedisPubSub) Test(org.junit.Test)

Aggregations

JedisPubSub (redis.clients.jedis.JedisPubSub)8 Test (org.junit.Test)6 BinaryJedisPubSub (redis.clients.jedis.BinaryJedisPubSub)6 Jedis (redis.clients.jedis.Jedis)3 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 HostAndPort (redis.clients.jedis.HostAndPort)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 JedisConnectionException (redis.clients.jedis.exceptions.JedisConnectionException)1