示例#1
0
        public bool  TryGet(out RabbitConnectionFactory factory)
        {
            rwLock.EnterReadLock();
            RabbitConnectionFactory tempFactory = null;

            try
            {
                while (!_queue.IsEmpty)
                {
                    if (_queue.TryDequeue(out tempFactory))
                    {
                        if (tempFactory.Connection != null)
                        {
                            _queue.Enqueue(tempFactory);

                            factory = tempFactory;

                            return(true);
                        }
                    }
                }
            }
            finally
            {
                rwLock.ExitReadLock();
            }
            factory = tempFactory;
            return(false);
        }
示例#2
0
        public void Add(IConnectOptions key, RabbitConnectionFactory factory)
        {
            rwLock.EnterWriteLock();

            IConnectOptions connectOptions = null;

            try
            {
                if (_keys.TryGetValue(key.TagName, out connectOptions))
                {
                    //但是属性已发生变更
                    if (connectOptions.CompareTo(key) == 0)
                    {
                        RabbitConnectionFactory oldFactory = null;

                        _keys.Remove(key.TagName);

                        _queue.TryDequeue(out oldFactory);

                        //重新入队
                        _keys.Add(key.TagName, key);
                        _queue.Enqueue(factory);
                    }
                }
                else
                {
                    _keys.Add(key.TagName, key);
                    _queue.Enqueue(factory);
                }
            }
            finally {
                rwLock.ExitWriteLock();
            }
        }