Java Code Examples for redis.clients.jedis.Jedis#subscribe()
The following examples show how to use
redis.clients.jedis.Jedis#subscribe() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MessageConsumerRedisImpl.java From redis_util with Apache License 2.0 | 6 votes |
/** * * @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(); } } }
Example 2
Source File: JRedisCache.java From springJredisCache with Apache License 2.0 | 6 votes |
/** * 订阅指定的消息 订阅得到信息在JedisPubSub的onMessage(...)方法中进行处理 * * @param channels */ @Override public void subscribe(final String... channels) { Jedis jedis = null; try { jedis = jedisPool.getResource(); final byte[][] ps = new byte[channels.length][]; for (int i = 0; i < ps.length; i++) { ps[i] = SafeEncoder.encode(channels[i]); } jedis.subscribe(jRedisBinaryPubSub, ps); } catch (Exception ex) { coverException(ex, jedisPool, jedis); } finally { if (jedis != null && jedis.isConnected()) { jedisPool.returnResource(jedis); if (LOGGER.isDebugEnabled()) { LOGGER.debug("close redis connection-{" + jedis.toString() + "}"); } } } }
Example 3
Source File: SubscriberTest.java From code with Apache License 2.0 | 5 votes |
public static void main(String[] args){ Jedis jedis = new Jedis("192.168.108.130" , 6379); jedis.subscribe(new JedisPubSub() { @Override public void onMessage(String channel, String message) { System.out.println("receive channel ["+channel+"] message ["+message+"]"); } } , "aliTV" , "googleTV"); }
Example 4
Source File: JedisUtil.java From scaffold-cloud with MIT License | 5 votes |
/** * 调用jedis的subscribe()方法 * * @param jedisPubSub * @param channels */ public static void subscribe(JedisPubSub jedisPubSub, String... channels) { Jedis jedis = null; try { jedis = getResource(); jedis.subscribe(jedisPubSub, channels); } catch (Exception e) { logger.warn("psubscribe channels==> {} exception==> {}", channels, e); } finally { close(jedis); } }
Example 5
Source File: TestSubscribe.java From Redis_Learning with Apache License 2.0 | 5 votes |
@Test public void testSubscribe() throws Exception{ Jedis jedis = new Jedis("192.168.65.130", 6379); jedis.auth("redis"); RedisMsgPubSubListener listener = new RedisMsgPubSubListener(); jedis.subscribe(listener, "redisChatTest"); // other code }
Example 6
Source File: UCJedisLoader.java From UltimateChat with GNU General Public License v3.0 | 5 votes |
@Override public void run() { try { Jedis rsc = pool.getResource(); rsc.subscribe(channel, channels); } catch (JedisException | ClassCastException ignored) { } }
Example 7
Source File: AutoDetector.java From word with Apache License 2.0 | 5 votes |
private static void watchHttp(String resource, final ResourceLoader resourceLoader){ String[] attrs = resource.split("/"); final String channel = attrs[attrs.length-1]; Thread thread = new Thread(new Runnable() { @Override public void run() { String host = WordConfTools.get("redis.host", "localhost"); int port = WordConfTools.getInt("redis.port", 6379); String channel_add = channel+".add"; String channel_remove = channel+".remove"; LOGGER.info("redis服务器配置信息 host:" + host + ",port:" + port + ",channels:[" + channel_add + "," + channel_remove+"]"); while(true){ try{ JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), host, port); final Jedis jedis = jedisPool.getResource(); LOGGER.info("redis守护线程启动"); jedis.subscribe(new HttpResourceChangeRedisListener(resourceLoader), new String[]{channel_add, channel_remove}); jedisPool.returnResource(jedis); LOGGER.info("redis守护线程结束"); break; }catch(Exception e){ LOGGER.info("redis未启动,暂停一分钟后重新连接"); try { Thread.sleep(60000); } catch (InterruptedException ex) { LOGGER.error(ex.getMessage(), ex); } } } } }); thread.setDaemon(true); thread.setName("redis守护线程,用于动态监控资源:"+channel); thread.start(); }
Example 8
Source File: ExtractRegular.java From HtmlExtractor with Apache License 2.0 | 5 votes |
/** * 订阅Redis服务器Channel:pr,当规则改变的时候会收到通知消息CHANGE并重新初始化规则集合 */ private void subscribeRedis(final String redisHost, final int redisPort, final String serverUrl) { if (null == redisHost || redisPort < 1) { LOGGER.error("没有指定redis服务器配置!"); return; } Thread thread = new Thread(new Runnable() { @Override public void run() { String channel = "pr"; LOGGER.info("redis服务器配置信息 host:" + redisHost + ",port:" + redisPort + ",channel:" + channel); while (true) { try { JedisPool jedisPool = new JedisPool(new JedisPoolConfig(), redisHost, redisPort); Jedis jedis = jedisPool.getResource(); LOGGER.info("redis守护线程启动"); jedis.subscribe(new ExtractRegularChangeRedisListener(serverUrl), new String[]{channel}); jedisPool.returnResource(jedis); LOGGER.info("redis守护线程结束"); break; } catch (Exception e) { LOGGER.info("redis未启动,暂停一分钟后重新连接"); try { Thread.sleep(600000); } catch (InterruptedException ex) { LOGGER.error(ex.getMessage(), ex); } } } } }); thread.setDaemon(true); thread.setName("redis守护线程,用于动态加载抽取规则"); thread.start(); }