码迷,mamicode.com
首页 > 其他好文 > 详细

rabbitmq topic

时间:2020-08-18 13:55:53      阅读:61      评论:0      收藏:0      [点我收藏+]

标签:rabbitmq   readline   sicp   admin   code   write   actor   div   icp   

            var factory = new ConnectionFactory();
            factory.HostName = "localhost";
            factory.UserName = "admin";
            factory.Password = "admin";

            using (var connection = factory.CreateConnection())
            {
                using (var channel = connection.CreateModel())
                {
                    channel.ExchangeDeclare("topic_dev", "topic", true, false, null);
                    channel.QueueDeclare("login", true, false, false, null);
                    channel.QueueDeclare("signin", true, false, false, null);
                    channel.QueueBind("login", "topic_dev", "#.login.#", null);
                    channel.QueueBind("signin", "topic_dev", "#.signin.#", null);

                    var properties = channel.CreateBasicProperties();
                    properties.DeliveryMode = 2;

                    var consumer = new EventingBasicConsumer(channel);
                    consumer.Received += (model, ea) =>
                    {
                        var body = ea.Body.ToArray();
                        var msg = Encoding.UTF8.GetString(body);
                        Console.WriteLine($"login => {msg}, DeliveryTag={ea.DeliveryTag}, Redelivered={ea.Redelivered}, RoutingKey={ea.RoutingKey}");
                        channel.BasicAck(ea.DeliveryTag, false);
                    };
                    channel.BasicConsume("login", false, consumer);

                    var consumer1 = new EventingBasicConsumer(channel);
                    consumer1.Received += (model, ea) =>
                    {
                        var body = ea.Body.ToArray();
                        var msg = Encoding.UTF8.GetString(body);
                        Console.WriteLine($"signin => {msg}, DeliveryTag={ea.DeliveryTag}, Redelivered={ea.Redelivered}, RoutingKey={ea.RoutingKey}");
                        channel.BasicAck(ea.DeliveryTag, false);
                    };
                    channel.BasicConsume("signin", false, consumer1);

                    string message = "I am jonney";
                    channel.BasicPublish("topic_dev", "login.signin", properties, Encoding.UTF8.GetBytes(message)); 
                    Console.WriteLine($"Send:{message}");

                    Console.ReadLine();
                }
            }

 

rabbitmq topic

标签:rabbitmq   readline   sicp   admin   code   write   actor   div   icp   

原文地址:https://www.cnblogs.com/jonney-wang/p/13511922.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!