本文整理匯總了Java中redis.clients.jedis.Jedis.subscribe方法的典型用法代碼示例。如果您正苦於以下問題:Java Jedis.subscribe方法的具體用法?Java Jedis.subscribe怎麽用?Java Jedis.subscribe使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類redis.clients.jedis.Jedis
的用法示例。
在下文中一共展示了Jedis.subscribe方法的4個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: MessageConsumerRedisImpl
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
/**
*
* @param redisConnection redis 連接類
* @param channels 訂閱的頻道列表
*/
public MessageConsumerRedisImpl(RedisConnection redisConnection, String[] channels) {
Jedis jedis = null;
try {
if (channels != null && channels.length > 0) {
jedis = redisConnection.getJedis();
jedis.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
System.out.println("receive " + message + " from " + channel);
handleMessage(message);
}
}, channels);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (jedis != null) {
jedis.close();
}
}
}
示例2: subscribe
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
public void subscribe(JedisPubSub jedisPubSub, String channel) {
Collection<JedisPool> poolCollection = jedisCluster.getClusterNodes().values();
Iterator iterator = poolCollection.iterator();
while(iterator.hasNext()) {
JedisPool jedisPool = (JedisPool)iterator.next();
Jedis jedis = null;
try {
jedis = jedisPool.getResource();
jedis.subscribe(jedisPubSub, new String[]{channel});
} catch (Exception var11) {
throw new RuntimeException(var11);
} finally {
jedisPool.returnResourceObject(jedis);
}
}
}
示例3: subscribe
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
public void subscribe(JedisPubSub jedisPubSub, String channel) {
Jedis jedis = null;
try {
jedis = pool.getResource();
jedis.subscribe(jedisPubSub, new String[]{channel});
} catch (Exception var8) {
throw new RuntimeException(var8);
} finally {
pool.returnResourceObject(jedis);
}
}
示例4: run
import redis.clients.jedis.Jedis; //導入方法依賴的package包/類
public void run() {
running.set(true);
while (running.get()) {
j = new Jedis(host, port);
try {
// double check that it is not being shutdown
if (!running.get()) {
break;
}
j.subscribe(new JedisPubSub() {
@Override
public void onMessage(String channel, String message) {
log.fine("Sentinel " + host + ":" + port + " published: " + message + ".");
String[] switchMasterMsg = message.split(" ");
if (switchMasterMsg.length > 3) {
if (masterName.equals(switchMasterMsg[0])) {
initPool(toHostAndPort(Arrays.asList(switchMasterMsg[3], switchMasterMsg[4])));
} else {
log.fine("Ignoring message on +switch-master for master name "
+ switchMasterMsg[0] + ", our master name is " + masterName);
}
} else {
log.severe("Invalid message received on Sentinel " + host + ":" + port
+ " on channel +switch-master: " + message);
}
}
}, "+switch-master");
} catch (JedisConnectionException e) {
if (running.get()) {
log.log(Level.SEVERE, "Lost connection to Sentinel at " + host + ":" + port
+ ". Sleeping 5000ms and retrying.", e);
try {
Thread.sleep(subscribeRetryWaitTimeMillis);
} catch (InterruptedException e1) {
log.log(Level.SEVERE, "Sleep interrupted: ", e1);
}
} else {
log.fine("Unsubscribing from Sentinel at " + host + ":" + port);
}
} finally {
j.close();
}
}
}