zoukankan      html  css  js  c++  java
  • RabbitMQ Topic交换机代码实现

    Topic交换机和队列绑定是允许使用通配符

    *匹配一个单词

    #匹配多个单词

    发布

    public void TopicPublish()
            {
                MQHelper mh = new MQHelper();
                using (var conn = mh.GetConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        // 声明队列
                        channel.QueueDeclare(queue: "ChinaQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
                        channel.QueueDeclare(queue: "NewsQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
    
                        //声明交换机
                        channel.ExchangeDeclare(exchange: "TopicExchange", type: ExchangeType.Topic, durable: true, autoDelete: false, arguments: null);
    
                        //绑定
                        channel.QueueBind(queue: "ChinaQueue", exchange: "TopicExchange", routingKey: "China.#", arguments: null);
                        channel.QueueBind(queue: "NewsQueue", exchange: "TopicExchange", routingKey: "#.News", arguments: null);
    
                        string message = "来自中国的新闻消息";
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish(exchange: "TopicExchange", routingKey:"China.News", basicProperties: null, body: body);
                        Console.WriteLine($"[{message}]已发送到队列");
    
                        string message2 = "来自中国的天气消息";
                        var body2 = Encoding.UTF8.GetBytes(message2);
                        channel.BasicPublish(exchange: "TopicExchange", routingKey: "China.Weather", basicProperties: null, body: body2);
                        Console.WriteLine($"[{message2}]已发送到队列");
    
                        string message3 = "来自美国的新闻消息";
                        var body3 = Encoding.UTF8.GetBytes(message2);
                        channel.BasicPublish(exchange: "TopicExchange", routingKey: "US.News", basicProperties: null, body: body3);
                        Console.WriteLine($"[{message3}]已发送到队列");
                    }
                }
            }

    上面发布了三条消息。China.News和China.Weather会匹配到ChinaQueue,而China.News和US.News会匹配到NewsQueue

    消费:

            public void TopicConsume()
            {
                var factory = new ConnectionFactory();
                factory.HostName = "localhost";
                factory.UserName = "guest";
                factory.Password = "guest";
    
                using (var conn = factory.CreateConnection())
                {
                    using (IModel channel = conn.CreateModel())
                    {
                        // 声明队列
                        // channel.QueueDeclare(queue: "ChinaQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
                        channel.QueueDeclare(queue: "NewsQueue", durable: true, exclusive: false, autoDelete: false, arguments: null);
    
                        //声明交换机
                        channel.ExchangeDeclare(exchange: "TopicExchange", type: ExchangeType.Topic, durable: true, autoDelete: false, arguments: null);
    
                        //绑定
                        // channel.QueueBind(queue: "ChinaQueue", exchange: "TopicExchange", routingKey: "China.#", arguments: null);
                        channel.QueueBind(queue: "NewsQueue", exchange: "TopicExchange", routingKey: "#.News", arguments: null);
    
    
                        //消费消息
                        var consumer = new EventingBasicConsumer(channel);
                        consumer.Received += (model, ea) =>
                        {
                            var body = ea.Body;
                            var message = Encoding.UTF8.GetString(body.ToArray());
                            Console.WriteLine($"接收成功,[{message}]");
                        };
                        //处理消息
                        channel.BasicConsume(queue: "NewsQueue", autoAck: true, consumer: consumer);
                    }
                }
            }
    记录编程的点滴,体会学习的乐趣
  • 相关阅读:
    Ubuntu 20.04下EasyConnect兼容性问题临时解决方案
    Ubuntu 20.04 LTS安装搜狗输入法,只需三条命令,还能自动更新
    Java笔记: 继承成员覆盖和隐藏
    Java扫雷游戏: JMine
    Emacs: 设置窗口标题格式
    Java笔记: protected的真正含义
    Java笔记: 初始化块
    Ubuntu跨版本安装软件
    百度编辑器 Ueditor 增加字体
    AspCms 升级百度编辑器
  • 原文地址:https://www.cnblogs.com/AduBlog/p/14905553.html
Copyright © 2011-2022 走看看