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;
}
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;
}
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;
}
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);
}
}
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);
}
}
}
}
Aggregations