Search in sources :

Example 16 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project tutorials by eugenp.

the class SimpleRegistryService method subscribe.

public void subscribe(URL url, NotifyListener listener) {
    if (getUrl().getPort() == 0) {
        URL registryUrl = RpcContext.getContext().getUrl();
        if (registryUrl != null && registryUrl.getPort() > 0 && RegistryService.class.getName().equals(registryUrl.getPath())) {
            super.setUrl(registryUrl);
            super.register(registryUrl);
        }
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners == null) {
        remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
        clientListeners = remoteSubscribed.get(client);
    }
    Set<NotifyListener> listeners = clientListeners.get(url);
    if (listeners == null) {
        clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
        listeners = clientListeners.get(url);
    }
    listeners.add(listener);
    super.subscribe(url, listener);
    subscribed(url, listener);
}
Also used : Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 17 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class SimpleRegistryService method subscribe.

public void subscribe(URL url, NotifyListener listener) {
    if (getUrl().getPort() == 0) {
        URL registryUrl = RpcContext.getContext().getUrl();
        if (registryUrl != null && registryUrl.getPort() > 0 && RegistryService.class.getName().equals(registryUrl.getPath())) {
            super.setUrl(registryUrl);
            super.register(registryUrl);
        }
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners == null) {
        remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
        clientListeners = remoteSubscribed.get(client);
    }
    Set<NotifyListener> listeners = clientListeners.get(url);
    if (listeners == null) {
        clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
        listeners = clientListeners.get(url);
    }
    listeners.add(listener);
    super.subscribe(url, listener);
    subscribed(url, listener);
}
Also used : Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 18 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project incubator-dubbo-ops by apache.

the class RegistryContainer method start.

