Пример #1
0
 protected void notify(URL url, NotifyListener listener, List<URL> urls) {
   if (url == null) {
     throw new IllegalArgumentException("notify url == null");
   }
   if (listener == null) {
     throw new IllegalArgumentException("notify listener == null");
   }
   if ((urls == null || urls.size() == 0)
       && !Constants.ANY_VALUE.equals(url.getServiceInterface())) {
     logger.warn("Ignore empty notify urls for subscribe url " + url);
     return;
   }
   if (logger.isInfoEnabled()) {
     logger.info("Notify urls for subscribe url " + url + ", urls: " + urls);
   }
   Map<String, List<URL>> result = new HashMap<String, List<URL>>();
   for (URL u : urls) {
     if (UrlUtils.isMatch(url, u)) {
       String category = u.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
       List<URL> categoryList = result.get(category);
       if (categoryList == null) {
         categoryList = new ArrayList<URL>();
         result.put(category, categoryList);
       }
       categoryList.add(u);
     }
   }
   if (result.size() == 0) {
     return;
   }
   Map<String, List<URL>> categoryNotified = notified.get(url);
   if (categoryNotified == null) {
     notified.putIfAbsent(url, new ConcurrentHashMap<String, List<URL>>());
     categoryNotified = notified.get(url);
   }
   for (Map.Entry<String, List<URL>> entry : result.entrySet()) {
     String category = entry.getKey();
     List<URL> categoryList = entry.getValue();
     categoryNotified.put(category, categoryList);
     saveProperties(url);
     listener.notify(categoryList);
   }
 }
  private void doNotify(
      Jedis jedis, Collection<String> keys, URL url, Collection<NotifyListener> listeners) {
    if (keys == null || keys.size() == 0 || listeners == null || listeners.size() == 0) {
      return;
    }
    long now = System.currentTimeMillis();
    List<URL> result = new ArrayList<URL>();
    List<String> categories =
        Arrays.asList(url.getParameter(Constants.CATEGORY_KEY, new String[0]));
    String consumerService = url.getServiceInterface();
    for (String key : keys) {
      if (!Constants.ANY_VALUE.equals(consumerService)) {
        String prvoiderService = RedisRegistryUtil.toServiceName(key, root);
        if (!prvoiderService.equals(consumerService)) {
          continue;
        }
      }
      String category = RedisRegistryUtil.toCategoryName(key);
      if (!categories.contains(Constants.ANY_VALUE) && !categories.contains(category)) {
        continue;
      }

      Map<String, String> values = jedis.hgetAll(key);
      List<URL> urls = RedisRegistryUtil.getUrlsForDoNotify(url, now, values);

      if (urls.isEmpty()) {
        urls.add(RedisRegistryUtil.setUrlProperties(url, key, category, root));
      }
      result.addAll(urls);
      if (logger.isWarnEnabled()) {
        logger.warn("redis notify: " + key + " = " + urls);
      }
    }
    if (result == null || result.size() == 0) {
      return;
    }
    for (NotifyListener listener : listeners) {
      notify(url, listener, result);
    }
  }