Example #1
0
 public MQManager(MqConfig config)
 {
     Open(config);
     if (_connection != null)
     {
         this._connected = true;
     }
 }
Example #2
0
        private static void Open(MqConfig config)
        {
            try
            {
                if (_connection != null)
                {
                    return;
                }
                lock (LockObj)
                {
                    int mqport = 5672;
                    if (!string.IsNullOrEmpty(config.Port))
                    {
                        int.TryParse(config.Port, out mqport);
                    }
                    var factory = new ConnectionFactory
                    {
                        //设置主机名
                        HostName = config.Host,

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

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

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

                        //用户名
                        UserName = config.UserName,

                        //密码
                        Password = config.Password,
                        //端口
                        Port = mqport
                    };
                    factory.AutomaticRecoveryEnabled = true;
                    factory.NetworkRecoveryInterval  = new TimeSpan(1000);
                    _connection = _connection ?? factory.CreateConnection();
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(string.Format("RabbitMQ连接初始化({0})出错!{1}", config.Host, ex.Message));
            }
        }