Пример #1
0
        /// <summary>
        /// 接收MQ消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="func"></param>
        /// <param name="queueName"></param>
        public static void GetMQ <T>(Func <T, bool> func, string queueName)
        {
            using (var channel = ConnectionMQ.Connection().CreateModel())
            {
                //事件基本消费者
                EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

                //接收到消息事件
                consumer.Received += (ch, ea) =>
                {
                    var message = Encoding.UTF8.GetString(ea.Body);
                    try
                    {
                        var item = JsonConvert.DeserializeObject <T>(message);
                        func(item);
                    }
                    catch (Exception ex)
                    {
                        LogHelp.Error(ex);
                    }
                    //确认该消息已被消费
                    channel.BasicAck(ea.DeliveryTag, false);
                };
                //启动消费者 设置为手动应答消息
                channel.BasicConsume(queueName, false, consumer);
            }
        }
Пример #2
0
        /// <summary>
        /// 发送MQ消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="item"></param>
        /// <param name="queueName"></param>
        public static void SendMQ <T>(T item, string queueName)
        {
            string input = Newtonsoft.Json.JsonConvert.SerializeObject(item);

            using (var channel = ConnectionMQ.Connection().CreateModel())
            {
                //声明一个队列
                channel.QueueDeclare(
                    queue: queueName,
                    durable: true,
                    exclusive: false,
                    autoDelete: false,
                    arguments: null);


                var sendBytes  = Encoding.UTF8.GetBytes(input);
                var properties = new BasicProperties();
                properties.DeliveryMode = 2;
                //发布消息
                channel.BasicPublish(
                    exchange: "",
                    routingKey: queueName,
                    mandatory: true,
                    basicProperties: properties,
                    body: sendBytes);
            }
        }
Пример #3
0
        /// <summary>
        /// 接收MQ消息
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="func"></param>
        /// <param name="queueName"></param>
        public static void GetMQ <T>(Func <T, bool> func, string queueName)
        {
            var channel = ConnectionMQ.Connection().CreateModel();

            //事件基本消费者
            EventingBasicConsumer consumer = new EventingBasicConsumer(channel);

            //接收到消息事件
            consumer.Received += (ch, ea) =>
            {
                var message = Encoding.UTF8.GetString(ea.Body);
                var result  = false;
                try
                {
                    var item = JsonConvert.DeserializeObject <T>(message);
                    result = func(item);
                }
                catch (Exception ex)
                {
                    LogHelper.Error(ex);
                }
                //确认该消息已被消费
                if (result)
                {
                    channel.BasicAck(ea.DeliveryTag, false);
                }
            };
            //启动消费者 设置为手动应答消息
            channel.BasicConsume(queue: queueName, autoAck: false, consumer: consumer);



            //channel.QueueDeclare(queue: queueName,
            //                 durable: true,
            //                 exclusive: false,
            //                 autoDelete: false,
            //                 arguments: null);

            //    channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
            //    var consumer = new EventingBasicConsumer(channel);
            //    consumer.Received += (model, ea) =>
            //    {
            //        var body = ea.Body;
            //        var message = Encoding.UTF8.GetString(body);
            //        log?.Invoke(message);

            //        var item = JsonConvert.DeserializeObject<T>(message);
            //        var result = func(item);
            //        if (result)
            //            channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
            //    };
            //    channel.BasicConsume(queue: queueName,
            //                         autoAck: false,
            //                         consumer: consumer);
            //}
        }