public void execute(Context context) { Set<String> applications = new HashSet<String>(); Set<String> services = new HashSet<String>(); List<Provider> pList = new ArrayList<Provider>(); try { pList = providerService.findAll(); } catch (Exception e) { logger.error(e.getMessage(), e); } for (Provider p : pList) { applications.add(p.getApplication()); services.add(p.getService()); } List<Consumer> cList = new ArrayList<Consumer>(); try { cList = consumerService.findAll(); } catch (Exception e) { logger.error(e.getMessage(), e); } for (Consumer c : cList) { applications.add(c.getApplication()); services.add(c.getService()); } context.put("rootContextPath", new RootContextPath(request.getContextPath())); context.put("services", services.size()); context.put("providers", pList.size()); context.put("consumers", cList.size()); context.put("applications", applications.size()); }
private void returnResource(ShardedJedis shardedJedis) { try { shardedJedis.close(); } catch (Exception e) { logger.error("returnResource error.", e); } }
@SuppressWarnings("unchecked") @Test public void testRpcException() { Logger logger = EasyMock.createMock(Logger.class); RpcContext.getContext().setRemoteAddress("127.0.0.1", 1234); RpcException exception = new RpcException("TestRpcException"); logger.error( EasyMock.eq( "Got unchecked and undeclared exception which called by 127.0.0.1. service: " + DemoService.class.getName() + ", method: sayHello, exception: " + RpcException.class.getName() + ": TestRpcException"), EasyMock.eq(exception)); ExceptionFilter exceptionFilter = new ExceptionFilter(logger); RpcInvocation invocation = new RpcInvocation("sayHello", new Class<?>[] {String.class}, new Object[] {"world"}); Invoker<DemoService> invoker = EasyMock.createMock(Invoker.class); EasyMock.expect(invoker.getInterface()).andReturn(DemoService.class); EasyMock.expect(invoker.invoke(EasyMock.eq(invocation))).andThrow(exception); EasyMock.replay(logger, invoker); try { exceptionFilter.invoke(invoker, invocation); } catch (RpcException e) { assertEquals("TestRpcException", e.getMessage()); } EasyMock.verify(logger, invoker); RpcContext.removeContext(); }
public static void error(Logger logger, String msg, Throwable e) { if (logger == null) { return; } if (logger.isErrorEnabled()) { logger.error(msg, e); } }
/** * 获取List * * @param key * @return */ public List<String> getList(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); return shardedJedis.lrange(key, 0, -1); } catch (Exception ex) { logger.error("getList error.", ex); } finally { returnResource(shardedJedis); } return null; }
/** * 获得排序打分 * * @param key * @return */ public Double getScore(String key, String member) { ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); return shardedJedis.zscore(key, member); } catch (Exception ex) { logger.error("getSoredSet error.", ex); } finally { returnResource(shardedJedis); } return null; }
public String get(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); return shardedJedis.get(key); } catch (Exception ex) { logger.error("get error.", ex); return null; } finally { returnResource(shardedJedis); } }
public long decr(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); return shardedJedis.decr(key); } catch (Exception ex) { logger.error("incr error.", ex); } finally { returnResource(shardedJedis); } return 0; }
/** * 删除HashSet对象 * * @param domain 域名 * @param key 键值 * @return 删除的记录数 */ public long delHSet(String domain, String... key) { ShardedJedis shardedJedis = null; long count = 0; try { shardedJedis = masterShardedJedisPool.getResource(); count = shardedJedis.hdel(domain, key); } catch (Exception ex) { logger.error("delHSet error.", ex); } finally { returnResource(shardedJedis); } return count; }
/** * 判断key是否存在 * * @param domain 域名 * @param key 键值 * @return */ public boolean existsHSet(String domain, String key) { ShardedJedis shardedJedis = null; boolean isExist = false; try { shardedJedis = slaveShardedJedisPool.getResource(); isExist = shardedJedis.hexists(domain, key); } catch (Exception ex) { logger.error("existsHSet error.", ex); } finally { returnResource(shardedJedis); } return isExist; }
/** * 返回 domain 指定的哈希key值总数 * * @param domain * @return */ public long lenHset(String domain) { ShardedJedis shardedJedis = null; long retList = 0; try { shardedJedis = slaveShardedJedisPool.getResource(); retList = shardedJedis.hlen(domain); } catch (Exception ex) { logger.error("hkeys error.", ex); } finally { returnResource(shardedJedis); } return retList; }
/** * 计算排序长度 * * @param key * @param startScore * @param endScore * @return */ public long countSoredSet(String key, long startScore, long endScore) { ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); Long count = shardedJedis.zcount(key, startScore, endScore); return count == null ? 0L : count; } catch (Exception ex) { logger.error("countSoredSet error.", ex); } finally { returnResource(shardedJedis); } return 0L; }
public boolean set(String key, String value) { ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); shardedJedis.set(key, value); return true; } catch (Exception ex) { logger.error("set error.", ex); } finally { returnResource(shardedJedis); } return false; }
/** * 删除排序集合 * * @param key * @param value * @return */ public boolean delSortedSet(String key, String value) { ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); long count = shardedJedis.zrem(key, value); return count > 0; } catch (Exception ex) { logger.error("delSortedSet error.", ex); } finally { returnResource(shardedJedis); } return false; }
/** * 返回 domain 指定的哈希集中所有字段的key值 * * @param domain * @return */ public Set<String> hkeys(String domain) { ShardedJedis shardedJedis = null; Set<String> retList = null; try { shardedJedis = slaveShardedJedisPool.getResource(); retList = shardedJedis.hkeys(domain); } catch (Exception ex) { logger.error("hkeys error.", ex); } finally { returnResource(shardedJedis); } return retList; }
/** * 从list中删除value * * @param key * @param count 要删除个数 * @param value * @return */ public boolean removeListValue(String key, long count, String value) { ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); shardedJedis.lrem(key, count, value); return true; } catch (Exception ex) { logger.error("getList error.", ex); ; } finally { returnResource(shardedJedis); } return false; }
public boolean del(String key) { ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); shardedJedis.del(key); return true; } catch (Exception ex) { logger.error("del error.", ex); } finally { returnResource(shardedJedis); } return false; }
/** * @param key * @param value * @return 判断值是否包含在set中 */ public boolean containsInSet(String key, String value) { if (key == null || value == null) { return false; } ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); return shardedJedis.sismember(key, value); } catch (Exception ex) { logger.error("setList error.", ex); } finally { returnResource(shardedJedis); } return false; }
/** * 检查List长度 * * @param key * @return */ public long countList(String key) { if (key == null) { return 0; } ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); return shardedJedis.llen(key); } catch (Exception ex) { logger.error("countList error.", ex); } finally { returnResource(shardedJedis); } return 0; }
/** * 截断一个List * * @param key 列表key * @param start 开始位置 从0开始 * @param end 结束位置 * @return 状态码 */ public String trimList(String key, long start, long end) { if (key == null || key.equals("")) { return "-"; } ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); return shardedJedis.ltrim(key, start, end); } catch (Exception ex) { logger.error( "LTRIM 出错[key=" + key + " start=" + start + " end=" + end + "]" + ex.getMessage(), ex); } finally { returnResource(shardedJedis); } return "-"; }
/** * 添加到List * * @param key * @param value * @return */ public boolean addList(String key, String... value) { if (key == null || value == null) { return false; } ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); shardedJedis.lpush(key, value); return true; } catch (Exception ex) { logger.error("setList error.", ex); } finally { returnResource(shardedJedis); } return false; }
/** * 设置一个key的过期时间(单位:秒) * * @param key key值 * @param seconds 多少秒后过期 * @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间 */ public long expire(String key, int seconds) { if (key == null || key.equals("")) { return 0; } ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); return shardedJedis.expire(key, seconds); } catch (Exception ex) { logger.error("EXPIRE error[key=" + key + " seconds=" + seconds + "]" + ex.getMessage(), ex); } finally { returnResource(shardedJedis); } return 0; }
/** * 获得排序集合 * * @param key * @param startScore * @param endScore * @param orderByDesc * @return */ public Set<String> getSoredSet(String key, long startScore, long endScore, boolean orderByDesc) { ShardedJedis shardedJedis = null; try { shardedJedis = slaveShardedJedisPool.getResource(); if (orderByDesc) { return shardedJedis.zrevrangeByScore(key, endScore, startScore); } else { return shardedJedis.zrangeByScore(key, startScore, endScore); } } catch (Exception ex) { logger.error("getSoredSet error.", ex); } finally { returnResource(shardedJedis); } return null; }
/** * 设置一个key在某个时间点过期 * * @param key key值 * @param unixTimestamp unix时间戳,从1970-01-01 00:00:00开始到现在的秒数 * @return 1:设置了过期时间 0:没有设置过期时间/不能设置过期时间 */ public long expireAt(String key, int unixTimestamp) { if (key == null || key.equals("")) { return 0; } ShardedJedis shardedJedis = null; try { shardedJedis = masterShardedJedisPool.getResource(); return shardedJedis.expireAt(key, unixTimestamp); } catch (Exception ex) { logger.error( "EXPIRE error[key=" + key + " unixTimestamp=" + unixTimestamp + "]" + ex.getMessage(), ex); } finally { returnResource(shardedJedis); } return 0; }
private void doNotify(String service, List<URL> urls) { notified.put(service, urls); List<NotifyListener> listeners = notifyListeners.get(service); if (listeners != null) { for (NotifyListener listener : listeners) { try { notify(service, urls, listener); } catch (Throwable t) { logger.error( "Failed to notify registry event, service: " + service + ", urls: " + urls + ", cause: " + t.getMessage(), t); } } } }
protected void notify(List<URL> urls) { if (urls == null || urls.isEmpty()) return; for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) { URL url = entry.getKey(); if (!UrlUtils.isMatch(url, urls.get(0))) { continue; } Set<NotifyListener> listeners = entry.getValue(); if (listeners != null) { for (NotifyListener listener : listeners) { try { notify(url, listener, filterEmpty(url, urls)); } catch (Throwable t) { logger.error( "Failed to notify registry event, urls: " + urls + ", cause: " + t.getMessage(), t); } } } } }
/** * 全局扫描hset * * @param match field匹配模式 * @return */ public List<Map.Entry<String, String>> scanHSet(String domain, String match) { ShardedJedis shardedJedis = null; try { int cursor = 0; shardedJedis = slaveShardedJedisPool.getResource(); ScanParams scanParams = new ScanParams(); scanParams.match(match); Jedis jedis = shardedJedis.getShard(domain); ScanResult<Map.Entry<String, String>> scanResult; List<Map.Entry<String, String>> list = new ArrayList<Map.Entry<String, String>>(); do { scanResult = jedis.hscan(domain, String.valueOf(cursor), scanParams); list.addAll(scanResult.getResult()); cursor = Integer.parseInt(scanResult.getStringCursor()); } while (cursor > 0); return list; } catch (Exception ex) { logger.error("scanHSet error.", ex); } finally { returnResource(shardedJedis); } return null; }