Example #1
0
 protected void recover() throws Exception {
   // register
   Set<URL> recoverRegistered = new HashSet<URL>(getRegistered());
   if (!recoverRegistered.isEmpty()) {
     if (logger.isInfoEnabled()) {
       logger.info("Recover register url " + recoverRegistered);
     }
     for (URL url : recoverRegistered) {
       register(url);
     }
   }
   // subscribe
   Map<URL, Set<NotifyListener>> recoverSubscribed =
       new HashMap<URL, Set<NotifyListener>>(getSubscribed());
   if (!recoverSubscribed.isEmpty()) {
     if (logger.isInfoEnabled()) {
       logger.info("Recover subscribe url " + recoverSubscribed.keySet());
     }
     for (Map.Entry<URL, Set<NotifyListener>> entry : recoverSubscribed.entrySet()) {
       URL url = entry.getKey();
       for (NotifyListener listener : entry.getValue()) {
         subscribe(url, listener);
       }
     }
   }
 }
Example #2
0
 public void destroy() {
   if (logger.isInfoEnabled()) {
     logger.info("Destroy registry:" + getUrl());
   }
   Set<URL> destroyRegistered = new HashSet<URL>(getRegistered());
   if (!destroyRegistered.isEmpty()) {
     for (URL url : new HashSet<URL>(getRegistered())) {
       if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
         try {
           unregister(url);
           if (logger.isInfoEnabled()) {
             logger.info("Destroy unregister url " + url);
           }
         } catch (Throwable t) {
           logger.warn(
               "Failed to unregister url "
                   + url
                   + " to registry "
                   + getUrl()
                   + " on destroy, cause: "
                   + t.getMessage(),
               t);
         }
       }
     }
   }
   Map<URL, Set<NotifyListener>> destroySubscribed =
       new HashMap<URL, Set<NotifyListener>>(getSubscribed());
   if (!destroySubscribed.isEmpty()) {
     for (Map.Entry<URL, Set<NotifyListener>> entry : destroySubscribed.entrySet()) {
       URL url = entry.getKey();
       for (NotifyListener listener : entry.getValue()) {
         try {
           unsubscribe(url, listener);
           if (logger.isInfoEnabled()) {
             logger.info("Destroy unsubscribe url " + url);
           }
         } catch (Throwable t) {
           logger.warn(
               "Failed to unsubscribe url "
                   + url
                   + " to registry "
                   + getUrl()
                   + " on destroy, cause: "
                   + t.getMessage(),
               t);
         }
       }
     }
   }
 }
Example #3
0
  public static void info(Logger logger, Throwable e) {
    if (logger == null) {
      return;
    }

    if (logger.isInfoEnabled()) {
      logger.info(e);
    }
  }
Example #4
0
  public static void info(Logger logger, String msg) {
    if (logger == null) {
      return;
    }

    if (logger.isInfoEnabled()) {
      logger.info(msg);
    }
  }
Example #5
0
 public void unregister(URL url) {
   if (url == null) {
     throw new IllegalArgumentException("unregister url == null");
   }
   if (logger.isInfoEnabled()) {
     logger.info("Unregister: " + url);
   }
   registered.remove(url);
 }
Example #6
0
 public void unsubscribe(URL url, NotifyListener listener) {
   if (url == null) {
     throw new IllegalArgumentException("unsubscribe url == null");
   }
   if (listener == null) {
     throw new IllegalArgumentException("unsubscribe listener == null");
   }
   if (logger.isInfoEnabled()) {
     logger.info("Unsubscribe: " + url);
   }
   Set<NotifyListener> listeners = subscribed.get(url);
   if (listeners != null) {
     listeners.remove(listener);
   }
 }
Example #7
0
 public void subscribe(URL url, NotifyListener listener) {
   if (url == null) {
     throw new IllegalArgumentException("subscribe url == null");
   }
   if (listener == null) {
     throw new IllegalArgumentException("subscribe listener == null");
   }
   if (logger.isInfoEnabled()) {
     logger.info("Subscribe: " + url);
   }
   Set<NotifyListener> listeners = subscribed.get(url);
   if (listeners == null) {
     subscribed.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
     listeners = subscribed.get(url);
   }
   listeners.add(listener);
 }
Example #8
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);
   }
 }
Example #9
0
 private void loadProperties() {
   if (file != null && file.exists()) {
     InputStream in = null;
     try {
       in = new FileInputStream(file);
       properties.load(in);
       if (logger.isInfoEnabled()) {
         logger.info("Load registry store file " + file + ", data: " + properties);
       }
     } catch (Throwable e) {
       logger.warn("Failed to load registry store file " + file, e);
     } finally {
       if (in != null) {
         try {
           in.close();
         } catch (IOException e) {
           logger.warn(e.getMessage(), e);
         }
       }
     }
   }
 }
 public void unsubscribe(URL url, NotifyListener listener) {
   if (logger.isInfoEnabled()) {
     logger.info("Unsubscribe service: " + url.getServiceKey() + ",url:" + url);
   }
   unsubscribe(url.getServiceKey(), url, listener);
 }
 public void unregister(URL url) {
   if (logger.isInfoEnabled()) {
     logger.info("Unregister service: " + url.getServiceKey() + ",url:" + url);
   }
   unregister(url.getServiceKey(), url);
 }