public void start() {
    String url = ConfigUtils.getProperty(REGISTRY_ADDRESS);
    if (url == null || url.length() == 0) {
        throw new IllegalArgumentException("Please set java start argument: -D" + REGISTRY_ADDRESS + "=zookeeper://127.0.0.1:2181");
    }
    registry = (RegistryService) SpringContainer.getContext().getBean("registryService");
    URL subscribeUrl = new URL(Constants.ADMIN_PROTOCOL, NetUtils.getLocalHost(), 0, "", Constants.INTERFACE_KEY, Constants.ANY_VALUE, Constants.GROUP_KEY, Constants.ANY_VALUE, Constants.VERSION_KEY, Constants.ANY_VALUE, Constants.CLASSIFIER_KEY, Constants.ANY_VALUE, Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY + "," + Constants.CONSUMERS_CATEGORY, Constants.CHECK_KEY, String.valueOf(false));
    registry.subscribe(subscribeUrl, new NotifyListener() {

        public void notify(List<URL> urls) {
            if (urls == null || urls.size() == 0) {
                return;
            }
            Map<String, List<URL>> proivderMap = new HashMap<String, List<URL>>();
            Map<String, List<URL>> consumerMap = new HashMap<String, List<URL>>();
            for (URL url : urls) {
                String application = url.getParameter(Constants.APPLICATION_KEY);
                if (application != null && application.length() > 0) {
                    applications.add(application);
                }
                String service = url.getServiceInterface();
                services.add(service);
                String category = url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
                if (Constants.PROVIDERS_CATEGORY.equals(category)) {
                    if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                        serviceProviders.remove(service);
                    } else {
                        List<URL> list = proivderMap.get(service);
                        if (list == null) {
                            list = new ArrayList<URL>();
                            proivderMap.put(service, list);
                        }
                        list.add(url);
                        if (application != null && application.length() > 0) {
                            Set<String> serviceApplications = providerServiceApplications.get(service);
                            if (serviceApplications == null) {
                                providerServiceApplications.put(service, new ConcurrentHashSet<String>());
                                serviceApplications = providerServiceApplications.get(service);
                            }
                            serviceApplications.add(application);
                            Set<String> applicationServices = providerApplicationServices.get(application);
                            if (applicationServices == null) {
                                providerApplicationServices.put(application, new ConcurrentHashSet<String>());
                                applicationServices = providerApplicationServices.get(application);
                            }
                            applicationServices.add(service);
                        }
                    }
                } else if (Constants.CONSUMERS_CATEGORY.equals(category)) {
                    if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                        serviceConsumers.remove(service);
                    } else {
                        List<URL> list = consumerMap.get(service);
                        if (list == null) {
                            list = new ArrayList<URL>();
                            consumerMap.put(service, list);
                        }
                        list.add(url);
                        if (application != null && application.length() > 0) {
                            Set<String> serviceApplications = consumerServiceApplications.get(service);
                            if (serviceApplications == null) {
                                consumerServiceApplications.put(service, new ConcurrentHashSet<String>());
                                serviceApplications = consumerServiceApplications.get(service);
                            }
                            serviceApplications.add(application);
                            Set<String> applicationServices = consumerApplicationServices.get(application);
                            if (applicationServices == null) {
                                consumerApplicationServices.put(application, new ConcurrentHashSet<String>());
                                applicationServices = consumerApplicationServices.get(application);
                            }
                            applicationServices.add(service);
                        }
                    }
                }
            }
            if (proivderMap != null && proivderMap.size() > 0) {
                serviceProviders.putAll(proivderMap);
            }
            if (consumerMap != null && consumerMap.size() > 0) {
                serviceConsumers.putAll(consumerMap);
            }
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 19 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class AbstractRegistryService method doNotify.

private void doNotify(String service, List<URL> urls) {
    notified.put(service, urls);
    List<NotifyListener> listeners = notifyListeners.get(service);
    if (listeners != null) {
        for (NotifyListener listener : listeners) {
            try {
                notify(service, urls, listener);
            } catch (Throwable t) {
                logger.error("Failed to notify registry event, service: " + service + ", urls: " + urls + ", cause: " + t.getMessage(), t);
            }
        }
    }
}
Also used : NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 20 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class SimpleRegistryService method subscribe.

@Override
public void subscribe(String service, URL url, NotifyListener listener) {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("[subscribe] service: " + service + ",client:" + client);
    }
    List<URL> urls = getRegistered().get(service);
    if ((RegistryService.class.getName() + ":0.0.0").equals(service) && (urls == null || urls.size() == 0)) {
        register(service, new URL("dubbo", NetUtils.getLocalHost(), RpcContext.getContext().getLocalPort(), com.alibaba.dubbo.registry.RegistryService.class.getName(), url.getParameters()));
        List<String> rs = registries;
        if (rs != null && rs.size() > 0) {
            for (String registry : rs) {
                register(service, UrlUtils.parseURL(registry, url.getParameters()));
            }
        }
    }
    super.subscribe(service, url, listener);
    Map<String, NotifyListener> listeners = remoteListeners.get(client);
    if (listeners == null) {
        remoteListeners.putIfAbsent(client, new ConcurrentHashMap<String, NotifyListener>());
        listeners = remoteListeners.get(client);
    }
    listeners.put(service, listener);
    urls = getRegistered().get(service);
    if (urls != null && urls.size() > 0) {
        listener.notify(urls);
    }
}
Also used : URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Aggregations

NotifyListener (com.alibaba.dubbo.registry.NotifyListener)30 URL (com.alibaba.dubbo.common.URL)25 Set (java.util.Set)16 ConcurrentHashSet (com.alibaba.dubbo.common.utils.ConcurrentHashSet)15 Map (java.util.Map)15 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)15 ConcurrentMap (java.util.concurrent.ConcurrentMap)13 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Test (org.junit.Test)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 RegistryProtocol (com.alibaba.dubbo.registry.integration.RegistryProtocol)2 ChildListener (com.alibaba.dubbo.remoting.zookeeper.ChildListener)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1