Ejemplo n.º 1
0
        /// <summary>
        /// 获取channel
        /// </summary>
        /// <param name="url"></param>
        /// <returns></returns>
        public ChannelWrapper GetChannel()
        {
            GetConnlockObj.EnterUpgradeableReadLock();
            ChannelWrapper channel = null;

            try
            {
                foreach (var item in connectionList)
                {
                    channel = item.GetChannelFromPool();
                    if (channel != null)
                    {
                        return(channel);
                    }
                }


                #region 创建新连接

                ConnectionFactory cf = new ConnectionFactory()
                {
                    HostName = this._hostName
                };
                Connection newConn        = null;
                var        connectSuccess = true;
                var        reconnectCount = 0;
                do
                {
                    try
                    {
                        newConn        = (Connection)cf.CreateConnection();
                        connectSuccess = true;
                    }
                    catch (BrokerUnreachableException)
                    {
                        connectSuccess = false;
                        reconnectCount++;
                        Thread.Sleep(ReconnectWaitTime);
                    }
                }while (!connectSuccess && reconnectCount <= MaxReconnectCount);

                if (newConn == null)
                {
                    throw new BrokerUnreachableException(new Exception("Broker Unreachable"));
                }

                GetConnlockObj.EnterWriteLock();
                ConnectionWrapper connWrapper = new ConnectionWrapper(newConn);
                connectionList.Add(connWrapper);
                GetConnlockObj.ExitWriteLock();

                return(connWrapper.GetChannelFromPool());

                #endregion
            }
            finally
            {
                GetConnlockObj.ExitUpgradeableReadLock();
            }
        }
Ejemplo n.º 2
0
        public ChannelWrapper(IModel channel, ConnectionWrapper connection)
        {
            if (channel == null)
            {
                throw new ArgumentNullException("channel");
            }
            if (channel == connection)
            {
                throw new ArgumentNullException("connection");
            }

            this.Channel    = channel;
            this.Connection = connection;
            this.Reusable   = true;
            this.SetBusy();
        }
Ejemplo n.º 3
0
        public ChannelPool(ConnectionWrapper connection)
        {
            if (connection == null)
            {
                throw new ArgumentNullException("connection");
            }

            this.Connection  = connection;
            this.ChannelList = new List <ChannelWrapper>();

            //后台清理线程,定期清理整个应用域中每一个Channel中的每一个Channel项
            ScanTimer           = new System.Timers.Timer(ScanInterval);
            ScanTimer.AutoReset = true;
            ScanTimer.Enabled   = true;
            ScanTimer.Elapsed  += new System.Timers.ElapsedEventHandler(DoScan);
        }