Search in sources :

Example 1 with ConcurrentHashSet

use of org.apache.dubbo.common.utils.ConcurrentHashSet 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 (CollectionUtils.isEmpty(urls)) {
                if (urls == null) {
                    urls = new ConcurrentHashSet<URL>();
                }
                URL empty = url.setProtocol(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(org.apache.dubbo.common.utils.ConcurrentHashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 2 with ConcurrentHashSet

use of org.apache.dubbo.common.utils.ConcurrentHashSet 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.computeIfAbsent(key, k -> new ConcurrentHashSet<>());
            urls.add(url);
            List<URL> list = toList(urls);
            for (final NotifyListener listener : entry.getValue()) {
                notify(key, listener, list);
                synchronized (listener) {
                    listener.notify();
                }
            }
        }
    }
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) ConcurrentHashSet(org.apache.dubbo.common.utils.ConcurrentHashSet) ConcurrentMap(java.util.concurrent.ConcurrentMap) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) URL(org.apache.dubbo.common.URL) NotifyListener(org.apache.dubbo.registry.NotifyListener)

Example 3 with ConcurrentHashSet

use of org.apache.dubbo.common.utils.ConcurrentHashSet in project dubbo by alibaba.

the class CallbackServiceCodec method referOrDestroyCallbackService.

/**
 * refer or destroy callback service on server side
 *
 * @param url
 */
@SuppressWarnings("unchecked")
private static Object referOrDestroyCallbackService(Channel channel, URL url, Class<?> clazz, Invocation inv, int instid, boolean isRefer) {
    Object proxy;
    String invokerCacheKey = getServerSideCallbackInvokerCacheKey(channel, clazz.getName(), instid);
    String proxyCacheKey = getServerSideCallbackServiceCacheKey(channel, clazz.getName(), instid);
    proxy = channel.getAttribute(proxyCacheKey);
    String countkey = getServerSideCountKey(channel, clazz.getName());
    if (isRefer) {
        if (proxy == null) {
            URL referurl = URL.valueOf("callback://" + url.getAddress() + "/" + clazz.getName() + "?" + INTERFACE_KEY + "=" + clazz.getName());
            referurl = referurl.addParametersIfAbsent(url.getParameters()).removeParameter(METHODS_KEY);
            if (!isInstancesOverLimit(channel, referurl, clazz.getName(), instid, true)) {
                ApplicationModel.getServiceRepository().registerService(clazz);
                @SuppressWarnings("rawtypes") Invoker<?> invoker = new ChannelWrappedInvoker(clazz, channel, referurl, String.valueOf(instid));
                proxy = PROXY_FACTORY.getProxy(new AsyncToSyncInvoker<>(invoker));
                channel.setAttribute(proxyCacheKey, proxy);
                channel.setAttribute(invokerCacheKey, invoker);
                increaseInstanceCount(channel, countkey);
                // convert error fail fast .
                // ignore concurrent problem.
                Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
                if (callbackInvokers == null) {
                    callbackInvokers = new ConcurrentHashSet<>(1);
                    channel.setAttribute(CHANNEL_CALLBACK_KEY, callbackInvokers);
                }
                callbackInvokers.add(invoker);
                logger.info("method " + inv.getMethodName() + " include a callback service :" + invoker.getUrl() + ", a proxy :" + invoker + " has been created.");
            }
        }
    } else {
        if (proxy != null) {
            Invoker<?> invoker = (Invoker<?>) channel.getAttribute(invokerCacheKey);
            try {
                Set<Invoker<?>> callbackInvokers = (Set<Invoker<?>>) channel.getAttribute(CHANNEL_CALLBACK_KEY);
                if (callbackInvokers != null) {
                    callbackInvokers.remove(invoker);
                }
                invoker.destroy();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            // cancel refer, directly remove from the map
            channel.removeAttribute(proxyCacheKey);
            channel.removeAttribute(invokerCacheKey);
            decreaseInstanceCount(channel, countkey);
        }
    }
    return proxy;
}
Also used : Set(java.util.Set) ConcurrentHashSet(org.apache.dubbo.common.utils.ConcurrentHashSet) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) Invoker(org.apache.dubbo.rpc.Invoker) AsyncToSyncInvoker(org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker) URL(org.apache.dubbo.common.URL) RemotingException(org.apache.dubbo.remoting.RemotingException) IOException(java.io.IOException)

Aggregations

Set (java.util.Set)3 URL (org.apache.dubbo.common.URL)3 ConcurrentHashSet (org.apache.dubbo.common.utils.ConcurrentHashSet)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ConcurrentMap (java.util.concurrent.ConcurrentMap)2 NotifyListener (org.apache.dubbo.registry.NotifyListener)2 IOException (java.io.IOException)1 RemotingException (org.apache.dubbo.remoting.RemotingException)1 Invoker (org.apache.dubbo.rpc.Invoker)1 AsyncToSyncInvoker (org.apache.dubbo.rpc.protocol.AsyncToSyncInvoker)1