use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class EtcdRegistryTest method test_subscribe_when_register.
@Test
public void test_subscribe_when_register() throws InterruptedException {
Assertions.assertEquals(0, registry.getRegistered().size());
Assertions.assertEquals(0, registry.getSubscribed().size());
CountDownLatch notNotified = new CountDownLatch(2);
final AtomicReference<URL> notifiedUrl = new AtomicReference<URL>();
registry.subscribe(consumerUrl, new NotifyListener() {
public void notify(List<URL> urls) {
notifiedUrl.set(urls.get(0));
notNotified.countDown();
}
});
registry.register(serviceUrl);
Assertions.assertTrue(notNotified.await(15, TimeUnit.SECONDS));
Assertions.assertEquals(serviceUrl.toFullString(), notifiedUrl.get().toFullString());
Map<URL, Set<NotifyListener>> subscribed = registry.getSubscribed();
Assertions.assertEquals(consumerUrl, subscribed.keySet().iterator().next());
}
use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class EtcdRegistryTest method test_unsubscribe.
@Test
public void test_unsubscribe() throws InterruptedException {
Assertions.assertEquals(0, registry.getRegistered().size());
Assertions.assertEquals(0, registry.getSubscribed().size());
CountDownLatch notNotified = new CountDownLatch(2);
final AtomicReference<URL> notifiedUrl = new AtomicReference<URL>();
NotifyListener listener = new NotifyListener() {
public void notify(List<URL> urls) {
if (urls != null) {
for (Iterator<URL> iterator = urls.iterator(); iterator.hasNext(); ) {
URL url = iterator.next();
if (!url.getProtocol().equals("empty")) {
notifiedUrl.set(url);
notNotified.countDown();
}
}
}
}
};
registry.subscribe(consumerUrl, listener);
registry.unsubscribe(consumerUrl, listener);
registry.register(serviceUrl);
Assertions.assertFalse(notNotified.await(2, TimeUnit.SECONDS));
// expect nothing happen
Assertions.assertNull(notifiedUrl.get());
}
use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class EtcdRegistryTest method test_subscribe.
@Test
public void test_subscribe() {
registry.register(serviceUrl);
final AtomicReference<URL> notifiedUrl = new AtomicReference<URL>();
registry.subscribe(consumerUrl, new NotifyListener() {
public void notify(List<URL> urls) {
notifiedUrl.set(urls.get(0));
}
});
Assertions.assertEquals(serviceUrl.toFullString(), notifiedUrl.get().toFullString());
Map<URL, Set<NotifyListener>> arg = registry.getSubscribed();
Assertions.assertEquals(consumerUrl, arg.keySet().iterator().next());
}
use of org.apache.dubbo.registry.NotifyListener in project dubbo by alibaba.
the class ZookeeperRegistryTest method testSubscribe.
@Test
public void testSubscribe() {
NotifyListener listener = mock(NotifyListener.class);
zookeeperRegistry.subscribe(serviceUrl, listener);
Map<URL, Set<NotifyListener>> subscribed = zookeeperRegistry.getSubscribed();
assertThat(subscribed.size(), is(1));
assertThat(subscribed.get(serviceUrl).size(), is(1));
zookeeperRegistry.unsubscribe(serviceUrl, listener);
subscribed = zookeeperRegistry.getSubscribed();
assertThat(subscribed.size(), is(1));
assertThat(subscribed.get(serviceUrl).size(), is(0));
}
use of org.apache.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) && CollectionUtils.isEmpty(urls)) {
register(service, new URL("dubbo", NetUtils.getLocalHost(), RpcContext.getContext().getLocalPort(), 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.computeIfAbsent(client, k -> new ConcurrentHashMap<>());
listeners.put(service, listener);
urls = getRegistered().get(service);
if (urls != null && urls.size() > 0) {
listener.notify(urls);
}
}
Aggregations