Search in sources :

Example 1 with NotifyListener

use of com.networknt.registry.NotifyListener in project light-4j by networknt.

the class FailbackRegistry method retry.

private void retry() {
    if (!failedRegistered.isEmpty()) {
        Set<URL> failed = new HashSet<>(failedRegistered);
        logger.info("[{}] Retry register {}", registryClassName, failed);
        try {
            for (URL url : failed) {
                super.register(url);
                failedRegistered.remove(url);
            }
        } catch (Exception e) {
            logger.error(String.format("[%s] Failed to retry register, retry later, failedRegistered.size=%s, cause=%s", registryClassName, failedRegistered.size(), e.getMessage()), e);
        }
    }
    if (!failedUnregistered.isEmpty()) {
        Set<URL> failed = new HashSet<>(failedUnregistered);
        logger.info("[{}] Retry unregister {}", registryClassName, failed);
        try {
            for (URL url : failed) {
                super.unregister(url);
                failedUnregistered.remove(url);
            }
        } catch (Exception e) {
            logger.error(String.format("[%s] Failed to retry unregister, retry later, failedUnregistered.size=%s, cause=%s", registryClassName, failedUnregistered.size(), e.getMessage()), e);
        }
    }
    if (!failedSubscribed.isEmpty()) {
        Map<URL, Set<NotifyListener>> failed = new HashMap<>(failedSubscribed);
        for (Map.Entry<URL, Set<NotifyListener>> entry : new HashMap<>(failed).entrySet()) {
            if (entry.getValue() == null || entry.getValue().size() == 0) {
                failed.remove(entry.getKey());
            }
        }
        if (failed.size() > 0) {
            logger.info("[{}] Retry subscribe {}", registryClassName, failed);
            try {
                for (Map.Entry<URL, Set<NotifyListener>> entry : failed.entrySet()) {
                    URL url = entry.getKey();
                    Set<NotifyListener> listeners = entry.getValue();
                    for (NotifyListener listener : listeners) {
                        super.subscribe(url, listener);
                        listeners.remove(listener);
                    }
                }
            } catch (Exception e) {
                logger.error(String.format("[%s] Failed to retry subscribe, retry later, failedSubscribed.size=%s, cause=%s", registryClassName, failedSubscribed.size(), e.getMessage()), e);
            }
        }
    }
    if (!failedUnsubscribed.isEmpty()) {
        Map<URL, Set<NotifyListener>> failed = new HashMap<>(failedUnsubscribed);
        for (Map.Entry<URL, Set<NotifyListener>> entry : new HashMap<>(failed).entrySet()) {
            if (entry.getValue() == null || entry.getValue().size() == 0) {
                failed.remove(entry.getKey());
            }
        }
        if (failed.size() > 0) {
            logger.info("[{}] Retry unsubscribe {}", registryClassName, failed);
            try {
                for (Map.Entry<URL, Set<NotifyListener>> entry : failed.entrySet()) {
                    URL url = entry.getKey();
                    Set<NotifyListener> listeners = entry.getValue();
                    for (NotifyListener listener : listeners) {
                        super.unsubscribe(url, listener);
                        listeners.remove(listener);
                    }
                }
            } catch (Exception e) {
                logger.error(String.format("[%s] Failed to retry unsubscribe, retry later, failedUnsubscribed.size=%s, cause=%s", registryClassName, failedUnsubscribed.size(), e.getMessage()), e);
            }
        }
    }
}
Also used : ConcurrentHashSet(com.networknt.utility.ConcurrentHashSet) Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) URL(com.networknt.registry.URL) FrameworkException(com.networknt.exception.FrameworkException) ConcurrentHashSet(com.networknt.utility.ConcurrentHashSet) HashSet(java.util.HashSet) NotifyListener(com.networknt.registry.NotifyListener)

Example 2 with NotifyListener

use of com.networknt.registry.NotifyListener in project light-4j by networknt.

the class CommandServiceManager method notifyService.

@Override
public void notifyService(URL serviceUrl, URL registryUrl, List<URL> urls) {
    if (registry == null) {
        throw new FrameworkException(new Status(REGISTRY_IS_NULL));
    }
    URL urlCopy = serviceUrl.createCopy();
    String groupName = urlCopy.getParameter(URLParamType.group.getName(), URLParamType.group.getValue());
    groupServiceCache.put(groupName, urls);
    List<URL> finalResult = new ArrayList<URL>();
    if (logger.isInfoEnabled())
        logger.info("command cache is null. service:" + serviceUrl.toSimpleString());
    // if no command cache, return group
    finalResult.addAll(discoverOneGroup(refUrl));
    for (NotifyListener notifyListener : notifySet) {
        notifyListener.notify(registry.getUrl(), finalResult);
    }
}
Also used : Status(com.networknt.status.Status) FrameworkException(com.networknt.exception.FrameworkException) ArrayList(java.util.ArrayList) URL(com.networknt.registry.URL) NotifyListener(com.networknt.registry.NotifyListener)

Aggregations

FrameworkException (com.networknt.exception.FrameworkException)2 NotifyListener (com.networknt.registry.NotifyListener)2 URL (com.networknt.registry.URL)2 Status (com.networknt.status.Status)1 ConcurrentHashSet (com.networknt.utility.ConcurrentHashSet)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1