Пример #1
0
        public static bool CreateConnection(string key, MQConfig config, bool conn = true)
        {
            var factory = new ConnectionFactory()
            {
                HostName    = config.HostUrl,
                Port        = config.Port > 0 ? config.Port : 5672,
                UserName    = config.UserName,
                Password    = config.Password,
                VirtualHost = config.VirtualHost,
                AutomaticRecoveryEnabled = true,
                //NetworkRecoveryInterval=TimeSpan.FromMilliseconds(1000)
            };

            try
            {
                if (mapConnection.ContainsKey(key) == false)
                {
                    mapConnection.Add(key, null);
                    mapChannel.Add(key, null);
                }
                if (conn == true)
                {
                    IConnection _connection = factory.CreateConnection();//创建连接
                    mapConnection[key] = _connection;
                }
                mapChannel[key] = mapConnection[key].CreateModel();//创建通道
                return(true);
            }
            catch (Exception ex)
            {
                return(false);
            }
        }
Пример #2
0
        public static void AddMQConfigMap(MQConfig config)
        {
            string key = config.HostUrl + "&" + config.Port + "&" + config.VirtualHost;

            if (mapMQConfig.ContainsKey(key) == false)
            {
                mapMQConfig.Add(key, config);
            }
        }
Пример #3
0
        public static void CreateConnection(MQConfig config, bool conn = true)
        {
            string key = config.HostUrl + "&" + config.Port + "&" + config.VirtualHost;

            if (mapMQConfig.ContainsKey(key) == false)
            {
                mapMQConfig.Add(key, null);
            }
            mapMQConfig[key] = config;
            CreateConnection(key, config, conn);
        }
Пример #4
0
 public static bool BuildMQ(MQConfig config, MQInfo mq)
 {
     try
     {
         string key = config.HostUrl + "&" + config.Port + "&" + config.VirtualHost;
         mapChannel[key].ExchangeDeclare(mq.ExchangeName, mq.ExchangeType);     //声明交换机
         mapChannel[key].QueueDeclare(mq.QueueName, false, false, false, null); //声明一个队列
         mapChannel[key].QueueBind(mq.QueueName, mq.ExchangeName, mq.RouteKey);
         return(true);
     }
     catch (Exception ex)
     {
         return(false);
     }
 }