Search in sources :

Example 1 with JedisPubSub

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

the class PublishSubscribeCommandsTest method pubSubChannelWithPingPong.

@Test
public void pubSubChannelWithPingPong() throws InterruptedException {
    final CountDownLatch latchUnsubscribed = new CountDownLatch(1);
    final CountDownLatch latchReceivedPong = new CountDownLatch(1);
    jedis.subscribe(new JedisPubSub() {

        @Override
        public void onSubscribe(String channel, int subscribedChannels) {
            publishOne("testchan1", "hello");
        }

        @Override
        public void onMessage(String channel, String message) {
            this.ping();
        }

        @Override
        public void onPong(String pattern) {
            latchReceivedPong.countDown();
            unsubscribe();
        }

        @Override
        public void onUnsubscribe(String channel, int subscribedChannels) {
            latchUnsubscribed.countDown();
        }
    }, "testchan1");
    assertEquals(0L, latchReceivedPong.getCount());
    assertEquals(0L, latchUnsubscribed.getCount());
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) BinaryJedisPubSub(redis.clients.jedis.BinaryJedisPubSub) JedisPubSub(redis.clients.jedis.JedisPubSub) Test(org.junit.Test)

Example 2 with JedisPubSub

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

the class PublishSubscribeCommandsTest method pubSubChannels.

@Test
public void pubSubChannels() {
    final List<String> expectedActiveChannels = Arrays.asList("testchan1", "testchan2", "testchan3");
    jedis.subscribe(new JedisPubSub() {

        private int count = 0;

        @Override
        public void onSubscribe(String channel, int subscribedChannels) {
            count++;
            // All channels are subscribed
            if (count == 3) {
                Jedis otherJedis = createJedis();
                List<String> activeChannels = otherJedis.pubsubChannels("test*");
                assertTrue(expectedActiveChannels.containsAll(activeChannels));
                unsubscribe();
            }
        }
    }, "testchan1", "testchan2", "testchan3");
}
Also used : Jedis(redis.clients.jedis.Jedis) List(java.util.List) BinaryJedisPubSub(redis.clients.jedis.BinaryJedisPubSub) JedisPubSub(redis.clients.jedis.JedisPubSub) Test(org.junit.Test)

Example 3 with JedisPubSub

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

the class PublishSubscribeCommandsTest method handleClientOutputBufferLimitForSubscribeTooSlow.

@Test(expected = JedisConnectionException.class)
public void handleClientOutputBufferLimitForSubscribeTooSlow() throws InterruptedException {
    final Jedis j = createJedis();
    final AtomicBoolean exit = new AtomicBoolean(false);
    final Thread t = new Thread(new Runnable() {

        public void run() {
            try {
                // we already set jedis1 config to
                // client-output-buffer-limit pubsub 256k 128k 5
                // it means if subscriber delayed to receive over 256k or
                // 128k continuously 5 sec,
                // redis disconnects subscriber
                // we publish over 100M data for making situation for exceed
                // client-output-buffer-limit
                String veryLargeString = makeLargeString(10485760);
                // 10M * 10 = 100M
                for (int i = 0; i < 10 && !exit.get(); i++) {
                    j.publish("foo", veryLargeString);
                }
                j.disconnect();
            } catch (Exception ex) {
            }
        }
    });
    t.start();
    try {
        jedis.subscribe(new JedisPubSub() {

            public void onMessage(String channel, String message) {
                try {
                    // wait 0.5 secs to slow down subscribe and
                    // client-output-buffer exceed
                    // System.out.println("channel - " + channel +
                    // " / message - " + message);
                    Thread.sleep(100);
                } catch (Exception e) {
                    try {
                        t.join();
                    } catch (InterruptedException e1) {
                    }
                    fail(e.getMessage());
                }
            }
        }, "foo");
    } finally {
        // exit the publisher thread. if exception is thrown, thread might
        // still keep publishing things.
        exit.set(true);
        if (t.isAlive()) {
            t.join();
        }
    }
}
Also used : Jedis(redis.clients.jedis.Jedis) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BinaryJedisPubSub(redis.clients.jedis.BinaryJedisPubSub) JedisPubSub(redis.clients.jedis.JedisPubSub) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) JedisConnectionException(redis.clients.jedis.exceptions.JedisConnectionException) Test(org.junit.Test)

Example 4 with JedisPubSub

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

the class JedisSentinelTestUtil method waitForNewPromotedMaster.

public static HostAndPort waitForNewPromotedMaster(final String masterName, final Jedis sentinelJedis, final Jedis commandJedis) throws InterruptedException {
    final AtomicReference<String> newmaster = new AtomicReference<String>("");
    sentinelJedis.psubscribe(new JedisPubSub() {

        @Override
        public void onPMessage(String pattern, String channel, String message) {
            if (channel.equals("+switch-master")) {
                newmaster.set(message);
                punsubscribe();
            } else if (channel.startsWith("-failover-abort")) {
                punsubscribe();
                throw new FailoverAbortedException("Unfortunately sentinel cannot failover... reason(channel) : " + channel + " / message : " + message);
            }
        }

        @Override
        public void onPSubscribe(String pattern, int subscribedChannels) {
            commandJedis.sentinelFailover(masterName);
        }
    }, "*");
    String[] chunks = newmaster.get().split(" ");
    HostAndPort newMaster = new HostAndPort(chunks[3], Integer.parseInt(chunks[4]));
    return newMaster;
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) AtomicReference(java.util.concurrent.atomic.AtomicReference) JedisPubSub(redis.clients.jedis.JedisPubSub)

Example 5 with JedisPubSub

use of redis.clients.jedis.JedisPubSub in project cachecloud by sohutv.

the class JedisSentinelTestUtil method waitForNewPromotedMaster.

public static HostAndPort waitForNewPromotedMaster(final String masterName, final Jedis sentinelJedis, final Jedis commandJedis) throws InterruptedException {
    final AtomicReference<String> newmaster = new AtomicReference<String>("");
    sentinelJedis.psubscribe(new JedisPubSub() {

        @Override
        public void onPMessage(String pattern, String channel, String message) {
            if (channel.equals("+switch-master")) {
                newmaster.set(message);
                punsubscribe();
            } else if (channel.startsWith("-failover-abort")) {
                punsubscribe();
                throw new FailoverAbortedException("Unfortunately sentinel cannot failover... reason(channel) : " + channel + " / message : " + message);
            }
        }

        @Override
        public void onPSubscribe(String pattern, int subscribedChannels) {
            commandJedis.sentinelFailover(masterName);
        }
    }, "*");
    String[] chunks = newmaster.get().split(" ");
    HostAndPort newMaster = new HostAndPort(chunks[3], Integer.parseInt(chunks[4]));
    return newMaster;
}
Also used : HostAndPort(redis.clients.jedis.HostAndPort) AtomicReference(java.util.concurrent.atomic.AtomicReference) JedisPubSub(redis.clients.jedis.JedisPubSub)

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