zoukankan      html  css  js  c++  java
  • MQ入门示例

    RabbitMQ使用

    参考链接地址:http://www.rabbitmq.com/tutorials/tutorial-one-dotnet.html

    建立两个控制台程序,一个用于发送,一个用于接收,服务端用的是别人搭好的环境,暂时不管

    通过NuGet获取需要引用的库输入RabbitMQ搜索即可,using RabbitMQ.Client;

    发送端:

    static void Main(string[] args)
            {
                var factory = new ConnectionFactory() { HostName = "XXX.33.55.XXX", Port = XXXX, UserName = "XXX", Password = "XXX" };
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "hello",
                                         durable: false,
                                         exclusive: false,
                                         autoDelete: false,
                                         arguments: null);
    
                    string message = "Hello World!";
                    var body = Encoding.UTF8.GetBytes(message);
    
                    channel.BasicPublish(exchange: "",
                                         routingKey: "hello",
                                         basicProperties: null,
                                         body: body);
                    Console.WriteLine(" [x] Sent {0}", message);
                }
    
                Console.WriteLine(" Press [enter] to exit.");
                Console.ReadLine();
            }

    接收端:

    static void Main(string[] args)
            {
                var factory = new ConnectionFactory() { HostName = "XXX.33.55.XXX", Port = XXXX, UserName = "XXX", Password = "XXX" };
                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) =>
                    {
                        var body = ea.Body;
                        var message = Encoding.UTF8.GetString(body);
                        Console.WriteLine(" [x] Received {0}", message);
                    };
                    channel.BasicConsume(queue: "hello",
                                         noAck: true,
                                         consumer: consumer);
    
                    Console.WriteLine(" Press [enter] to exit.");
                    Console.ReadLine();
                }
            }
  • 相关阅读:
    NO29 用户提权sudo配置文件详解实践--志行为审计
    NO28 第四关考试题
    NO27 定时任务
    NO26 Linux的文件权限--chmod--Linux删除文件说明--suid--sgid
    NO25 三剑客之SED行天下
    NO24 第三关--企业面试题
    gcc编译错误表
    C++的精髓——虚函数
    gcc 优化选项 -O1 -O2 -O3 -Os 优先级,-fomit-frame-pointer
    正确使用#include和前置声明(forward declaration)
  • 原文地址:https://www.cnblogs.com/sulong/p/6354385.html
Copyright © 2011-2022 走看看