Exemple #1
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="queueName"></param>
        /// <param name="delegateHandle"></param>
        public void GetQueueMessage(string queueName, ReceiveActionDelegate delegateHandle)
        {
            using (var connection = _factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    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) =>
                    {
                        channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                        var    body = ea.Body;
                        object objval;
                        objval = CommonHelper.BytesToObject(body);
                        delegateHandle(objval);
                        WriteLogHelper.WriteLogDoc("GetQueueLog", "队列" + queueName + "获取数据成功", "rabbitMqLog");
                    };
                    channel.BasicConsume(queue: queueName,
                                         autoAck: false,
                                         consumer: consumer);
                }
        }
Exemple #2
0
        /// <summary>
        /// 接收消息
        /// </summary>
        /// <param name="exchangeName"></param>
        /// <param name="queueName"></param>
        /// <param name="rootingkey"></param>
        /// <param name="delegateHandle"></param>
        public void GetQueueMessage(string exchangeName, string queueName, string rootingkey, ReceiveActionDelegate delegateHandle)
        {
            using (var connection = _factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare(exchange: exchangeName,
                                            type: "direct");
                    channel.QueueDeclare(queue: queueName,
                                         durable: true,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);
                    ///绑定交换机队列
                    channel.QueueBind(queue: queueName,
                                      exchange: exchangeName,
                                      routingKey: rootingkey);

                    var consumer = new EventingBasicConsumer(channel);
                    //声明方法
                    consumer.Received += (model, ea) =>
                    {
                        channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                        var    body = ea.Body;
                        object objval;
                        objval = CommonHelper.BytesToObject(body);
                        delegateHandle(objval);
                        WriteLogHelper.WriteLogDoc("GetExchangeQueueLog", "交换机:" + exchangeName + "绑定队列:" + queueName + "获取数据成功", "rabbitMqLog");
                    };
                    channel.BasicConsume(queue: queueName,
                                         autoAck: false,
                                         consumer: consumer);
                }
        }