Java Code Examples for redis.clients.jedis.Jedis#publish()

The following examples show how to use redis.clients.jedis.Jedis#publish() . 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: RedisLockCoordinator.java    From jeesuite-libs with Apache License 2.0 6 votes vote down vote up
public void notifyNext(Jedis pubClient,String lockName){
	long currentTimeMillis = System.currentTimeMillis();
	
	String nextEventId;
	while(true){
		nextEventId = pubClient.rpop(lockName);
		if(StringUtils.isBlank(nextEventId)){
			return;
		}
		if(activeNodeIds.contains(nextEventId.substring(0,8)) 
				&& currentTimeMillis - Long.parseLong(nextEventId.substring(8,21)) < CLEAN_TIME){
			break;
		}
	}
	pubClient.publish(channelName, lockName + SPLIT_STR + nextEventId);
}
 
Example 2
Source File: JRedisCache.java    From springJredisCache with Apache License 2.0 6 votes vote down vote up
/**
 * 发布 消息
 *
 * @param channel
 * @param message
 * @return
 */
@Override
public Long publish(String channel, byte[] message) {
    Jedis jedis = null;
    try {
        jedis = jedisPool.getResource();
        return jedis.publish(channel.getBytes(), message);
    } catch (Exception ex) {
        coverException(ex, jedisPool, jedis);
        return null;
    } finally {
        if (jedis != null && jedis.isConnected()) {
            jedisPool.returnResource(jedis);
            if (LOGGER.isDebugEnabled()) {
                LOGGER.debug("close redis connection-{" + jedis.toString() + "}");
            }
        }
    }
}
 
Example 3
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void deferExpired() {
    for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
        JedisPool jedisPool = entry.getValue();
        boolean isBroken = false;
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                for (URL url : new HashSet<URL>(getRegistered())) {
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        String key = toCategoryPath(url);
                        if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) {
                            jedis.publish(key, Constants.REGISTER);
                        }
                    }
                }
                if (admin) {
                    clean(jedis);
                }
                if (!replicate) {
                    break;//  如果服务器端已同步数据,只需写入单台机器
                }
            } catch (JedisConnectionException e){
                isBroken = true;
            } finally {
                if(isBroken){
                    jedisPool.returnBrokenResource(jedis);
                } else {
                    jedisPool.returnResource(jedis);
                }
            }
        } catch (Throwable t) {
            logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t);
        }
    }
}
 
Example 4
Source File: RedisRegistry.java    From dubbo3 with Apache License 2.0 5 votes vote down vote up
private void clean(Jedis jedis) {
    Set<String> keys = jedis.keys(root + Constants.ANY_VALUE);
    if (keys != null && keys.size() > 0) {
        for (String key : keys) {
            Map<String, String> values = jedis.hgetAll(key);
            if (values != null && values.size() > 0) {
                boolean delete = false;
                long now = System.currentTimeMillis();
                for (Map.Entry<String, String> entry : values.entrySet()) {
                    URL url = URL.valueOf(entry.getKey());
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        long expire = Long.parseLong(entry.getValue());
                        if (expire < now) {
                            jedis.hdel(key, entry.getKey());
                            delete = true;
                            if (logger.isWarnEnabled()) {
                                logger.warn("Delete expired key: " + key + " -> value: " + entry.getKey() + ", expire: " + new Date(expire) + ", now: " + new Date(now));
                            }
                        }
                    }
                }
                if (delete) {
                    jedis.publish(key, Constants.UNREGISTER);
                }
            }
        }
    }
}
 
Example 5
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void deferExpired() {
    for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) {
        JedisPool jedisPool = entry.getValue();
        try {
            Jedis jedis = jedisPool.getResource();
            try {
                for (URL url : new HashSet<URL>(getRegistered())) {
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        String key = toCategoryPath(url);
                        if (jedis.hset(key, url.toFullString(), String.valueOf(System.currentTimeMillis() + expirePeriod)) == 1) {
                            jedis.publish(key, Constants.REGISTER);
                        }
                    }
                }
                if (admin) {
                    clean(jedis);
                }
                if (! replicate) {
                	break;//  如果服务器端已同步数据,只需写入单台机器
                }
            } finally {
                jedisPool.returnResource(jedis);
            }
        } catch (Throwable t) {
            logger.warn("Failed to write provider heartbeat to redis registry. registry: " + entry.getKey() + ", cause: " + t.getMessage(), t);
        }
    }
}
 
