Exemple #1
0
        public async Task SubscribeAsync(List <string> subscribeNames)
        {
            subscribeNames.CheckNull();
            //创建一个信道
            channel = await narutoChannelFactory.GetAsync();

            //绑定存储的消息队列
            channel.QueueDeclare(queue: RabbitMQOption.QueueName, durable: true, exclusive: false, autoDelete: false, arguments: null);
            //设置绑定关系
            foreach (var subscribeName in subscribeNames)
            {
                logger.LogInformation("开始订阅[{subscribeName}]信息", subscribeName);
                //将队列和交换机还有路由key绑定关系
                channel.QueueBind(queue: RabbitMQOption.QueueName, exchange: RabbitMQOption.ExchangeName, routingKey: subscribeName);

                logger.LogInformation("订阅完成[{subscribeName}]", subscribeName);
            }
            // 构造消费者实例
            var consumer = new EventingBasicConsumer(channel);

            // 绑定消息接收后的事件委托
            consumer.Received += ReceivedMessage;
            //消费取消
            consumer.ConsumerCancelled += ConsumerCancelledEvent;
            //服务关闭的时候
            consumer.Shutdown += ShutdownEvent;
            // 启动消费者
            channel.BasicConsume(queue: RabbitMQOption.QueueName, autoAck: false, consumer: consumer);
        }
Exemple #2
0
        public async Task PublishAsync(string subscribeName, object msg = null)
        {
            //创建一个信道
            using var channel = await narutoChannel.GetAsync();

            logger.LogInformation("Publish:开始发布消息,subscribeName={subscribeName},msg={msg}", subscribeName, msg);
            channel.PublishMessage(msg, subscribeName);
            logger.LogInformation("Publish:发布完成,subscribeName={subscribeName},msg={msg}", subscribeName, msg);
        }