Search in sources :

Example 26 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class AbstractRegistry method destroy.

public void destroy() {
    if (logger.isInfoEnabled()) {
        logger.info("Destroy registry:" + getUrl());
    }
    Set<URL> destroyRegistered = new HashSet<URL>(getRegistered());
    if (!destroyRegistered.isEmpty()) {
        for (URL url : new HashSet<URL>(getRegistered())) {
            if (url.getParameter(Constants.DYNAMIC_KEY, true)) {
                try {
                    unregister(url);
                    if (logger.isInfoEnabled()) {
                        logger.info("Destroy unregister url " + url);
                    }
                } catch (Throwable t) {
                    logger.warn("Failed to unregister url " + url + " to registry " + getUrl() + " on destroy, cause: " + t.getMessage(), t);
                }
            }
        }
    }
    Map<URL, Set<NotifyListener>> destroySubscribed = new HashMap<URL, Set<NotifyListener>>(getSubscribed());
    if (!destroySubscribed.isEmpty()) {
        for (Map.Entry<URL, Set<NotifyListener>> entry : destroySubscribed.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                try {
                    unsubscribe(url, listener);
                    if (logger.isInfoEnabled()) {
                        logger.info("Destroy unsubscribe url " + url);
                    }
                } catch (Throwable t) {
                    logger.warn("Failed to unsubscribe url " + url + " to registry " + getUrl() + " on destroy, cause: " + t.getMessage(), t);
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.alibaba.dubbo.common.URL) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 27 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class AbstractRegistry method recover.

protected void recover() throws Exception {
    // register
    Set<URL> recoverRegistered = new HashSet<URL>(getRegistered());
    if (!recoverRegistered.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Recover register url " + recoverRegistered);
        }
        for (URL url : recoverRegistered) {
            register(url);
        }
    }
    // subscribe
    Map<URL, Set<NotifyListener>> recoverSubscribed = new HashMap<URL, Set<NotifyListener>>(getSubscribed());
    if (!recoverSubscribed.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Recover subscribe url " + recoverSubscribed.keySet());
        }
        for (Map.Entry<URL, Set<NotifyListener>> entry : recoverSubscribed.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                subscribe(url, listener);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.alibaba.dubbo.common.URL) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 28 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class FailbackRegistry method recover.

@Override
protected void recover() throws Exception {
    // register
    Set<URL> recoverRegistered = new HashSet<URL>(getRegistered());
    if (!recoverRegistered.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Recover register url " + recoverRegistered);
        }
        for (URL url : recoverRegistered) {
            failedRegistered.add(url);
        }
    }
    // subscribe
    Map<URL, Set<NotifyListener>> recoverSubscribed = new HashMap<URL, Set<NotifyListener>>(getSubscribed());
    if (!recoverSubscribed.isEmpty()) {
        if (logger.isInfoEnabled()) {
            logger.info("Recover subscribe url " + recoverSubscribed.keySet());
        }
        for (Map.Entry<URL, Set<NotifyListener>> entry : recoverSubscribed.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                addFailedSubscribed(url, listener);
            }
        }
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 29 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project dubbo by alibaba.

the class FailbackRegistryTest method testDoRetry_nofify.

@Test
public void testDoRetry_nofify() throws Exception {
    // Initial value 0
    final AtomicInteger count = new AtomicInteger(0);
    NotifyListener listner = new NotifyListener() {

        public void notify(List<URL> urls) {
            count.incrementAndGet();
            // The exception is thrown for the first time to see if the back will be called again to incrementAndGet
            if (count.get() == 1l) {
                throw new RuntimeException("test exception please ignore");
            }
        }
    };
    registry = new MockRegistry(registryUrl, new CountDownLatch(0));
    registry.subscribe(serviceUrl.setProtocol(Constants.CONSUMER_PROTOCOL).addParameters(CollectionUtils.toStringMap("check", "false")), listner);
    // Make sure that the subscribe call has just been called once count.incrementAndGet after the call is completed
    assertEquals(1, count.get());
    // Wait for the timer.
    for (int i = 0; i < trytimes; i++) {
        System.out.println("failback notify retry ,times:" + i);
        if (count.get() == 2)
            break;
        Thread.sleep(sleeptime);
    }
    assertEquals(2, count.get());
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) List(java.util.List) CountDownLatch(java.util.concurrent.CountDownLatch) NotifyListener(com.alibaba.dubbo.registry.NotifyListener) Test(org.junit.Test)

Example 30 with NotifyListener

use of com.alibaba.dubbo.registry.NotifyListener in project tutorials by eugenp.

the class SimpleRegistryService method disconnect.

public void disconnect() {
    String client = RpcContext.getContext().getRemoteAddressString();
    if (logger.isInfoEnabled()) {
        logger.info("Disconnected " + client);
    }
    Set<URL> urls = remoteRegistered.get(client);
    if (urls != null && urls.size() > 0) {
        for (URL url : urls) {
            unregister(url);
        }
    }
    Map<URL, Set<NotifyListener>> listeners = remoteSubscribed.get(client);
    if (listeners != null && listeners.size() > 0) {
        for (Map.Entry<URL, Set<NotifyListener>> entry : listeners.entrySet()) {
            URL url = entry.getKey();
            for (NotifyListener listener : entry.getValue()) {
                unsubscribe(url, listener);
            }
        }
    }
}
Also used : Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Aggregations

NotifyListener (com.alibaba.dubbo.registry.NotifyListener)30 URL (com.alibaba.dubbo.common.URL)25 Set (java.util.Set)16 ConcurrentHashSet (com.alibaba.dubbo.common.utils.ConcurrentHashSet)15 Map (java.util.Map)15 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)15 ConcurrentMap (java.util.concurrent.ConcurrentMap)13 HashMap (java.util.HashMap)11 HashSet (java.util.HashSet)9 ArrayList (java.util.ArrayList)7 List (java.util.List)7 Test (org.junit.Test)6 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 CountDownLatch (java.util.concurrent.CountDownLatch)3 RegistryProtocol (com.alibaba.dubbo.registry.integration.RegistryProtocol)2 ChildListener (com.alibaba.dubbo.remoting.zookeeper.ChildListener)1 RpcException (com.alibaba.dubbo.rpc.RpcException)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1