Search in sources :

Example 11 with NotifyListener

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

the class RegistryProtocolTest method testNotifyOverride_notmatch.

/**
 * The name of the service does not match and can't override invoker
 * Service name matching, service version number mismatch
 */
@Test
public void testNotifyOverride_notmatch() throws Exception {
    URL newRegistryUrl = registryUrl.addParameter(Constants.EXPORT_KEY, serviceUrl);
    Invoker<RegistryProtocolTest> invoker = new MockInvoker<RegistryProtocolTest>(RegistryProtocolTest.class, newRegistryUrl);
    Exporter<?> exporter = protocol.export(invoker);
    RegistryProtocol rprotocol = RegistryProtocol.getRegistryProtocol();
    NotifyListener listener = getListener(rprotocol);
    List<URL> urls = new ArrayList<URL>();
    urls.add(URL.valueOf("override://0.0.0.0/com.alibaba.dubbo.registry.protocol.HackService?timeout=100"));
    listener.notify(urls);
    assertEquals(true, exporter.getInvoker().isAvailable());
    assertEquals(null, exporter.getInvoker().getUrl().getParameter("timeout"));
    exporter.unexport();
    destroyRegistryProtocol();
}
Also used : RegistryProtocol(com.alibaba.dubbo.registry.integration.RegistryProtocol) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener) Test(org.junit.Test)

Example 12 with NotifyListener

use of com.alibaba.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(), com.alibaba.dubbo.registry.RegistryService.class.getName(), getSubscribed(service)), entry.getValue());
        }
    }
}
Also used : Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 13 with NotifyListener

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

the class MulticastRegistry method unregistered.

protected void unregistered(URL url) {
    for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
        URL key = entry.getKey();
        if (UrlUtils.isMatch(key, url)) {
            Set<URL> urls = received.get(key);
            if (urls != null) {
                urls.remove(url);
            }
            if (urls == null || urls.isEmpty()) {
                if (urls == null) {
                    urls = new ConcurrentHashSet<URL>();
                }
                URL empty = url.setProtocol(Constants.EMPTY_PROTOCOL);
                urls.add(empty);
            }
            List<URL> list = toList(urls);
            for (NotifyListener listener : entry.getValue()) {
                notify(key, listener, list);
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 14 with NotifyListener

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

the class MulticastRegistry method registered.

protected void registered(URL url) {
    for (Map.Entry<URL, Set<NotifyListener>> entry : getSubscribed().entrySet()) {
        URL key = entry.getKey();
        if (UrlUtils.isMatch(key, url)) {
            Set<URL> urls = received.get(key);
            if (urls == null) {
                received.putIfAbsent(key, new ConcurrentHashSet<URL>());
                urls = received.get(key);
            }
            urls.add(url);
            List<URL> list = toList(urls);
            for (NotifyListener listener : entry.getValue()) {
                notify(key, listener, list);
                synchronized (listener) {
                    listener.notify();
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 15 with NotifyListener

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

the class RedisRegistry method doNotify.

private void doNotify(Jedis jedis, Collection<String> keys, URL url, Collection<NotifyListener> listeners) {
    if (keys == null || keys.isEmpty() || listeners == null || listeners.isEmpty()) {
        return;
    }
    long now = System.currentTimeMillis();
    List<URL> result = new ArrayList<URL>();
    List<String> categories = Arrays.asList(url.getParameter(Constants.CATEGORY_KEY, new String[0]));
    String consumerService = url.getServiceInterface();
    for (String key : keys) {
        if (!Constants.ANY_VALUE.equals(consumerService)) {
            String prvoiderService = toServiceName(key);
            if (!prvoiderService.equals(consumerService)) {
                continue;
            }
        }
        String category = toCategoryName(key);
        if (!categories.contains(Constants.ANY_VALUE) && !categories.contains(category)) {
            continue;
        }
        List<URL> urls = new ArrayList<URL>();
        Map<String, String> values = jedis.hgetAll(key);
        if (values != null && values.size() > 0) {
            for (Map.Entry<String, String> entry : values.entrySet()) {
                URL u = URL.valueOf(entry.getKey());
                if (!u.getParameter(Constants.DYNAMIC_KEY, true) || Long.parseLong(entry.getValue()) >= now) {
                    if (UrlUtils.isMatch(url, u)) {
                        urls.add(u);
                    }
                }
            }
        }
        if (urls.isEmpty()) {
            urls.add(url.setProtocol(Constants.EMPTY_PROTOCOL).setAddress(Constants.ANYHOST_VALUE).setPath(toServiceName(key)).addParameter(Constants.CATEGORY_KEY, category));
        }
        result.addAll(urls);
        if (logger.isInfoEnabled()) {
            logger.info("redis notify: " + key + " = " + urls);
        }
    }
    if (result == null || result.isEmpty()) {
        return;
    }
    for (NotifyListener listener : listeners) {
        notify(url, listener, result);
    }
}
Also used : ArrayList(java.util.ArrayList) HashMap(java.util.HashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) 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