Search in sources :

Example 1 with ConcurrentHashSet

use of com.alibaba.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.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 2 with ConcurrentHashSet

use of com.alibaba.dubbo.common.utils.ConcurrentHashSet in project dubbo by alibaba.

the class CallbackServiceCodec method referOrdestroyCallbackService.

/**
     * server端 应用一个callbackservice
     * @param url 
     */
@SuppressWarnings("unchecked")
private static Object referOrdestroyCallbackService(Channel channel, URL url, Class<?> clazz, Invocation inv, int instid, boolean isRefer) {
    Object proxy = null;
    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() + "?" + Constants.INTERFACE_KEY + "=" + clazz.getName());
            referurl = referurl.addParametersIfAbsent(url.getParameters()).removeParameter(Constants.METHODS_KEY);
            if (!isInstancesOverLimit(channel, referurl, clazz.getName(), instid, true)) {
                @SuppressWarnings("rawtypes") Invoker<?> invoker = new ChannelWrappedInvoker(clazz, channel, referurl, String.valueOf(instid));
                proxy = proxyFactory.getProxy(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(Constants.CHANNEL_CALLBACK_KEY);
                if (callbackInvokers == null) {
                    callbackInvokers = new ConcurrentHashSet<Invoker<?>>(1);
                    callbackInvokers.add(invoker);
                    channel.setAttribute(Constants.CHANNEL_CALLBACK_KEY, callbackInvokers);
                }
                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(Constants.CHANNEL_CALLBACK_KEY);
                if (callbackInvokers != null) {
                    callbackInvokers.remove(invoker);
                }
                invoker.destroy();
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            //取消refer 直接在map中去除,
            channel.removeAttribute(proxyCacheKey);
            channel.removeAttribute(invokerCacheKey);
            decreaseInstanceCount(channel, countkey);
        }
    }
    return proxy;
}
Also used : Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) Invoker(com.alibaba.dubbo.rpc.Invoker) URL(com.alibaba.dubbo.common.URL) IOException(java.io.IOException) RemotingException(com.alibaba.dubbo.remoting.RemotingException)

Example 3 with ConcurrentHashSet

use of com.alibaba.dubbo.common.utils.ConcurrentHashSet in project dubbo by alibaba.

the class RegistryContainer method start.