Example 6
Source File: RedisCommandInvoker.java    From hanboDB with Apache License 2.0 5 votes vote down vote up
public void publish(ByteBuf byteBuf) {
    for (int i = 0; i < slaverHostList.size(); i++) {
        Jedis jedis = slaverHostList.get(i);
        try {
            jedis.publish(repKey.getBytes(), byteBuf.array());
        } catch (Exception e) {
            log.error("", e);
            slaverHostList.remove(jedis);
        }
    }
}
 
Example 7
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void clean(Jedis jedis) {
    Set<String> keys = jedis.keys(root + Constants.ANY_VALUE);
    if (keys != null && keys.size() > 0) {
        for (String key : keys) {
            Map<String, String> values = jedis.hgetAll(key);
            if (values != null && values.size() > 0) {
                boolean delete = false;
                long now = System.currentTimeMillis();
                for (Map.Entry<String, String> entry : values.entrySet()) {
                    URL url = URL.valueOf(entry.getKey());
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        long expire = Long.parseLong(entry.getValue());
                        if (expire < now) {
                            jedis.hdel(key, entry.getKey());
                            delete = true;
                            if (logger.isWarnEnabled()) {
                                logger.warn("Delete expired key: " + key + " -> value: " + entry.getKey() + ", expire: " + new Date(expire) + ", now: " + new Date(now));
                            }
                        }
                    }
                }
                if (delete) {
                    jedis.publish(key, Constants.UNREGISTER);
                }
            }
        }
    }
}
 
Example 8
Source File: RedisRegistry.java    From dubbox with Apache License 2.0 5 votes vote down vote up
private void clean(Jedis jedis) {
    Set<String> keys = jedis.keys(root + Constants.ANY_VALUE);
    if (keys != null && keys.size() > 0) {
        for (String key : keys) {
            Map<String, String> values = jedis.hgetAll(key);
            if (values != null && values.size() > 0) {
                boolean delete = false;
                long now = System.currentTimeMillis();
                for (Map.Entry<String, String> entry : values.entrySet()) {
                    URL url = URL.valueOf(entry.getKey());
                    if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                        long expire = Long.parseLong(entry.getValue());
                        if (expire < now) {
                            jedis.hdel(key, entry.getKey());
                            delete = true;
                            if (logger.isWarnEnabled()) {
                                logger.warn("Delete expired key: " + key + " -> value: " + entry.getKey() + ", expire: " + new Date(expire) + ", now: " + new Date(now));
                            }
                        }
                    }
                }
                if (delete) {
                    jedis.publish(key, Constants.UNREGISTER);
                }
            }
        }
    }
}
 
Example 9
Source File: ShiroSessionListener.java    From Spring-Shiro-Spark with Apache License 2.0 5 votes vote down vote up
@Override
public void onStop(Session session) {
    shiroSessionDao.delete(session);
    Jedis jedis = jedisPool.getResource();
    jedis.publish("shiro.session.uncache",(String) session.getId());
    logger.debug("session {} onStop", session.getId());
}
 
Example 10
Source File: RedisTweetInjector.java    From AIDR with GNU Affero General Public License v3.0 5 votes vote down vote up
public void publishTweet(final Jedis jedis, final String collectionCode, final String tweet) {
	jedis.publish(config.channelPrefix+collectionCode, tweet);
	try {
		Thread.sleep(sleepDuration);
	} catch (InterruptedException e) {
		System.err.println("Thread sleep interrupted for thread: " + Thread.currentThread().getName());
	}
}
 
Example 11
Source File: RedisClient.java    From HtmlExtractor with Apache License 2.0 5 votes vote down vote up
/**
 * 当抽取规则发生变化的时候
 * 向Redis服务器Channel:pr发送消息CHANGE
 * 从节点就会重新初始化抽取规则
 */
