RabbitMQ Headers Exchange示例
(1)、发布者
1 var connectionFactory = new ConnectionFactory() { HostName="192.168.205.128",UserName="guest",Password="guest"}; //创建连接工厂 2 var connection = connectionFactory.CreateConnection(); //创建connection 3 var channel = connection.CreateModel(); //创建channel 4 //声明交换机 5 //String exchange, 交换机名称 6 //String type, 交换机类型 7 //Boolean durable, 是否持久化 8 //Boolean autoDelete, 是否自动删除 9 //IDictionary< String, Object > arguments 交换机参数 10 channel.ExchangeDeclare("headersExchange", ExchangeType.Headers, true, false, null); 11 //声明队列 12 //String queue, 队列名称 13 //Boolean durable, 是否持久化 14 //Boolean exclusive, 是否专有的(排外) 15 //Boolean autoDelete, 是否自动删除 16 //IDictionary<String, Object> arguments 队列参数 17 channel.QueueDeclare("headersQueue", true, false, false, null); 18 //将队列绑定到交换机上 19 //String queue, 队列名称 20 //String exchange, 交换机名称 21 //String routingKey, routingKey 22 //IDictionary< String, Object > arguments 绑定参数 23 IDictionary<String, Object> arguments = new Dictionary<String, Object>() 24 { 25 {"x-match","any"}, //设置x-match属性为any,即头信息中只要有一个匹配就可以了 26 { "username","fanqi"} 27 }; 28 channel.QueueBind("headersQueue", "headersExchange", string.Empty, arguments); 29 30 //设置消息属性 31 var properties = channel.CreateBasicProperties(); 32 properties.Headers = new Dictionary<String, Object>(); 33 properties.Headers.Add("username", "fanqi"); 34 35 //发布消息 36 //String exchange, 交换机名称 37 //String routingKey, routingKey 38 //IBasicProperties basicProperties, 发布属性 39 //Byte[] body 消息内容 40 channel.BasicPublish("headersExchange", string.Empty, properties, Encoding.UTF8.GetBytes("来自.net的问候")); 41 Console.ReadKey();
(2)、接收者
1 var connectionFactory = new ConnectionFactory() { HostName = "192.168.205.128", UserName = "guest", Password = "guest" }; //创建连接工厂 2 var connection = connectionFactory.CreateConnection(); //创建connection 3 var channel = connection.CreateModel(); //创建channel 4 //声明交换机 5 //String exchange, 交换机名称 6 //String type, 交换机类型 7 //Boolean durable, 是否持久化 8 //Boolean autoDelete, 是否自动删除 9 //IDictionary< String, Object > arguments 交换机参数 10 channel.ExchangeDeclare("headersExchange", ExchangeType.Headers, true, false, null); 11 //声明队列 12 //String queue, 队列名称 13 //Boolean durable, 是否持久化 14 //Boolean exclusive, 是否专有的(排外) 15 //Boolean autoDelete, 是否自动删除 16 //IDictionary<String, Object> arguments 队列参数 17 channel.QueueDeclare("headersQueue", true, false, false, null); 18 //将队列绑定到交换机上 19 //String queue, 队列名称 20 //String exchange, 交换机名称 21 //String routingKey, routingKey 22 //IDictionary< String, Object > arguments 绑定参数 23 24 IDictionary<String, Object> arguments = new Dictionary<String, Object>() 25 { 26 {"x-match","any"}, //设置x-match属性为any,即头信息中只要有一个匹配就可以了 27 { "username","fanqi"} 28 }; 29 channel.QueueBind("headersQueue", "headersExchange", string.Empty, arguments); 30 //1.直接获取消息 31 //var result = channel.BasicGet("headersQueue", true); 32 //Console.WriteLine(Encoding.UTF8.GetString(result.Body)); 33 //2.使用事件机制获取消息 34 EventingBasicConsumer consumer = new EventingBasicConsumer(channel); 35 consumer.Received += (sender, e) => 36 { 37 Console.WriteLine(Encoding.UTF8.GetString(e.Body)); 38 }; 39 channel.BasicConsume("headersQueue", true, consumer); 40 Console.ReadKey();