• C#操作RabbitMQ示例


    using RabbitMQ.Client;
    using RabbitMQ.Client.Events;
    using System;
    using System.Text;
    using System.Threading;
    
    namespace RabbitMQ
    {
        class Program
        {
            static void Main(string[] args)
            {
                var type = Console.ReadLine();
    
                if (type == "PRODUCER")
                {
                    var factory = new ConnectionFactory() { HostName = "localhost" };
                    using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        channel.ExchangeDeclare(exchange: "RabbitMQ_Exchange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
                        channel.QueueDeclare(queue: "RabbitMQ_Queue", durable: true, exclusive: false, autoDelete: false, arguments: null);
                        channel.QueueBind(queue: "RabbitMQ_Queue", exchange: "RabbitMQ_Exchange", routingKey: "RabbitMQ_RoutingKey", arguments: null);
    
                        var properties = channel.CreateBasicProperties();
                        properties.Persistent = true;
                        for (var i = 0; i < 1000; i++)
                        {
                            var message = $"RabbitMQ_Exchange # RabbitMQ_Queue # RabbitMQ_RoutingKey # {i}";
                            var body = Encoding.UTF8.GetBytes(message);
    
                            channel.BasicPublish(exchange: "RabbitMQ_Exchange", routingKey: "RabbitMQ_RoutingKey", basicProperties: properties, body: body);
                            Console.WriteLine(" [x] Sent {0}", message);
                        }
                        Console.ReadLine();
                    }
                }
    
                if (type == "CONSUMER")
                {
                    var factory = new ConnectionFactory() { HostName = "localhost" };
                    using (var connection = factory.CreateConnection())
                    using (var channel = connection.CreateModel())
                    {
                        channel.ExchangeDeclare(exchange: "RabbitMQ_Exchange", type: ExchangeType.Direct, durable: true, autoDelete: false, arguments: null);
                        channel.QueueDeclare(queue: "RabbitMQ_Queue", durable: true, exclusive: false, autoDelete: false, arguments: null);
                        channel.QueueBind(queue: "RabbitMQ_Queue", exchange: "RabbitMQ_Exchange", routingKey: "RabbitMQ_RoutingKey", arguments: null);
    
                        channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
    
                        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);
    
                            Thread.Sleep(2000);
    
                            Console.WriteLine(" [x] Done");
    
                            channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
                        };
                        channel.BasicConsume(queue: "RabbitMQ_Queue", autoAck: false, consumer: consumer);
                        Console.ReadLine();
                    }
                }
            }
        }
    }
  • 相关阅读:
    一台服务器Nginx配置多个域名(一级域名或二级)
    Nginx启动和停止
    git删除本地及远程分支
    纯js实现复制内容到剪切板
    js 实现字符串转日期进行比较大小
    git 本地分支与远程分支 新建/删除/合并
    设置Git 记住密码
    炒鸡简单的javaScript的call和apply方法
    如何打war包
    Git 查看远程分支、本地分支、删除本地分支及远程分支
  • 原文地址:https://www.cnblogs.com/xiaowangzhi/p/8642691.html
Copyright © 2020-2023  润新知