麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

rabbitmq學習4:Routing

2019-11-08 03:09:46
字體:
來源:轉載
供稿:網友

非常感謝  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'  

 

 


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 久久久激情网 | lutube成人福利在线观看污 | 国产小视频一区 | 国产在线观看av | 亚洲精品欧美在线 | 国产精品久久久乱弄 | 日本精品一二区 | 日韩色电影 | 国产精品免费一区二区 | 12av毛片 | av免费在线不卡 | 久久免费视频精品 | 一区二区精品在线 | 一区二区三级视频 | 91精品国产乱码久久久久久久久 | 久久靖品 | av影院在线播放 | 一级爱爱 | 欧美a视频 | 粉嫩粉嫩一区二区三区在线播放 | 伊人亚洲精品 | 99这里有精品 | 国产精品久久久久久久久久10秀 | 免费香蕉成视频成人网 | 天天碰夜夜操 | 4p一女两男做爰在线观看 | 中文字幕 亚洲一区 | 亚州精品国产 | 超碰97国产在线 | 色女人在线 | 午夜在线视频一区二区三区 | www.99久久久 | 国产一区国产二区在线观看 | 免费a视频 | 毛片网站视频 | 成人毛片免费播放 | 精品国产视频一区二区三区 | 成人黄视频在线观看 | 欧美无极品 | 一级一级一级一级毛片 | 一级做受毛片免费大片 |