Search in sources :

Example 31 with NotifyListener

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

the class SimpleRegistryService method disconnect.

public void disconnect() {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("Disconnected " + client);
    }
    ConcurrentMap<String, URL> urls = remoteRegistered.get(client);
    if (urls != null && urls.size() > 0) {
        for (Map.Entry<String, URL> entry : urls.entrySet()) {
            super.unregister(entry.getKey(), entry.getValue());
        }
    }
    Map<String, NotifyListener> listeners = remoteListeners.get(client);
    if (listeners != null && listeners.size() > 0) {
        for (Map.Entry<String, NotifyListener> entry : listeners.entrySet()) {
            String service = entry.getKey();
            super.unsubscribe(service, new URL("subscribe", RpcContext.getContext().getRemoteHost(), RpcContext.getContext().getRemotePort(), RegistryService.class.getName(), getSubscribed(service)), entry.getValue());
        }
    }
}
Also used : Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 32 with NotifyListener

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

the class AbstractRegistryService method doNotify.

private void doNotify(String service, List<URL> urls) {
    notified.put(service, urls);
    List<NotifyListener> listeners = notifyListeners.get(service);
    if (listeners != null) {
        for (NotifyListener listener : listeners) {
            try {
                notify(service, urls, listener);
            } catch (Throwable t) {
                logger.error("Failed to notify registry event, service: " + service + ", urls: " + urls + ", cause: " + t.getMessage(), t);
            }
        }
    }
}
Also used : NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 33 with NotifyListener

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

the class MultipleRegistry2S2RTest method testRegistryAndUnRegistry.

@Test
public void testRegistryAndUnRegistry() throws InterruptedException {
    URL serviceUrl = URL.valueOf("http2://multiple/" + SERVICE_NAME + "?notify=false&methods=test1,test2&category=providers");
    // URL serviceUrl2 = URL.valueOf("http2://multiple2/" + SERVICE_NAME + "?notify=false&methods=test1,test2&category=providers");
    multipleRegistry.register(serviceUrl);
    String path = "/dubbo/" + SERVICE_NAME + "/providers";
    List<String> providerList = zookeeperClient.getChildren(path);
    Assertions.assertTrue(!providerList.isEmpty());
    System.out.println(providerList.get(0));
    Assertions.assertNotNull(MultipleRegistryTestUtil.getRedisHashContent(redisServerPort, path, serviceUrl.toFullString()));
    final List<URL> list = new ArrayList<URL>();
    multipleRegistry.subscribe(serviceUrl, new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
            System.out.println("invoke notify: " + urls);
            list.clear();
            list.addAll(urls);
        }
    });
    Thread.sleep(1500);
    Assertions.assertEquals(2, list.size());
    multipleRegistry.unregister(serviceUrl);
    Thread.sleep(1500);
    Assertions.assertEquals(1, list.size());
    List<URL> urls = MultipleRegistryTestUtil.getProviderURLsFromNotifyURLS(list);
    Assertions.assertEquals(1, list.size());
    Assertions.assertEquals("empty", list.get(0).getProtocol());
}
Also used : ArrayList(java.util.ArrayList) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

Example 34 with NotifyListener

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

the class EtcdRegistry method doUnsubscribe.

@Override
public void doUnsubscribe(URL url, NotifyListener listener) {
    ConcurrentMap<NotifyListener, ChildListener> listeners = etcdListeners.get(url);
    if (listeners != null) {
        ChildListener etcdListener = listeners.remove(listener);
        if (etcdListener != null) {
            if (ANY_VALUE.equals(url.getServiceInterface())) {
                String root = toRootPath();
                etcdClient.removeChildListener(root, etcdListener);
            } else {
                // maybe url has many subscribed paths
                for (String path : toUnsubscribedPath(url)) {
                    etcdClient.removeChildListener(path, etcdListener);
                }
            }
        }
        if (listeners.isEmpty()) {
            etcdListeners.remove(url);
        }
    }
}
Also used : ChildListener(org.apache.dubbo.remoting.etcd.ChildListener) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 35 with NotifyListener

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

the class FailbackRegistryTest method testRecover.

@Test
public void testRecover() throws Exception {
    CountDownLatch countDownLatch = new CountDownLatch(6);
    final AtomicReference<Boolean> notified = new AtomicReference<Boolean>(false);
    NotifyListener listener = new NotifyListener() {

        @Override
        public void notify(List<URL> urls) {
            notified.set(Boolean.TRUE);
        }
    };
    MockRegistry mockRegistry = new MockRegistry(registryUrl, countDownLatch);
    mockRegistry.register(serviceUrl);
    mockRegistry.subscribe(serviceUrl, listener);
    Assertions.assertEquals(1, mockRegistry.getRegistered().size());
    Assertions.assertEquals(1, mockRegistry.getSubscribed().size());
    mockRegistry.recover();
    countDownLatch.await();
    Assertions.assertEquals(0, mockRegistry.getFailedRegistered().size());
    FailbackRegistry.Holder h = new FailbackRegistry.Holder(registryUrl, listener);
    Assertions.assertNull(mockRegistry.getFailedSubscribed().get(h));
    Assertions.assertEquals(countDownLatch.getCount(), 0);
}
Also used : AtomicReference(java.util.concurrent.atomic.AtomicReference) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) NotifyListener(org.apache.dubbo.registry.NotifyListener) Test(org.junit.jupiter.api.Test)

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