Search in sources :

Example 26 with NotifyListener

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());
}
Also used : Set(java.util.Set) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 27 with NotifyListener

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());
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 28 with NotifyListener

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());
}
Also used : Set(java.util.Set) AtomicReference(java.util.concurrent.atomic.AtomicReference) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 29 with NotifyListener

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));
}
Also used : Set(java.util.Set) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 30 with NotifyListener

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);
    }
}
Also used : URL(org.apache.dubbo.common.URL) 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