Search in sources :

Example 1 with NotifyListener

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

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 2 with NotifyListener

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

the class SimpleRegistryService method disconnect.

public void disconnect() {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("Disconnected " + client);
    }
    Set<URL> urls = remoteRegistered.get(client);
    if (urls != null && urls.size() > 0) {
        for (URL url : urls) {
            unregister(url);
        }
    }
    Map<URL, Set<NotifyListener>> listeners = remoteSubscribed.get(client);
    if (listeners != null && listeners.size() > 0) {
        for (Map.Entry<URL, Set<NotifyListener>> entry : listeners.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                unsubscribe(url, listener);
            }
        }
    }
}
Also used : Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 3 with NotifyListener

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

the class SimpleRegistryService method disconnect.

public void disconnect() {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("Disconnected " + client);
    }
    Set<URL> urls = remoteRegistered.get(client);
    if (urls != null && urls.size() > 0) {
        for (URL url : urls) {
            unregister(url);
        }
    }
    Map<URL, Set<NotifyListener>> listeners = remoteSubscribed.get(client);
    if (listeners != null && listeners.size() > 0) {
        for (Map.Entry<URL, Set<NotifyListener>> entry : listeners.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                unsubscribe(url, listener);
            }
        }
    }
}
Also used : Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 4 with NotifyListener

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

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 5 with NotifyListener

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

the class AbstractRegistry method lookup.

public List<URL> lookup(URL url) {
    List<URL> result = new ArrayList<URL>();
    Map<String, List<URL>> notifiedUrls = getNotified().get(url);
    if (notifiedUrls != null && notifiedUrls.size() > 0) {
        for (List<URL> urls : notifiedUrls.values()) {
            for (URL u : urls) {
                if (!Constants.EMPTY_PROTOCOL.equals(u.getProtocol())) {
                    result.add(u);
                }
            }
        }
    } else {
        final AtomicReference<List<URL>> reference = new AtomicReference<List<URL>>();
        NotifyListener listener = new NotifyListener() {

            public void notify(List<URL> urls) {
                reference.set(urls);
            }
        };
        // Subscribe logic guarantees the first notify to return
        subscribe(url, listener);
        List<URL> urls = reference.get();
        if (urls != null && !urls.isEmpty()) {
            for (URL u : urls) {
                if (!Constants.EMPTY_PROTOCOL.equals(u.getProtocol())) {
                    result.add(u);
                }
            }
        }
    }
    return result;
}
Also used : ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) AtomicReference(java.util.concurrent.atomic.AtomicReference) 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