Пример #1
0
        public void DeleteQueue(string channel, string queue)
        {
            if (String.IsNullOrEmpty(channel) || String.IsNullOrEmpty(queue))
            {
                Console.WriteLine("Params Must Not Be NULL");
            }
            ConnectChannel tempChannel = null;

            lock (listAccessLock)
            {
                if (channelList != null)
                {
                    tempChannel = channelList.Where(p => p != null && p.ChannelName == channel).FirstOrDefault();
                }
            }
            if (tempChannel != null)
            {
                lock (tempChannel.AccessLock)
                {
                    if (tempChannel.QueueNames != null && tempChannel.QueueNames.Contains(queue))
                    {
                        tempChannel.ConsumerChannel.QueueDelete(queue);
                        tempChannel.QueueNames.Remove(queue);
                    }
                    else
                    {
                        Console.WriteLine($"{queue} Not Exist");
                    }
                }
            }
            else
            {
                Console.WriteLine($"{channel} Not Exist");
            }
        }
Пример #2
0
        /// <summary>
        /// 为指定名称的channel创建Exchange
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="exchangeType"></param>
        /// <param name="exchangeName"></param>
        /// <returns></returns>
        public bool ConstructMqExchange(string channelName, ExchangeType exchangeType, string exchangeName)
        {
            if (string.IsNullOrEmpty(exchangeName))
            {
                LogHelper.LogError("Exchange Name Must Not Be NULL");
                return(false);
            }
            ConnectChannel channel = channelList.Find(n => n.ChannelName == channelName);

            if (channel == null)
            {
                LogHelper.LogError($"Channel {channelName} Not Exist.");
                return(false);
            }
            channel.ExchangeType = exchangeType;
            channel.ExchangeName = exchangeName;
            try
            {
                channel.ConsumerChannel.ExchangeDeclare(exchangeName, exchangeType.ToString(), true);
                return(true);
            }
            catch (Exception e)
            {
                LogHelper.LogError(e.Message);
                return(false);
            }
        }
Пример #3
0
        /// <summary>
        /// 创建基础channel
        /// </summary>
        /// <param name="channelName"></param>
        /// <param name="exchangeType"></param>
        /// <param name="exchangeName"></param>
        /// <param name="msgCallback"></param>
        /// <returns></returns>
        public bool ConstructMqChannel(string channelName, Func <RemoteMessage, BasicDeliverEventArgs, bool> msgCallback)
        {
            if (string.IsNullOrEmpty(channelName))
            {
                LogHelper.LogError("The channel Name Must Not Be NULL");
                return(false);
            }

            if (channelList.Find(n => n.ChannelName == channelName) != null)
            {
                LogHelper.LogError($"Channel {channelName} Aleady Exist.");
                return(false);
            }
            ConnectChannel connectChannel = new ConnectChannel();

            connectChannel.ChannelName            = channelName;
            connectChannel.ChannelGuid            = Guid.NewGuid().ToString();
            connectChannel.QueueNames             = new List <string>();
            connectChannel.ReceiveMessageCallback = msgCallback;
            connectChannel.AccessLock             = new object();
            connectChannel.ExchangeType           = ExchangeType.noexist;
            connectChannel.ConsumerChannel        = null;
            try
            {
                if (ConsumerConn != null)
                {
                    connectChannel.ConsumerChannel = consumerConn.CreateModel();
                    if (connectChannel.ConsumerChannel != null)
                    {
                        lock (listAccessLock)
                        {
                            channelList.Add(connectChannel);
                        }
                        return(true);
                    }
                    else
                    {
                        return(false);
                    }
                }
                else
                {
                    return(false);
                }
            }
            catch (Exception e)
            {
                LogHelper.LogError(e.Message);
                return(false);
            }
        }
Пример #4
0
        public bool ConstructMqQueue(string channelName, string queueName = null, string bindingKey = null)
        {
            if (string.IsNullOrEmpty(queueName))
            {
                LogHelper.LogError("QueueName Must Not Be NULL");
                return(false);
            }
            ConnectChannel channel = channelList.Find(n => n.ChannelName == channelName);

            if (channel == null)
            {
                LogHelper.LogError($"Channel {channelName} Not Exist.");
                return(false);
            }
            if (!string.IsNullOrEmpty(channel.ExchangeName))
            {
                if (string.IsNullOrEmpty(bindingKey))
                {
                    LogHelper.LogError($"The Channel has Exchange,So you must input BindingKey");
                    return(false);
                }
            }
            try
            {
                channel.ConsumerChannel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false);
                if (!string.IsNullOrEmpty(channel.ExchangeName))
                {
                    channel.ConsumerChannel.QueueBind(queueName, channel.ExchangeName, bindingKey);
                }
                lock (channel.AccessLock)
                {
                    channel.QueueNames.Add(queueName);
                }
                return(true);
            }
            catch (Exception e)
            {
                LogHelper.LogError(e.Message);
                return(false);
            }
        }
Пример #5
0
        /// <summary>
        /// 消息接收事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="args"></param>
        private void Consumer_MultiReceived(object sender, BasicDeliverEventArgs args)
        {
            string channelGuid = args.ConsumerTag;

            ConnectChannel tempChannel = null;

            lock (listAccessLock)
            {
                if (channelList != null)
                {
                    tempChannel = channelList.Where(p => p != null && p.ChannelGuid == channelGuid).FirstOrDefault();
                }
            }

            var body    = args.Body;
            var message = Encoding.UTF8.GetString(body.ToArray());

            if (tempChannel != null)
            {
                lock (tempChannel.AccessLock)
                {
                    if (tempChannel.ConsumerChannel != null)
                    {
                        bool result = tempChannel.ReceiveMessageCallback(JsonConvert.DeserializeObject <RemoteMessage>(message), args);
                        if (result)
                        {
                            if (tempChannel.ConsumerChannel != null && !tempChannel.ConsumerChannel.IsClosed)
                            {
                                tempChannel.ConsumerChannel.BasicAck(args.DeliveryTag, false);
                            }
                        }
                        else
                        {
                        }
                    }
                }
            }
        }
Пример #6
0
        /// <summary>
        /// 为指定消息队列创建生产者
        /// </summary>
        /// <param name="channelGuid"></param>
        public bool Consume(string channelName, string queueName)
        {
            ConnectChannel temp = null;

            lock (listAccessLock)
            {
                if (channelList != null)
                {
                    temp = channelList.Where(p => p != null && p.ChannelName == channelName).FirstOrDefault();
                }
            }
            if (temp == null)
            {
                LogHelper.LogError($"Channel {channelName} Not Exist");
                return(false);
            }
            if (!temp.QueueNames.Contains(queueName))
            {
                LogHelper.LogError($"Queue {queueName} Not Exist");
                return(false);
            }
            try
            {
                lock (temp.AccessLock)
                {
                    var consumer = new EventingBasicConsumer(temp.ConsumerChannel);
                    consumer.Received += Consumer_MultiReceived;
                    temp.ConsumerChannel.BasicConsume(queueName, false, temp.ChannelGuid, consumer);
                }
                return(true);
            }
            catch (Exception e)
            {
                LogHelper.LogError(e.Message);
                return(false);
            }
        }