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);
}
}
}
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;
}
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);
}
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);
}
}
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);
}
Aggregations