• RabbitMQ具体使用


    上篇简述了 RabbitMQ环境准备/环境搭建,本片在上篇的基础上讲述RabbitMQ具体使用

    1、新建.NET Framwork console控制台项目ConsoleRabbitMQ项目(生产者,即产生消息的)

    2、对控制台项目ConsoleRabbitMQ项目使用NuGet程序管理包添加RabbitMQ.Client

    3、控制台项目ConsoleRabbitMQ项目的Program代码如下

    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Text;
    
    namespace ConsoleRabbitMQConsume
    {
        class Program
        {
            static void Main(string[] args)
            {
                var connectFactory = new ConnectionFactory
                {
                    HostName = "localhost",
                    UserName = "guest",
                    Password = "guest"
                };
                using (var conn = connectFactory.CreateConnection())
                {
                    using (var model = conn.CreateModel())
                    {
                        Console.WriteLine($"消费者连接成功");
                        model.QueueDeclare("queue", false, false, false, null);
                        var consume = new EventingBasicConsumer(model);
                        //consume.Received += Consume_Received;
                        consume.Received += (a, b) => { Console.WriteLine($"这是消费者据接收数据:{Encoding.UTF8.GetString(b.Body)}"); };
                        model.BasicConsume("queue", false, consume);
                        Console.Read();
                    }
                }
            }
    
            private static void Consume_Received(object sender, BasicDeliverEventArgs e)
            {
                Console.WriteLine($"接收数据:{Encoding.UTF8.GetString(e.Body)}");
            }
        }
    }

    4、新建.NET Framwork console控制台项目ConsoleRabbitMQConsume项目(消费者,即接收消息的)

    5、对控制台项目ConsoleRabbitMQConsume项目使用NuGet程序管理包添加RabbitMQ.Client

    6、控制台项目ConsoleRabbitMQConsume项目的Program代码如下

    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Text;
    
    namespace ConsoleRabbitMQConsume
    {
        class Program
        {
            static void Main(string[] args)
            {
                var connectFactory = new ConnectionFactory
                {
                    HostName = "localhost",
                    UserName = "guest",
                    Password = "guest"
                };
                using (var conn = connectFactory.CreateConnection())
                {
                    using (var model = conn.CreateModel())
                    {
                        Console.WriteLine($"消费者连接成功");
                        model.QueueDeclare("queue", false, false, false, null);
                        var consume = new EventingBasicConsumer(model);
                        //consume.Received += Consume_Received;
                        consume.Received += (a, b) => { Console.WriteLine($"这是消费者据接收数据:{Encoding.UTF8.GetString(b.Body)}"); };
                        model.BasicConsume("queue", false, consume);
                        Console.Read();
                    }
                }
            }
    
            private static void Consume_Received(object sender, BasicDeliverEventArgs e)
            {
                Console.WriteLine($"接收数据:{Encoding.UTF8.GetString(e.Body)}");
            }
        }
    }

    7、启动RabbitMQ服务

    输入命令:rabbitmq-service start

    浏览器中输入:http://localhost:15672/#/,点击ConnectionsChannels,Queues可以查看相关信息,如下

    8、分别启动ConsoleRabbitMQ.exe和ConsoleRabbitMQConsume.exe这个两个项目,运行效果如下

     

  • 相关阅读:
    jar包打包成exe示例(基于maven项目)
    助教小工具
    HUST软件测试1504班: 第0周作业成绩
    软件质量与测试2018春第0周作业1:开设博客
    RobotFramework学习-问题
    Java读取本地文件乱码的解决方案
    滑动窗口模板题(对读写性能要求贼高)
    Invalid index name [xxx], must be lowercase
    Java自动装箱和自动拆箱的理解
    TKMabatis坑
  • 原文地址:https://www.cnblogs.com/1175429393wljblog/p/12697263.html
Copyright © 2020-2023  润新知