zoukankan      html  css  js  c++  java
  • RabbitMQ 消息队列 入门 第一章

    RabbitMQ :

    官网:https://www.rabbitmq.com/

    GitHub:https://github.com/rabbitmq?q=rabbitmq

    第一步安装:

    1. 点击  http://www.erlang.org/downloads  下载 erlang  安装。
    2. 点击 https://www.rabbitmq.com/download.html 进入下载页面选择版本下载。
    3. 菜单查找  RabbitMQ Service - start.exe 点击运行服务。

    开始使用:

    1. 新建控制台项目
    2. 添加引用

        

        3.建立生产者

        

            /// <summary>
            /// 消息生产者
            /// </summary>
            /// <param name="message">消息</param>
            public static void RabbitProducerTest(string message)
            {
                try
                {
                    //创建连接工广场
                    ConnectionFactory factory = new ConnectionFactory()
                    {
                        HostName = "localhost",
                        Port = 5672,
                    };
                    //实例化连接
                    using (var connection = factory.CreateConnection())
                    {
                        //创建通道
                        using (var channel = connection.CreateModel())
                        {
                            //声明队列
                            channel.QueueDeclare(queue: "hello",
                                                durable: false,
                                                exclusive: false,
                                                autoDelete: false,
                                                arguments: null);
    
                            var body = Encoding.UTF8.GetBytes(message);
    
                            //消息推送
                            channel.BasicPublish(exchange: "",
                                                 routingKey: "hello",
                                                 basicProperties: null,
                                                 body: body);
                            Console.WriteLine("{1} Sent {0}", message,DateTime.Now.ToString());
                        }//# using channel end
                    }//# using connection end
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);
                    Console.WriteLine("HelloWordTest -- Error Press [enter] to cuntinue.");
                    Console.ReadLine();
                }
            }
    //循环发送消息
    static void Main(string[] args)
            {
        
                while (true)
                {
                    Console.WriteLine("press enter your message [enter] to send");
                    string message = Console.ReadLine();
                    RabbitMQTest.RabbitProducerTest(message);
                }
            }
    
            
    

      4.建立消费者(新建另外一个控制台程序)

     /// <summary>
            /// 消息消费者
            /// </summary>
            public static void RabbitComsumerTest()
            {
                try
                {
                    ConnectionFactory factory = new ConnectionFactory()
                    {
                        HostName = "localhost",
                        Port = 5672
                    };
                    using (var connection = factory.CreateConnection())
                    {
                        using (var channel = connection.CreateModel())
                        {
                            channel.QueueDeclare(queue: "hello",
                                                 durable: false,
                                                 exclusive: false,
                                                 autoDelete: false,
                                                 arguments: null);
                            //给通道创建消费者
                            var consumer = new EventingBasicConsumer(channel);
                            //消费者事件/行为
                            consumer.Received += (model, ea) =>
                            {
                                Console.WriteLine(string.Format("{0} Received a message", DateTime.Now.ToString()));
                                var body = ea.Body;
                                var message = Encoding.UTF8.GetString(body);
                                Console.WriteLine("Message Content:{0}", message);
                            };
                            channel.BasicConsume(queue: "hello",
                                                 autoAck: true,
                                                 consumer: consumer);
                            Console.ReadLine();
                        }
                    }
                }catch(Exception ex)
                {
                    Console.WriteLine("发生异常:"+ex.Message);
                    Console.ReadLine();
                }
            }        
    
    static void Main(string[] args)
            {
                RabbitMQTest.RabbitComsumerTest();
            }                    
    

      5.同时运行两个程序

    如果队列堆积,可开启多个消费者增加处理效率

      

  • 相关阅读:
    Excel导入
    Git 操作命令
    java线程池
    jquery 初始化数据 添加html 第一次玩0.0
    设计模式六大原则之二:里氏替换原则
    升级指令
    strtok和strtok_r(转载)
    docker进阶之路-基础篇 | 二:portainer安装与基本使用
    docker进阶之路-基础篇 | 一:环境搭建
    集群式Quartz定时任务框架实践
  • 原文地址:https://www.cnblogs.com/wuxiaozhu/p/9023385.html
Copyright © 2011-2022 走看看