示例#1
0
        /// <summary>
        /// 获取插入Channel
        /// </summary>
        /// <param name="channelName"></param>
        /// <returns></returns>
        public static IModel GetSaveChannel(string channelName)
        {
            lock (SaveObj)
            {
                //队列管道名称为空时使用默认管道
                if (string.IsNullOrEmpty(channelName))
                {
                    channelName = Config.DefaultQueueName;
                }
                if (SaveChannels == null)
                {
                    SaveChannels = new Dictionary <string, IModel>();
                }
                if (SaveChannels.Keys.Contains(channelName))
                {
                    return(SaveChannels[channelName]);
                }

                var connection = RabbitMqConnectionManage.CreatConnection();
                if (connection == null)
                {
                    throw new Exception("请先创建队列的连接!");
                }

                var channel = connection.CreateModel();
                //声明交换机
                channel.ExchangeDeclare(channelName, "direct", true, false, null);
                //声明队列
                channel.QueueDeclare(channelName, true, false, false, null);
                //声明队列绑定
                channel.QueueBind(channelName, channelName, $"Log/{channelName}", null);
                SaveChannels.Add(channelName, channel);
                return(channel);
            }
        }
示例#2
0
 public static bool Start()
 {
     try
     {
         //创建连接
         RabbitMqConnectionManage.CreatConnection();
         return(true);
     }
     catch (Exception ex)
     {
         Logger.Error($"开启队列服务失败:{ex}");
         return(false);
     }
 }
示例#3
0
 public static void Stop()
 {
     RabbitMqChannelManage.Close();
     RabbitMqConnectionManage.Close();
 }