Beispiel #1
0
        /// <summary>
        /// 解析MQ配置字符串
        /// </summary>
        /// <param name="serverConfig"></param>
        protected void GetServerConfigItems(string serverConfig)
        {
            if (rabbitMqServerItems == null)
            {
                rabbitMqServerItems = new List <RabbitMqServerItem>();
            }
            string[] configItems = serverConfig.Split(new string[] { ";" }, StringSplitOptions.RemoveEmptyEntries);
            foreach (var config in configItems)
            {
                if (!string.IsNullOrEmpty(config))
                {
                    string key    = config.GetHashCode().ToString();
                    var    item   = config.Split('@');
                    var    cgItem = new RabbitMqServerItem()
                    {
                        HostName = item[0],
                        UserName = item[1],
                        Password = item[2],
                    };

                    //if (item.Length > 3)
                    //{
                    //    cgItem.AutomaticRecoveryEnabled = item[3];
                    //    cgItem.NetworkRecoveryInterval = item[4];
                    //    cgItem.HeartBeat = item[5];
                    //}
                    rabbitMqServerItems.Add(cgItem);
                }
            }
        }
        private static void Open(RabbitMqServerItem config)
        {
            if (_conn != null)
            {
                return;
            }
            lock (LockObj)
            {
                var factory = new ConnectionFactory
                {
                    //设置主机名
                    HostName = config.HostName,

                    //设置心跳时间
                    RequestedHeartbeat = config.HeartBeat,

                    //设置自动重连
                    AutomaticRecoveryEnabled = config.AutomaticRecoveryEnabled,

                    //重连时间
                    NetworkRecoveryInterval = config.NetworkRecoveryInterval,

                    //用户名
                    UserName = config.UserName,

                    //密码
                    Password = config.Password
                };
                factory.AutomaticRecoveryEnabled = true;
                factory.NetworkRecoveryInterval  = new TimeSpan(1000);
                _conn = _conn ?? factory.CreateConnection();
            }
        }
 internal RabbitMqService(RabbitMqServerItem config)
 {
     Open(config);
 }