Search in sources :

Example 11 with URL

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

the class ConsulUtils method buildUrl.

/**
 * build url from service
 *
 * @param service consul service
 * @return URL object
 */
public static URL buildUrl(ConsulService service) {
    URL url = null;
    if (url == null) {
        Map<String, String> params = new HashMap<String, String>();
        // String group = service.getName();
        // params.put(URLParamType.group.getName(), group);
        // params.put(URLParamType.nodeType.getName(), Constants.NODE_TYPE_SERVICE);
        url = new URLImpl(ConsulConstants.DEFAULT_PROTOCOL, service.getAddress(), service.getPort(), ConsulUtils.getPathFromServiceId(service.getId()), params);
    }
    return url;
}
Also used : HashMap(java.util.HashMap) URL(com.networknt.registry.URL) URLImpl(com.networknt.registry.URLImpl)

Example 12 with URL

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

the class MockUtils method getMockUrl.

public static URL getMockUrl(String address, int port) {
    Map<String, String> params = new HashMap<>();
    // params.put(URLParamType.group.getName(), group);
    // params.put(URLParamType.protocol.getName(), protocol);
    URL url = new URLImpl(protocol, address, port, path, params);
    return url;
}
Also used : HashMap(java.util.HashMap) URL(com.networknt.registry.URL) URLImpl(com.networknt.registry.URLImpl)

Example 13 with URL

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

the class AbstractRegistry method discover.

@SuppressWarnings("unchecked")
@Override
public List<URL> discover(URL url) {
    if (url == null) {
        logger.warn("[{}] discover with malformed param, refUrl is null", registryClassName);
        return Collections.EMPTY_LIST;
    }
    url = url.createCopy();
    List<URL> results = new ArrayList<>();
    Map<String, List<URL>> categoryUrls = subscribedCategoryResponses.get(url);
    if (categoryUrls != null && categoryUrls.size() > 0) {
        for (List<URL> urls : categoryUrls.values()) {
            for (URL tempUrl : urls) {
                results.add(tempUrl.createCopy());
            }
        }
    } else {
        List<URL> urlsDiscovered = doDiscover(url);
        if (urlsDiscovered != null) {
            for (URL u : urlsDiscovered) {
                results.add(u.createCopy());
            }
        }
    }
    return results;
}
Also used : URL(com.networknt.registry.URL)

Example 14 with URL

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

the class AbstractRegistry method notify.

/*
    protected void notify(URL refUrl, NotifyListener listener, List<URL> urls) {
        if (listener == null || urls == null) {
            return;
        }
        Map<String, List<URL>> nodeTypeUrlsInRs = new HashMap<>();
        for (URL surl : urls) {
            String nodeType = surl.getParameter(URLParamType.nodeType.getName(), URLParamType.nodeType.getValue());
            List<URL> oneNodeTypeUrls = nodeTypeUrlsInRs.get(nodeType);
            if (oneNodeTypeUrls == null) {
                nodeTypeUrlsInRs.put(nodeType, new ArrayList<>());
                oneNodeTypeUrls = nodeTypeUrlsInRs.get(nodeType);
            }
            oneNodeTypeUrls.add(surl);
        }

        Map<String, List<URL>> curls = subscribedCategoryResponses.get(refUrl);
        if (curls == null) {
            subscribedCategoryResponses.putIfAbsent(refUrl, new ConcurrentHashMap<>());
            curls = subscribedCategoryResponses.get(refUrl);
        }

        // refresh local urls cache
        for (String nodeType : nodeTypeUrlsInRs.keySet()) {
            curls.put(nodeType, nodeTypeUrlsInRs.get(nodeType));
        }

        for (List<URL> us : nodeTypeUrlsInRs.values()) {
            listener.notify(getUrl(), us);
        }
    }
    */
protected void notify(URL refUrl, NotifyListener listener, List<URL> urls) {
    if (listener == null || urls == null) {
        return;
    }
    Map<String, List<URL>> serviceNameUrls = new HashMap<>();
    for (URL surl : urls) {
        String serviceName = surl.getPath();
        List<URL> serviceUrlList = serviceNameUrls.get(serviceName);
        if (serviceUrlList == null) {
            serviceNameUrls.put(serviceName, new ArrayList<>());
            serviceUrlList = serviceNameUrls.get(serviceName);
        }
        serviceUrlList.add(surl);
    }
    Map<String, List<URL>> curls = subscribedCategoryResponses.get(refUrl);
    if (curls == null) {
        subscribedCategoryResponses.putIfAbsent(refUrl, new ConcurrentHashMap<>());
        curls = subscribedCategoryResponses.get(refUrl);
    }
    // refresh local urls cache
    for (String serviceName : serviceNameUrls.keySet()) {
        curls.put(serviceName, serviceNameUrls.get(serviceName));
    }
    for (List<URL> us : serviceNameUrls.values()) {
        listener.notify(getUrl(), us);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.networknt.registry.URL)

Example 15 with URL

use of com.networknt.registry.URL 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)

Aggregations

URL (com.networknt.registry.URL)25 ArrayList (java.util.ArrayList)11 URLImpl (com.networknt.registry.URLImpl)7 HashMap (java.util.HashMap)7 Test (org.junit.Test)6 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)5 FrameworkException (com.networknt.exception.FrameworkException)3 List (java.util.List)3 Map (java.util.Map)3 NotifyListener (com.networknt.registry.NotifyListener)2 Registry (com.networknt.registry.Registry)1 ServiceListener (com.networknt.registry.support.command.ServiceListener)1 Status (com.networknt.status.Status)1 ConcurrentHashSet (com.networknt.utility.ConcurrentHashSet)1 HashSet (java.util.HashSet)1 Set (java.util.Set)1