Example #1
0
        /// <summary>
        /// 拉取指定队列的消息
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <returns></returns>
        public static void Pull(string queueName, Action <BasicGetResult> action)
        {
            if (string.IsNullOrEmpty(queueName))
            {
                throw new ArgumentNullException("queueName");
            }

            using (var channel = RabbitmqContext.GetModel())
            {
                BasicGetResult result = channel.BasicGet(queueName, false);
                if (result != null)
                {
                    channel.BasicAck(result.DeliveryTag, false);
                }
                action(result);
            }
        }
Example #2
0
        /// <summary>
        /// 拉取指定队列的消息
        /// </summary>
        /// <param name="queueName">队列名称</param>
        /// <returns></returns>
        public static ResponseResult Pull(string queueName, Func <BasicGetResult, ResponseResult> func)
        {
            if (string.IsNullOrEmpty(queueName))
            {
                throw new ArgumentNullException("queueName");
            }

            using (var channel = RabbitmqContext.GetModel())
            {
                BasicGetResult result = channel.BasicGet(queueName, false);
                if (result != null)
                {
                    channel.BasicAck(result.DeliveryTag, false);
                }
                return(func(result));
            }
        }
Example #3
0
        /// <summary>
        /// 客户端订阅
        /// </summary>
        /// <param name="exchangeName">交换机名称</param>
        /// <param name="queueName">队列名称</param>
        /// <returns></returns>
        public static void Subscribe(string exchangeName, Action <ConsumerContext> action)
        {
            if (string.IsNullOrEmpty(exchangeName))
            {
                throw new ArgumentNullException("exchangeName");
            }

            using (var channel = RabbitmqContext.GetModel())
            {
                channel.ExchangeDeclare(exchangeName, "fanout");
                var queueName = channel.QueueDeclare().QueueName;
                channel.QueueBind(queue: queueName, exchange: exchangeName, routingKey: "");

                var consumer = new EventingBasicConsumer(channel);
                consumer.Received += (sender, args) =>
                {
                    ConsumerContext context = args.GetConsumerContext();
                    action(context);
                };
                channel.BasicConsume(queueName, true, consumer);
            }
        }