1. Producer Producer.Java
package cn.slimsmart.rabbitmq.demo.headers; 02. 03.import java.util.Date; 04.import java.util.Hashtable; 05.import java.util.Map; 06. 07.import org.springframework.amqp.core.ExchangeTypes; 08. 09.import com.rabbitmq.client.AMQP; 10.import com.rabbitmq.client.AMQP.BasicProperties; 11.import com.rabbitmq.client.AMQP.BasicProperties.Builder; 12.import com.rabbitmq.client.Channel; 13.import com.rabbitmq.client.Connection; 14.import com.rabbitmq.client.ConnectionFactory; 15. 16.public class Producer { 17. private final static String EXCHANGE_NAME = "header-exchange"; 18. 19. @SuppressWarnings("deprecation") 20. public static void main(String[] args) throws Exception { 21. // Create connections and channels 22. ConnectionFactory factory = new ConnectionFactory(); 23. factory.setHost("192.168.36.102"); 24. // Specify the user password 25. factory.setUsername("admin"); 26. factory.setPassword("admin"); 27. // Specify the port 28. factory.setPort(AMQP.PROTOCOL.PORT); 29. Connection connection = factory.newConnection(); 30. Channel channel = connection.createChannel(); 31. 32. // Declare repeaters and type headers 33. channel.exchangeDeclare(EXCHANGE_NAME, ExchangeTypes.HEADERS,false,true,null); 34. String message = new Date().toLocaleString() + " : log something"; 35. 36. Map<String,Object> headers = new Hashtable<String, Object>(); 37. headers.put("aaa", "01234"); 38. Builder properties = new BasicProperties.Builder(); 39. properties.headers(headers); 40. 41. // Specify the forwarder to which the message is sent, bind the key-value pair headers key-value pair 42. channel.basicPublish(EXCHANGE_NAME, "",properties.build(),message.getBytes()); 43. 44. System.out.println("Sent message :'" + message + "'"); 45. channel.close(); 46. connection.close(); 47. } 48.}
2. Consumer Consumer.java
package cn.slimsmart.rabbitmq.demo.headers; 02. 03.import java.util.Hashtable; 04.import java.util.Map; 05. 06.import org.springframework.amqp.core.ExchangeTypes; 07. 08.import com.rabbitmq.client.AMQP; 09.import com.rabbitmq.client.Channel; 10.import com.rabbitmq.client.Connection; 11.import com.rabbitmq.client.ConnectionFactory; 12.import com.rabbitmq.client.QueueingConsumer; 13. 14.public class Consumer { 15. private final static String EXCHANGE_NAME = "header-exchange"; 16. private final static String QUEUE_NAME = "header-queue"; 17. 18. public static void main(String[] args) throws Exception { 19. // Create connections and channels 20. ConnectionFactory factory = new ConnectionFactory(); 21. factory.setHost("192.168.36.102"); 22. // Specify the user password 23. factory.setUsername("admin"); 24. factory.setPassword("admin"); 25. // Specify the port 26. factory.setPort(AMQP.PROTOCOL.PORT); 27. Connection connection = factory.newConnection(); 28. Channel channel = connection.createChannel(); 29. 30. // Declare repeaters and type headers 31. channel.exchangeDeclare(EXCHANGE_NAME, ExchangeTypes.HEADERS,false,true,null); 32. channel.queueDeclare(QUEUE_NAME,false, false, true,null); 33. 34. Map<String, Object> headers = new Hashtable<String, Object>(); 35. headers.put("x-match", "any");//all any 36. headers.put("aaa", "01234"); 37. headers.put("bbb", "56789"); 38. // Specify the queue for the repeater, set the binding to bind the header key-value pair 39. channel.queueBind(QUEUE_NAME, EXCHANGE_NAME,"", headers); 40. QueueingConsumer consumer = new QueueingConsumer(channel); 41. // Specify the receiver, the second parameter is automatic response, no manual response is required 42. channel.basicConsume(QUEUE_NAME, true, consumer); 43. while (true) { 44. QueueingConsumer.Delivery delivery = consumer.nextDelivery(); 45. String message = new String(delivery.getBody()); 46. System.out.println(message); 47. } 48. } 49.}
Reprinted from: http://blog.csdn.net/zhu_tianwei/article/details/40923131