예제 #1
0
        /// <summary>
        ///  接收消息到队列中
        /// </summary>
        /// <param name="model">消息通道</param>
        /// <param name="queue">队列名称</param>
        /// <param name="callback">订阅消息的回调事件</param>
        /// <returns></returns>
        private EventingBasicConsumer CreateConsumer(IModel model, string queue, ref MqConsumerChannel mqConsumerChannel)
        {
            EventingBasicConsumer consumer = new EventingBasicConsumer(model);

            consumer.Received         += mqConsumerChannel.Receive;
            mqConsumerChannel.Consumer = consumer;
            model.BasicConsume(queue: queue, autoAck: MqGlobleConfig.AutoAck, consumer: consumer);
            return(consumer);
        }
예제 #2
0
        /// <summary>
        ///  创建消费的消息通道
        /// </summary>
        public MqConsumerChannel CreateReceiveChannel(string exchangeType, string exchange, string queue, string routekey, Func <MessageBody, Task> onReceivedCallback)
        {
            var channel = CreateConsumerChannel(exchangeType, exchange, queue, routekey);
            MqConsumerChannel myChannel = new MqConsumerChannel(exchangeType, exchange, queue, routekey)
            {
                Connection         = _mqConnection.Connection,
                OnReceivedCallback = onReceivedCallback,
            };

            CreateConsumer(channel, queue, ref myChannel);
            return(myChannel);
        }
예제 #3
0
        //public MqConsumerChannel CreateChannel(string queue, string routeKey, string exchangeType)
        //{
        //    MqConnection conn = new MqConnection(Config, vHost);
        //    MqChannelManager cm = new MqChannelManager(conn);
        //    MqConsumerChannel channel = cm.CreateReceiveChannel(exchangeType, ExchangeName, queue, routeKey);
        //    return channel;
        //}

        /// <summary>
        ///  启动订阅
        /// </summary>
        public void Start()
        {
            if (!_started)
            {
                if (!Queues.Exists(x => x.QueueName == QueueName))
                {
                    Queues.Add(new QueueInfo(QueueName, OnReceived));
                }

                var manager = new MqChannelManager(Config);
                foreach (var item in Queues)
                {
                    MqConsumerChannel channel = manager.CreateReceiveChannel(item.ExchangeType, this.ExchangeName, item.QueueName, item.RouterKey, item.OnReceived);
                    this.Channels.Add(channel);
                }
                _started = true;
                Log.Information("mq服务开始");
            }
        }