RabbitMQ 四種Exchange類型
- RabbitMQ Exchange類型之Direct Exchange
- RabbitMQ Exchange類型之Topic Exchange
- RabbitMQ Exchange類型之fanout Exchange
- RabbitMQ Exchange類型之headers Exchange
headers Exchange介紹
headers Exchange與Direct、topic、fanout
不同,它時通過匹配AMQP消息的header而非路由鍵。
headers Exchange
與Direct Exchange
類似,性能方面比後者查很多,所以在實際項目中用的很少,通過一張圖來直觀感受一下其工作流程
Java代碼實現
- 生產者
public static void main(String[] args) throws Exception{
//1 創建ConnectionFactory
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
//設置虛擬主機
connectionFactory.setVirtualHost("/");
//創建Connection
Connection connection = connectionFactory.newConnection();
//創建Channel
Channel channel = connection.createChannel();
//交換機名稱
String exchangeName = "test_headers_exchange";
//消息頭
Map<String,Object> headers=new HashMap<>();
headers.put("format","JSON");
headers.put("type","pdf");
//添加到headers
AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder()
.headers(headers)
.build();
// routingKey
//發送5條消息
for (int i=1;i<=5;i++) {
String msg = "RabbitMQ headers Exchange TEST"+i;
channel.basicPublish(exchangeName, "", basicProperties , msg.getBytes());
}
channel.close();
connection.close();
}
- 消費者
在聲明消息隊列時,需要指定header參數,其中x-match
爲特殊的headers
,爲all
時則表示要匹配所有的header,如果爲any
則表示只要匹配其中的一個header即可
Map<String,Object> arguments=new HashMap<>();
arguments.put("x-match","all");
//arguments.put("x-match","any");
//聲明一個隊列
channel.queueDeclare(queueName, false, false, false, arguments);
public static void main(String[] args) throws Exception{
ConnectionFactory connectionFactory = new ConnectionFactory() ;
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setVirtualHost("/");
connectionFactory.setUsername("guest");
connectionFactory.setPassword("guest");
connectionFactory.setAutomaticRecoveryEnabled(true);
connectionFactory.setNetworkRecoveryInterval(3000);
Connection connection = connectionFactory.newConnection();
Channel channel = connection.createChannel();
//交換機名稱
String exchangeName = "test_headers_exchange";
//交換機類型
String exchangeType = "headers";
//消息隊列名稱
String queueName = "test_fanout_queue";
//fanout模式不以routingKey爲匹配規則,可以爲空
String routingKey = " ";
//聲明交換機
channel.exchangeDeclare(exchangeName, exchangeType, true, false, false, null);
//隊列的參數
//x-match爲特殊的headers,爲all時則表示要匹配所有的header,如果爲any則表示只要匹配其中的一個header即可
Map<String,Object> arguments=new HashMap<>();
arguments.put("x-match","all");
//arguments.put("x-match","any");
//聲明一個隊列
channel.queueDeclare(queueName, false, false, false, arguments);
//建立Exchange 和Queue綁定關係
channel.queueBind(queueName, exchangeName, routingKey);
//參數說明:隊列名稱、是否自動ACK、DefaultConsumer(消費者)
channel.basicConsume(queueName, true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
byte[] body) throws IOException {
System.out.println("收到消息:"+new String(body));
}
});
}
依次啓動消費者
和生產者
,在消費者
控制檯可以看到接收到消息的輸出