• RabbitMQ使用


    发送消息:
    
    
            static ConnectionFactory factory = new ConnectionFactory();
            //static IConnection connection = factory.CreateConnection();
            //static IModel channel = connection.CreateModel();
            static void Main(string[] args)
            {
                factory.HostName = "192.168.5.38";//RabbitMQ服务在本地运行
                factory.UserName = "guest";//用户名
                factory.Password = "guest";//密码
    
                for (int i = 0; i < 100; i++)
                {
                    Thread t = new Thread(new ThreadStart(a));
                    t.Start();
                    Console.WriteLine(i.ToString());
                }
    
    
               // var factory = new ConnectionFactory();
    
    
                using (var connection = factory.CreateConnection())
                {
                    using (var channel = connection.CreateModel())
                    {
                        channel.QueueDeclare("test", false, false, false, null);//创建一个名称为test的消息队列
    
    
                        string message = "testaaaaaa"; //传递的消息内容
                        var body = Encoding.UTF8.GetBytes(message);
                        channel.BasicPublish("", "test", null, body); //开始传递
                        Console.WriteLine("已发送: {0}", message);
                        Console.ReadLine();
                    }
                }
            }
    
            static void a()
            {
                for (int i = 0; i < 1000; i++)
                {
                    using (var connection = factory.CreateConnection())
                    {
                        using (var channel = connection.CreateModel())
                        {
                            channel.QueueDeclare("test", false, false, false, null);//创建一个名称为test的消息队列
    
                            string message = "Hello World1 "+i; //传递的消息内容
                            var body = Encoding.UTF8.GetBytes(message);
                            channel.BasicPublish("", "test", null, body); //开始传递
                            Console.WriteLine("已发送: {0}", message);
                        }
                    }
                }
            }
    接收消息:
            static void Main(string[] args)
            {
                var factory = new ConnectionFactory() { HostName = "192.168.5.38" };
                using (var connection = factory.CreateConnection())
                using (var channel = connection.CreateModel())
                {
                    channel.QueueDeclare(queue: "test",
                                         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("接收 {0}", message);
                    };
                    channel.BasicConsume(queue: "test",
                                         noAck: true,
                                         consumer: consumer);
    
                    Console.ReadLine();
                }
            }
  • 相关阅读:
    HUB_mysql学习笔记
    SQL学习笔记
    java_cmd_命令行
    JavaScript_2016_8_28
    linux mysql 安装配置
    solr 添加索引
    solr 查询 实例分析
    solr update接口常用方法
    solr schema.xml文档节点配置
    solr4.5安装配置 linux+tomcat6.0+mmseg4j-1.9.1分词
  • 原文地址:https://www.cnblogs.com/liyangLife/p/7885854.html
Copyright © 2020-2023  润新知