非常感謝 http://wubin850219.iteye.com/blog/1004948
在《rabbitmq學習3:Publish/Subscribe 》中已經學習了發送一個消息,所有消費者端都能收到。那現在這節準備介紹通過路由規則來接受生產者端所發送的消費。Routing的工作示意圖如下:

對于Routing的示意圖與Publish/Subscribe中的示意圖區別:
第一:Publish/Subscribe的Exchange的類型為“fanout”,而Routing的類型為“direct”
第二:Publish/Subscribe的路由為默認的,而Routing的路由是自定義的。
可能從上圖的示意圖如可以發現可以把Routing的模式也可以轉化Publish/Subscribe的模式,如示意圖

我們也可能把所有的數據發送到一個Queue中去,示意圖如下:

下面我們就開始程序吧。
P端的程序如下:
java代碼
package com.abin.rabbitmq; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class EmitLogDirect { 運行結果可能如下:
Java代碼
[x] Sent 'error':'this is a error logs:0' [x] Sent 'error':'this is a error logs:1' ################################ [x] Sent 'info':'this is a info logs:0' [x] Sent 'info':'this is a info logs:1' [x] Sent 'info':'this is a info logs:2' ################################ [x] Sent 'all':'this is a all logs:0' [x] Sent 'all':'this is a all logs:1' [x] Sent 'all':'this is a all logs:2' [x] Sent 'all':'this is a all logs:3'
C端的代碼如下:
Java代碼
package com.abin.rabbitmq; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.QueueingConsumer; public class ReceiveLogsDirect { private static final String EXCHANGE_NAME = "direct_logs";//定義Exchange名稱 public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); channel.exchangeDeclare(EXCHANGE_NAME, "direct");//聲明Exchange String queueName = "queue_logs1";//定義隊列名為“queue_logs1”的Queue channel.queueDeclare(queueName, false, false, false, null); String routingKeyOne = "error";//"error"路由規則 channel.queueBind(queueName, EXCHANGE_NAME, routingKeyOne);//把Queue、Exchange及路由綁定 String routingKeyTwo = "info"; channel.queueBind(queueName, EXCHANGE_NAME, routingKeyTwo); System.out.println(" [*] Waiting for messages."); QueueingConsumer consumer = new QueueingConsumer(channel); channel.basicConsume(queueName, true, consumer); while (true) { QueueingConsumer.Delivery delivery = consumer.nextDelivery(); String message = new String(delivery.getBody()); String routingKey = delivery.getEnvelope().getRoutingKey(); System.out.println(" [x] Received '" + routingKey + "':'" + message + "'"); } } } 這里我做了二個消費端程序來模仿通過路由規則來分配信息給各個消費端。第二個消費者端的程序只是修改了一小部分代碼;只接受路由為”error“和”all“規則的消費。
運行程序1的結果如下:
Java代碼
[*] Waiting for messages. [x] Received 'error':'this is a error logs:0' [x] Received 'error':'this is a error logs:1' [x] Received 'info':'this is a info logs:0' [x] Received 'info':'this is a info logs:1' [x] Received 'info':'this is a info logs:2' 運行程序2的運行結果如下:
Java代碼
[*] Waiting for messages. [x] Received 'error':'this is a error logs:0' [x] Received 'error':'this is a error logs:1' [x] Received 'all':'this is a all logs:0' [x] Received 'all':'this is a all logs:1' [x] Received 'all':'this is a all logs:2' [x] Received 'all':'this is a all logs:3'
新聞熱點
疑難解答