Search in sources :

Example 1 with NotifyListener

use of com.weibo.api.motan.registry.NotifyListener in project motan by weibocom.

the class CommandServiceManager method notifyService.

@Override
public void notifyService(URL serviceUrl, URL registryUrl, List<URL> urls) {
    if (registry == null) {
        throw new MotanFrameworkException("registry must be set.");
    }
    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 (commandCache != null) {
        Map<String, Integer> weights = new HashMap<String, Integer>();
        finalResult = discoverServiceWithCommand(refUrl, weights, commandCache);
    } else {
        LoggerUtil.info("command cache is null. service:" + serviceUrl.toSimpleString());
        // 没有命令时,只返回这个manager实际group对应的结果
        finalResult.addAll(discoverOneGroup(refUrl));
    }
    for (NotifyListener notifyListener : notifySet) {
        notifyListener.notify(registry.getUrl(), finalResult);
    }
}
Also used : MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(com.weibo.api.motan.rpc.URL) NotifyListener(com.weibo.api.motan.registry.NotifyListener)

Example 2 with NotifyListener

use of com.weibo.api.motan.registry.NotifyListener in project motan by weibocom.

the class FailbackRegistry method retry.

private void retry() {
    if (!failedRegistered.isEmpty()) {
        Set<URL> failed = new HashSet<URL>(failedRegistered);
        LoggerUtil.info("[{}] Retry register {}", registryClassName, failed);
        try {
            for (URL url : failed) {
                super.register(url);
                failedRegistered.remove(url);
            }
        } catch (Exception e) {
            LoggerUtil.warn(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<URL>(failedUnregistered);
        LoggerUtil.info("[{}] Retry unregister {}", registryClassName, failed);
        try {
            for (URL url : failed) {
                super.unregister(url);
                failedUnregistered.remove(url);
            }
        } catch (Exception e) {
            LoggerUtil.warn(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<URL, Set<NotifyListener>>(failedSubscribed);
        for (Map.Entry<URL, Set<NotifyListener>> entry : new HashMap<URL, Set<NotifyListener>>(failed).entrySet()) {
            if (entry.getValue() == null || entry.getValue().size() == 0) {
                failed.remove(entry.getKey());
            }
        }
        if (failed.size() > 0) {
            LoggerUtil.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) {
                LoggerUtil.warn(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<URL, Set<NotifyListener>>(failedUnsubscribed);
        for (Map.Entry<URL, Set<NotifyListener>> entry : new HashMap<URL, Set<NotifyListener>>(failed).entrySet()) {
            if (entry.getValue() == null || entry.getValue().size() == 0) {
                failed.remove(entry.getKey());
            }
        }
        if (failed.size() > 0) {
            LoggerUtil.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) {
                LoggerUtil.warn(String.format("[%s] Failed to retry unsubscribe, retry later, failedUnsubscribed.size=%s, cause=%s", registryClassName, failedUnsubscribed.size(), e.getMessage()), e);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashSet(com.weibo.api.motan.util.ConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) URL(com.weibo.api.motan.rpc.URL) MotanFrameworkException(com.weibo.api.motan.exception.MotanFrameworkException) HashSet(java.util.HashSet) ConcurrentHashSet(com.weibo.api.motan.util.ConcurrentHashSet) NotifyListener(com.weibo.api.motan.registry.NotifyListener)

Example 3 with NotifyListener

use of com.weibo.api.motan.registry.NotifyListener in project motan by weibocom.

the class CommandServiceManager method notifyCommand.

@Override
public void notifyCommand(URL serviceUrl, String commandString) {
    LoggerUtil.info("CommandServiceManager notify command. service:" + serviceUrl.toSimpleString() + ", command:" + commandString);
    if (!MotanSwitcherUtil.isOpen(MOTAN_COMMAND_SWITCHER) || commandString == null) {
        LoggerUtil.info("command reset empty since swither is close.");
        commandString = "";
    }
    List<URL> finalResult = new ArrayList<URL>();
    URL urlCopy = serviceUrl.createCopy();
    if (!StringUtils.equals(commandString, commandStringCache)) {
        commandStringCache = commandString;
        commandCache = RpcCommandUtil.stringToCommand(commandStringCache);
        Map<String, Integer> weights = new HashMap<String, Integer>();
        if (commandCache != null) {
            commandCache.sort();
            finalResult = discoverServiceWithCommand(refUrl, weights, commandCache);
        } else {
            // 如果是指令有异常时,应当按没有指令处理,防止错误指令导致服务异常
            if (StringUtils.isNotBlank(commandString)) {
                LoggerUtil.warn("command parse fail, ignored! command:" + commandString);
                commandString = "";
            }
            // 没有命令时,只返回这个manager实际group对应的结果
            finalResult.addAll(discoverOneGroup(refUrl));
        }
        // 指令变化时,删除不再有效的缓存,取消订阅不再有效的group
        Set<String> groupKeys = groupServiceCache.keySet();
        for (String gk : groupKeys) {
            if (!weights.containsKey(gk)) {
                groupServiceCache.remove(gk);
                URL urlTemp = urlCopy.createCopy();
                urlTemp.addParameter(URLParamType.group.getName(), gk);
                registry.unsubscribeService(urlTemp, this);
            }
        }
    } else {
        LoggerUtil.info("command not change. url:" + serviceUrl.toSimpleString());
        // 指令没有变化,什么也不做
        return;
    }
    for (NotifyListener notifyListener : notifySet) {
        notifyListener.notify(registry.getUrl(), finalResult);
    }
    // 当指令从有改到无时,会触发取消订阅所有的group,需要重新订阅本组的service
    if ("".equals(commandString)) {
        LoggerUtil.info("reSub service" + refUrl.toSimpleString());
        registry.subscribeService(refUrl, this);
    }
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) URL(com.weibo.api.motan.rpc.URL) NotifyListener(com.weibo.api.motan.registry.NotifyListener)

Aggregations

NotifyListener (com.weibo.api.motan.registry.NotifyListener)3 URL (com.weibo.api.motan.rpc.URL)3 HashMap (java.util.HashMap)3 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)3 MotanFrameworkException (com.weibo.api.motan.exception.MotanFrameworkException)2 ArrayList (java.util.ArrayList)2 ConcurrentHashSet (com.weibo.api.motan.util.ConcurrentHashSet)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 Set (java.util.Set)1