Example #1
0
        protected override void DoSubscribe(string groupPath, INotifyListener listener)
        {
            _zkClient.EnsurePathRecursive(groupPath, false);

            ConcurrentDictionary <INotifyListener, ChildListener> listeners;

            if (_zkListeners.TryGetValue(groupPath, out listeners) == false)
            {
                listeners = _zkListeners.GetOrAdd(groupPath, new ConcurrentDictionary <INotifyListener, ChildListener>());
            }

            ChildListener childListener;

            if (listeners.TryGetValue(listener, out childListener) == false)
            {
                childListener = new ChildListener(_zkClient, groupPath);
                childListener.OnMetadataChanged += (metas) => this.Notify(groupPath, listener, metas);
                listeners.TryAdd(listener, childListener);
            }

            var children  = _zkClient.SubscribeChildListener(childListener);
            var metadatas = children.Select(child => _zkClient.GetData(groupPath + "/" + child, false, null).ToMetadata()).ToList();

            this.Notify(groupPath, listener, metadatas);
        }
Example #2
0
 public bool RemoveListener(int notifyType, INotifyListener listener)
 {
     if (!allListeners.ContainsKey(notifyType))
     {
         return(false);
     }
     return(allListeners[notifyType].Remove(listener));
 }
Example #3
0
 public bool AddListener(int notifyType, INotifyListener listener)
 {
     if (!allListeners.ContainsKey(notifyType))
     {
         allListeners[notifyType] = new List <INotifyListener>();
     }
     allListeners[notifyType].Add(listener);
     return(true);
 }
Example #4
0
 protected override void Notify(string groupPath, INotifyListener listener, List <ServiceMetadata> metadatas)
 {
     try
     {
         base.Notify(groupPath, listener, metadatas);
     }
     catch
     {
         _failedNotified.GetOrAdd(groupPath, new ConcurrentDictionary <INotifyListener, List <ServiceMetadata> >())
         .AddOrReplace(listener, metadatas);
     }
 }
Example #5
0
        public static void RemoveListener(string eventName, INotifyListener listener)
        {
            if (Instance == null)
            {
                return;
            }
            List <INotifyListener> thisEvent = null;

            if (Instance._eventDictionary.TryGetValue(eventName, out thisEvent))
            {
                thisEvent.Remove(listener);
            }
        }
Example #6
0
        protected override void DoUnsubscribe(string groupPath, INotifyListener listener)
        {
            ConcurrentDictionary <INotifyListener, ChildListener> listeners;

            if (_zkListeners.TryGetValue(groupPath, out listeners))
            {
                ChildListener childListener;
                if (listeners.TryGetValue(listener, out childListener))
                {
                    _zkClient.UnsubscribeChildListener(childListener);
                    listeners.TryRemove(listener);
                }
            }
        }
Example #7
0
        public virtual void Unsubscribe(string groupPath, INotifyListener listener, bool checkOnStart)
        {
            if (string.IsNullOrEmpty(groupPath) || listener == null)
            {
                return;
            }

            List <INotifyListener> listeners;

            if (_subscribed.TryGetValue(groupPath, out listeners))
            {
                listeners.Remove(listener);
            }
        }
Example #8
0
        public virtual void Subscribe(string groupPath, INotifyListener listener, bool checkOnStart)
        {
            if (listener == null)
            {
                throw new ArgumentNullException("listener", "notify listener == null");
            }

            if (string.IsNullOrEmpty(groupPath))
            {
                throw new ArgumentNullException("groupPath", "groupPath is null or empty");
            }

            _subscribed.GetOrAdd(groupPath, new List <INotifyListener>()).Add(listener);
        }
Example #9
0
        public static void AddListener(string eventName, INotifyListener listener)
        {
            List <INotifyListener> listenerList = null;

            if (Instance._eventDictionary.TryGetValue(eventName, out listenerList))
            {
                listenerList.Add(listener);
            }
            else
            {
                listenerList = new List <INotifyListener>();
                listenerList.Add(listener);
                Instance._eventDictionary.Add(eventName, listenerList);
            }
        }
Example #10
0
        public void subscribe(ServiceMeta serviceMeta, INotifyListener listener)
        {
            BlockingCollection <INotifyListener> listeners;

            subscribeListeners.TryGetValue(serviceMeta, out listeners);

            if (listeners == null)
            {
                BlockingCollection <INotifyListener> newListeners = new BlockingCollection <INotifyListener>();
                subscribeListeners.TryAdd(serviceMeta, newListeners);
                if (listeners == null)
                {
                    listeners = newListeners;
                }
            }
            listeners.Add(listener);

            doSubscribe(serviceMeta);
        }
Example #11
0
        public override void Subscribe(string groupPath, INotifyListener listener, bool checkOnStart)
        {
            base.Subscribe(groupPath, listener, checkOnStart);
            var emptyListeners = new List <INotifyListener>();

            _failedSubscribed.GetOrAdd(groupPath, emptyListeners).Remove(listener);
            _failedUnsubscribed.GetOrAdd(groupPath, emptyListeners).Remove(listener);

            try
            {
                this.DoSubscribe(groupPath, listener);
            }
            catch
            {
                // 如果开启了启动时检测,直接抛出异常
                if (checkOnStart)
                {
                    throw;
                }
                // 将失败的订阅请求记录到失败列表,定时重试
                _failedSubscribed.GetOrAdd(groupPath, emptyListeners).Add(listener);
            }
        }
Example #12
0
 /// <summary>
 /// Initializes a new instance of <see cref="Notify{T, EP}"/>
 /// </summary>
 /// <param name="propertyName">Name of property</param>
 /// <param name="listener">Listener</param>
 public Notify(string propertyName, INotifyListener listener)
 {
     _propertyName = propertyName;
     _listener     = listener;
 }
Example #13
0
 protected virtual void Notify(string groupPath, INotifyListener listener, List <ServiceMetadata> metadatas)
 {
     listener.Notify(metadatas);
 }
Example #14
0
 protected abstract void DoUnsubscribe(string groupPath, INotifyListener listener);