public void start() {
    String url = ConfigUtils.getProperty(REGISTRY_ADDRESS);
    if (url == null || url.length() == 0) {
        throw new IllegalArgumentException("Please set java start argument: -D" + REGISTRY_ADDRESS + "=zookeeper://127.0.0.1:2181");
    }
    registry = (RegistryService) SpringContainer.getContext().getBean("registryService");
    URL subscribeUrl = new URL(Constants.ADMIN_PROTOCOL, NetUtils.getLocalHost(), 0, "", Constants.INTERFACE_KEY, Constants.ANY_VALUE, Constants.GROUP_KEY, Constants.ANY_VALUE, Constants.VERSION_KEY, Constants.ANY_VALUE, Constants.CLASSIFIER_KEY, Constants.ANY_VALUE, Constants.CATEGORY_KEY, Constants.PROVIDERS_CATEGORY + "," + Constants.CONSUMERS_CATEGORY, Constants.CHECK_KEY, String.valueOf(false));
    registry.subscribe(subscribeUrl, new NotifyListener() {

        public void notify(List<URL> urls) {
            if (urls == null || urls.size() == 0) {
                return;
            }
            Map<String, List<URL>> proivderMap = new HashMap<String, List<URL>>();
            Map<String, List<URL>> consumerMap = new HashMap<String, List<URL>>();
            for (URL url : urls) {
                String application = url.getParameter(Constants.APPLICATION_KEY);
                if (application != null && application.length() > 0) {
                    applications.add(application);
                }
                String service = url.getServiceInterface();
                services.add(service);
                String category = url.getParameter(Constants.CATEGORY_KEY, Constants.DEFAULT_CATEGORY);
                if (Constants.PROVIDERS_CATEGORY.equals(category)) {
                    if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                        serviceProviders.remove(service);
                    } else {
                        List<URL> list = proivderMap.get(service);
                        if (list == null) {
                            list = new ArrayList<URL>();
                            proivderMap.put(service, list);
                        }
                        list.add(url);
                        if (application != null && application.length() > 0) {
                            Set<String> serviceApplications = providerServiceApplications.get(service);
                            if (serviceApplications == null) {
                                providerServiceApplications.put(service, new ConcurrentHashSet<String>());
                                serviceApplications = providerServiceApplications.get(service);
                            }
                            serviceApplications.add(application);
                            Set<String> applicationServices = providerApplicationServices.get(application);
                            if (applicationServices == null) {
                                providerApplicationServices.put(application, new ConcurrentHashSet<String>());
                                applicationServices = providerApplicationServices.get(application);
                            }
                            applicationServices.add(service);
                        }
                    }
                } else if (Constants.CONSUMERS_CATEGORY.equals(category)) {
                    if (Constants.EMPTY_PROTOCOL.equals(url.getProtocol())) {
                        serviceConsumers.remove(service);
                    } else {
                        List<URL> list = consumerMap.get(service);
                        if (list == null) {
                            list = new ArrayList<URL>();
                            consumerMap.put(service, list);
                        }
                        list.add(url);
                        if (application != null && application.length() > 0) {
                            Set<String> serviceApplications = consumerServiceApplications.get(service);
                            if (serviceApplications == null) {
                                consumerServiceApplications.put(service, new ConcurrentHashSet<String>());
                                serviceApplications = consumerServiceApplications.get(service);
                            }
                            serviceApplications.add(application);
                            Set<String> applicationServices = consumerApplicationServices.get(application);
                            if (applicationServices == null) {
                                consumerApplicationServices.put(application, new ConcurrentHashSet<String>());
                                applicationServices = consumerApplicationServices.get(application);
                            }
                            applicationServices.add(service);
                        }
                    }
                }
            }
            if (proivderMap != null && proivderMap.size() > 0) {
                serviceProviders.putAll(proivderMap);
            }
            if (consumerMap != null && consumerMap.size() > 0) {
                serviceConsumers.putAll(consumerMap);
            }
        }
    });
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ArrayList(java.util.ArrayList) URL(com.alibaba.dubbo.common.URL) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) ArrayList(java.util.ArrayList) List(java.util.List) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Map(java.util.Map) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Example 4 with ConcurrentHashSet

use of com.alibaba.dubbo.common.utils.ConcurrentHashSet in project dubbo by alibaba.

the class SimpleRegistryService method subscribe.

public void subscribe(URL url, NotifyListener listener) {
    if (getUrl().getPort() == 0) {
        URL registryUrl = RpcContext.getContext().getUrl();
        if (registryUrl != null && registryUrl.getPort() > 0 && RegistryService.class.getName().equals(registryUrl.getPath())) {
            super.setUrl(registryUrl);
            super.register(registryUrl);
        }
    }
    String client = RpcContext.getContext().getRemoteAddressString();
    ConcurrentMap<URL, Set<NotifyListener>> clientListeners = remoteSubscribed.get(client);
    if (clientListeners == null) {
        remoteSubscribed.putIfAbsent(client, new ConcurrentHashMap<URL, Set<NotifyListener>>());
        clientListeners = remoteSubscribed.get(client);
    }
    Set<NotifyListener> listeners = clientListeners.get(url);
    if (listeners == null) {
        clientListeners.putIfAbsent(url, new ConcurrentHashSet<NotifyListener>());
        listeners = clientListeners.get(url);
    }
    listeners.add(listener);
    super.subscribe(url, listener);
    subscribed(url, listener);
}
Also used : Set(java.util.Set) ConcurrentHashSet(com.alibaba.dubbo.common.utils.ConcurrentHashSet) URL(com.alibaba.dubbo.common.URL) NotifyListener(com.alibaba.dubbo.registry.NotifyListener)

Aggregations

URL (com.alibaba.dubbo.common.URL)4 ConcurrentHashSet (com.alibaba.dubbo.common.utils.ConcurrentHashSet)4 Set (java.util.Set)4 NotifyListener (com.alibaba.dubbo.registry.NotifyListener)3 HashSet (java.util.HashSet)2 Map (java.util.Map)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 RemotingException (com.alibaba.dubbo.remoting.RemotingException)1 Invoker (com.alibaba.dubbo.rpc.Invoker)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 ConcurrentMap (java.util.concurrent.ConcurrentMap)1