public void extractRegularChange() {
    String message = "CHANGE";
    Jedis jedis = jedisPool.getResource();
    jedis.publish("pr", message);
    jedisPool.returnResource(jedis);
}
 
Example 12
Source File: LimiterConfigResource.java    From redislimiter-spring-boot with Apache License 2.0 5 votes vote down vote up
private void publish(LimiterConfig limiterConfig) {
    ObjectMapper objectMapper = new ObjectMapper();
    String configMessage = null;
    try {
        configMessage = objectMapper.writeValueAsString(limiterConfig);
    }
    catch(IOException e) {
        logger.error("convert LimiterConfig object to json failed.");
    }
    Jedis jedis = jedisPool.getResource();
    jedis.publish(redisLimiterProperties.getChannel(), configMessage);
}
 
Example 13
Source File: JedisUtil.java    From scaffold-cloud with MIT License 5 votes vote down vote up
/**
 * 发布消息到其他节点,更新缓存时使用
 *
 * @param channel
 * @param message
 */
public static void publish(String channel, Serializable message) {
    if (StrUtil.isBlank(channel) || message == null) {
        return;
    }
    Jedis jedis = null;
    try {
        jedis = getResource();
        jedis.publish(channel, (String) message);
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
    } finally {
        close(jedis);
    }
}
 
Example 14
Source File: RedisUtil.java    From RedisBungee with Eclipse Public License 1.0 5 votes vote down vote up
public static void cleanUpPlayer(String player, Jedis rsc) {
    rsc.srem("proxy:" + RedisBungee.getApi().getServerId() + ":usersOnline", player);
    rsc.hdel("player:" + player, "server",  "ip", "proxy");
    long timestamp = System.currentTimeMillis();
    rsc.hset("player:" + player, "online", String.valueOf(timestamp));
    rsc.publish("redisbungee-data", RedisBungee.getGson().toJson(new DataManager.DataManagerMessage<>(
            UUID.fromString(player), DataManager.DataManagerMessage.Action.LEAVE,
            new DataManager.LogoutPayload(timestamp))));
}
 
Example 15
Source File: ResourceChangeNotifier.java    From word_web with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 二元模型 - 增加一个二元模型
 * @param item 二元模型
 */
public static void addBigram(String item){
    Jedis jedis = jedisPool.getResource();
    jedis.publish("bigram.txt.add", item);
    jedisPool.returnResource(jedis);
}
 
Example 16
Source File: ResourceChangeNotifier.java    From word_web with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 百家姓 - 移除一个百家姓
 * @param item 百家姓
 */
public static void removeSurname(String item){
    Jedis jedis = jedisPool.getResource();
    jedis.publish("surname.txt.remove", item);
    jedisPool.returnResource(jedis);
}
 
Example 17
Source File: ResourceChangeNotifier.java    From word_web with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 分词词典 - 移除一个词
 * @param item 词
 */
public static void removeDic(String item){
    Jedis jedis = jedisPool.getResource();
    jedis.publish("dic.txt.remove", item);
    jedisPool.returnResource(jedis);
}
 
Example 18
Source File: ResourceChangeNotifier.java    From word_web with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 分词词典 - 增加一个词
 * @param item 词
 */
public static void addDic(String item){
    Jedis jedis = jedisPool.getResource();
    jedis.publish("dic.txt.add", item);
    jedisPool.returnResource(jedis);
}
 
Example 19
Source File: ResourceChangeNotifier.java    From word_web with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 二元模型 - 移除一个二元模型
 * @param item 二元模型
 */
public static void removeBigram(String item){
    Jedis jedis = jedisPool.getResource();
    jedis.publish("bigram.txt.remove", item);
    jedisPool.returnResource(jedis);
}
 
Example 20
Source File: ResourceChangeNotifier.java    From word_web with GNU General Public License v3.0 4 votes vote down vote up
/**
 * 三元模型 - 移除一个三元模型
 * @param item 三元模型
 */
public static void removeTrigram(String item){
    Jedis jedis = jedisPool.getResource();
    jedis.publish("trigram.txt.remove", item);
    jedisPool.returnResource(jedis);
}