Search in sources :

Example 1 with ChildListener

use of org.apache.dubbo.remoting.zookeeper.ChildListener in project dubbo by alibaba.

the class ZookeeperRegistry method doUnsubscribe.

@Override
public void doUnsubscribe(URL url, NotifyListener listener) {
    ConcurrentMap<NotifyListener, ChildListener> listeners = zkListeners.get(url);
    if (listeners != null) {
        ChildListener zkListener = listeners.remove(listener);
        if (zkListener != null) {
            if (ANY_VALUE.equals(url.getServiceInterface())) {
                String root = toRootPath();
                zkClient.removeChildListener(root, zkListener);
            } else {
                for (String path : toCategoriesPath(url)) {
                    zkClient.removeChildListener(path, zkListener);
                }
            }
        }
        if (listeners.isEmpty()) {
            zkListeners.remove(url);
        }
    }
}
Also used : ChildListener(org.apache.dubbo.remoting.zookeeper.ChildListener) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 2 with ChildListener

use of org.apache.dubbo.remoting.zookeeper.ChildListener in project dubbo by alibaba.

the class ZookeeperMetadataReport method addServiceMappingListener.

private List<String> addServiceMappingListener(String path, String serviceKey, MappingListener listener) {
    ChildListener zkListener = new ChildListener() {

        @Override
        public void childChanged(String path, List<String> children) {
            MappingChangedEvent event = new MappingChangedEvent();
            event.setServiceKey(serviceKey);
            event.setApps(null != children ? new HashSet<>(children) : null);
            listener.onEvent(event);
        }
    };
    List<String> childNodes = zkClient.addChildListener(path, zkListener);
    listenerMap.put(path, zkListener);
    return childNodes;
}
Also used : MappingChangedEvent(org.apache.dubbo.metadata.MappingChangedEvent) ChildListener(org.apache.dubbo.remoting.zookeeper.ChildListener) ArrayList(java.util.ArrayList) List(java.util.List) HashSet(java.util.HashSet)

Example 3 with ChildListener

use of org.apache.dubbo.remoting.zookeeper.ChildListener in project dubbo by alibaba.

the class CuratorZookeeperClientTest method testRemoveChildrenListener.

@Test
public void testRemoveChildrenListener() {
    ChildListener childListener = mock(ChildListener.class);
    curatorClient.addChildListener("/children", childListener);
    curatorClient.removeChildListener("/children", childListener);
}
Also used : ChildListener(org.apache.dubbo.remoting.zookeeper.ChildListener) Test(org.junit.jupiter.api.Test)

Example 4 with ChildListener

use of org.apache.dubbo.remoting.zookeeper.ChildListener 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)

Example 5 with ChildListener

use of org.apache.dubbo.remoting.zookeeper.ChildListener in project dubbo by alibaba.

the class Curator5ZookeeperClientTest method testRemoveChildrenListener.

@Test
public void testRemoveChildrenListener() {
    ChildListener childListener = mock(ChildListener.class);
    curatorClient.addChildListener("/children", childListener);
    curatorClient.removeChildListener("/children", childListener);
}
Also used : ChildListener(org.apache.dubbo.remoting.zookeeper.ChildListener) Test(org.junit.jupiter.api.Test)

Aggregations

ChildListener (org.apache.dubbo.remoting.zookeeper.ChildListener)5 ArrayList (java.util.ArrayList)2 NotifyListener (org.apache.dubbo.registry.NotifyListener)2 Test (org.junit.jupiter.api.Test)2 HashSet (java.util.HashSet)1 List (java.util.List)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 URL (org.apache.dubbo.common.URL)1 MappingChangedEvent (org.apache.dubbo.metadata.MappingChangedEvent)1 RpcException (org.apache.dubbo.rpc.RpcException)1