/** Loops on processing a client forever */ public void run() { TProcessor processor = null; TTransport inputTransport = null; TTransport outputTransport = null; TProtocol inputProtocol = null; TProtocol outputProtocol = null; try { processor = processorFactory_.getProcessor(client_); inputTransport = inputTransportFactory_.getTransport(client_); outputTransport = outputTransportFactory_.getTransport(client_); inputProtocol = inputProtocolFactory_.getProtocol(inputTransport); outputProtocol = outputProtocolFactory_.getProtocol(outputTransport); // we check stopped_ first to make sure we're not supposed to be shutting // down. this is necessary for graceful shutdown. while (!stopped_ && processor.process(inputProtocol, outputProtocol)) {} } catch (TTransportException ttx) { // Assume the client died and continue silently } catch (TException tx) { LOGGER.error("Thrift error occurred during processing of message.", tx); } catch (Exception x) { LOGGER.error("Error occurred during processing of message.", x); } if (inputTransport != null) { inputTransport.close(); } if (outputTransport != null) { outputTransport.close(); } }
@Override public void onMessage(String key, String msg) { if (LOGGER.isInfoEnabled()) { LOGGER.info("redis event: " + key + " = " + msg); } if (msg.equals(Constants.REGISTER) || msg.equals(Constants.UNREGISTER)) { try { Jedis jedis = jedisPool.getResource(); try { doNotify(jedis, key); } finally { jedis.close(); } } catch (Throwable t) { LOGGER.error(t.getMessage(), t); } } }
@Override public void run() { try { while (running) { int retryTimes = 0; for (Map.Entry<String, JedisPool> entry : jedisPools.entrySet()) { try { JedisPool jedisPool = entry.getValue(); jedis = jedisPool.getResource(); if (listenNodePath.equals(monitorId) && !redisAvailable) { redisAvailable = true; appContext.getRegistryStatMonitor().setAvailable(redisAvailable); } try { retryTimes = 0; jedis.subscribe(new NotifySub(jedisPool), listenNodePath); // 阻塞 break; } finally { jedis.close(); } } catch (Throwable t) { // 重试另一台 LOGGER.warn( "Failed to subscribe node from redis registry. registry: " + entry.getKey(), t); if (++retryTimes % jedisPools.size() == 0) { // 如果在所有redis都不可用,需要休息一会,避免空转占用过多cpu资源 sleep(reconnectPeriod); if (listenNodePath.equals(monitorId) && redisAvailable) { redisAvailable = false; appContext.getRegistryStatMonitor().setAvailable(redisAvailable); } } } } } } catch (Throwable t) { LOGGER.error(t.getMessage(), t); } }