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());
}
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");
}
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();
}
}
}
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;
}
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;
}
Aggregations