Search in sources :

Example 51 with NotifyListener

use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class ZookeeperRegistry method doSubscribe.

@Override
public void doSubscribe(final URL url, final NotifyListener listener) {
    try {
        if (ANY_VALUE.equals(url.getServiceInterface())) {
            String root = toRootPath();
            ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
            ChildListener zkListener = listeners.computeIfAbsent(listener, k -> (parentPath, currentChilds) -> {
                for (String child : currentChilds) {
                    child = URL.decode(child);
                    if (!anyServices.contains(child)) {
                        anyServices.add(child);
                        subscribe(url.setPath(child).addParameters(INTERFACE_KEY, child, Constants.CHECK_KEY, String.valueOf(false)), k);
                    }
                }
            });
            zkClient.create(root, false);
            List<String> services = zkClient.addChildListener(root, zkListener);
            if (CollectionUtils.isNotEmpty(services)) {
                for (String service : services) {
                    service = URL.decode(service);
                    anyServices.add(service);
                    subscribe(url.setPath(service).addParameters(INTERFACE_KEY, service, Constants.CHECK_KEY, String.valueOf(false)), listener);
                }
            }
        } else {
            CountDownLatch latch = new CountDownLatch(1);
            try {
                List<URL> urls = new ArrayList<>();
                for (String path : toCategoriesPath(url)) {
                    ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.computeIfAbsent(url, k -> new ConcurrentHashMap<>());
                    ChildListener zkListener = listeners.computeIfAbsent(listener, k -> new RegistryChildListenerImpl(url, k, latch));
                    if (zkListener instanceof RegistryChildListenerImpl) {
                        ((RegistryChildListenerImpl) zkListener).setLatch(latch);
                    }
                    zkClient.create(path, false);
                    List<String> children = zkClient.addChildListener(path, zkListener);
                    if (children != null) {
                        urls.addAll(toUrlsWithEmpty(url, path, children));
                    }
                }
                notify(url, listener, urls);
            } finally {
                // tells the listener to run only after the sync notification of main thread finishes.
                latch.countDown();
            }
        }
    } catch (Throwable e) {
        throw new RpcException("Failed to subscribe " + url + " to zookeeper " + getUrl() + ", cause: " + e.getMessage(), e);
    }
}
Also used : ChildListener(org.apache.dubbo.remoting.zookeeper.ChildListener) ArrayList(java.util.ArrayList) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) RpcException(org.apache.dubbo.rpc.RpcException) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Aggregations

NotifyListener (org.apache.dubbo.registry.NotifyListener)51 URL (org.apache.dubbo.common.URL)43 Test (org.junit.jupiter.api.Test)29 Set (java.util.Set)28 Map (java.util.Map)21 ArrayList (java.util.ArrayList)19 List (java.util.List)17 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)13 ConcurrentMap (java.util.concurrent.ConcurrentMap)11 HashSet (java.util.HashSet)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 EMPTY_PROTOCOL (org.apache.dubbo.common.constants.RegistryConstants.EMPTY_PROTOCOL)9 LinkedHashMap (java.util.LinkedHashMap)8 Objects (java.util.Objects)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 HashMap (java.util.HashMap)7 MatcherAssert (org.hamcrest.MatcherAssert)7 Matchers (org.hamcrest.Matchers)7 AfterEach (org.junit.jupiter.api.AfterEach)7