最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

RabbitMQ 最常用的三大模式實(shí)例解析

 更新時(shí)間:2019年12月10日 09:34:16   作者:海向  
這篇文章主要介紹了RabbitMQ 最常用的三大模式實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

這篇文章主要介紹了RabbitMQ 最常用的三大模式實(shí)例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

Direct 模式

  • 所有發(fā)送到 Direct Exchange 的消息被轉(zhuǎn)發(fā)到 RouteKey 中指定的 Queue。
  • Direct 模式可以使用 RabbitMQ 自帶的 Exchange: default Exchange,所以不需要將 Exchange 進(jìn)行任何綁定(binding)操作。
  • 消息傳遞時(shí),RouteKey 必須完全匹配才會(huì)被隊(duì)列接收,否則該消息會(huì)被拋棄,

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class DirectProducer {
  public static void main(String[] args) throws Exception {
    //1. 創(chuàng)建一個(gè) ConnectionFactory 并進(jìn)行設(shè)置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");

    //2. 通過連接工廠來創(chuàng)建連接
    Connection connection = factory.newConnection();

    //3. 通過 Connection 來創(chuàng)建 Channel
    Channel channel = connection.createChannel();

    //4. 聲明
    String exchangeName = "test_direct_exchange";
    String routingKey = "item.direct";

    //5. 發(fā)送
    String msg = "this is direct msg";
    channel.basicPublish(exchangeName, routingKey, null, msg.getBytes());
    System.out.println("Send message : " + msg);

    //6. 關(guān)閉連接
    channel.close();
    connection.close();
  }
}
import com.rabbitmq.client.*;
import java.io.IOException;

public class DirectConsumer {

  public static void main(String[] args) throws Exception {
    //1. 創(chuàng)建一個(gè) ConnectionFactory 并進(jìn)行設(shè)置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(3000);
   
    //2. 通過連接工廠來創(chuàng)建連接
    Connection connection = factory.newConnection();

    //3. 通過 Connection 來創(chuàng)建 Channel
    Channel channel = connection.createChannel();

    //4. 聲明
    String exchangeName = "test_direct_exchange";
    String queueName = "test_direct_queue";
    String routingKey = "item.direct";
    channel.exchangeDeclare(exchangeName, "direct", true, false, null);
    channel.queueDeclare(queueName, false, false, false, null);

    //一般不用代碼綁定,在管理界面手動(dòng)綁定
    channel.queueBind(queueName, exchangeName, routingKey);

    //5. 創(chuàng)建消費(fèi)者并接收消息
    Consumer consumer = new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope,
                    AMQP.BasicProperties properties, byte[] body)
          throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
      }
    };

    //6. 設(shè)置 Channel 消費(fèi)者綁定隊(duì)列
    channel.basicConsume(queueName, true, consumer);

  }
}
 Send message : this is direct msg
 
 [x] Received 'this is direct msg'

Topic 模式

可以使用通配符進(jìn)行模糊匹配

  • 符號(hào)'#" 匹配一個(gè)或多個(gè)詞
  • 符號(hào)"*”匹配不多不少一個(gè)詞

例如

  • 'log.#"能夠匹配到'log.info.oa"
  • "log.*"只會(huì)匹配到"log.erro“

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;

public class TopicProducer {

  public static void main(String[] args) throws Exception {
    //1. 創(chuàng)建一個(gè) ConnectionFactory 并進(jìn)行設(shè)置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");

    //2. 通過連接工廠來創(chuàng)建連接
    Connection connection = factory.newConnection();

    //3. 通過 Connection 來創(chuàng)建 Channel
    Channel channel = connection.createChannel();

    //4. 聲明
    String exchangeName = "test_topic_exchange";
    String routingKey1 = "item.update";
    String routingKey2 = "item.delete";
    String routingKey3 = "user.add";

    //5. 發(fā)送
    String msg = "this is topic msg";
    channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());
    System.out.println("Send message : " + msg);

    //6. 關(guān)閉連接
    channel.close();
    connection.close();
  }
}
import com.rabbitmq.client.*;
import java.io.IOException;

public class TopicConsumer {

  public static void main(String[] args) throws Exception {
    //1. 創(chuàng)建一個(gè) ConnectionFactory 并進(jìn)行設(shè)置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(3000);

    //2. 通過連接工廠來創(chuàng)建連接
    Connection connection = factory.newConnection();

    //3. 通過 Connection 來創(chuàng)建 Channel
    Channel channel = connection.createChannel();

    //4. 聲明
    String exchangeName = "test_topic_exchange";
    String queueName = "test_topic_queue";
    String routingKey = "item.#";
    channel.exchangeDeclare(exchangeName, "topic", true, false, null);
    channel.queueDeclare(queueName, false, false, false, null);

    //一般不用代碼綁定,在管理界面手動(dòng)綁定
    channel.queueBind(queueName, exchangeName, routingKey);

    //5. 創(chuàng)建消費(fèi)者并接收消息
    Consumer consumer = new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope,
                    AMQP.BasicProperties properties, byte[] body)
          throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
      }
    };
    //6. 設(shè)置 Channel 消費(fèi)者綁定隊(duì)列
    channel.basicConsume(queueName, true, consumer);

  }
}
Send message : this is topc msg

[x] Received 'this is topc msg'
[x] Received 'this is topc msg'

Fanout 模式

不處理路由鍵,只需要簡單的將隊(duì)列綁定到交換機(jī)上發(fā)送到交換機(jī)的消息都會(huì)被轉(zhuǎn)發(fā)到與該交換機(jī)綁定的所有隊(duì)列上。
Fanout交換機(jī)轉(zhuǎn)發(fā)消息是最快的。

import com.rabbitmq.client.*;
import java.io.IOException;

public class FanoutConsumer {
  public static void main(String[] args) throws Exception {
    //1. 創(chuàng)建一個(gè) ConnectionFactory 并進(jìn)行設(shè)置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");
    factory.setAutomaticRecoveryEnabled(true);
    factory.setNetworkRecoveryInterval(3000);

    //2. 通過連接工廠來創(chuàng)建連接
    Connection connection = factory.newConnection();

    //3. 通過 Connection 來創(chuàng)建 Channel
    Channel channel = connection.createChannel();

    //4. 聲明
    String exchangeName = "test_fanout_exchange";
    String queueName = "test_fanout_queue";
    String routingKey = "item.#";
    channel.exchangeDeclare(exchangeName, "fanout", true, false, null);
    channel.queueDeclare(queueName, false, false, false, null);

    //一般不用代碼綁定,在管理界面手動(dòng)綁定
    channel.queueBind(queueName, exchangeName, routingKey);

    //5. 創(chuàng)建消費(fèi)者并接收消息
    Consumer consumer = new DefaultConsumer(channel) {
      @Override
      public void handleDelivery(String consumerTag, Envelope envelope,
                    AMQP.BasicProperties properties, byte[] body)
          throws IOException {
        String message = new String(body, "UTF-8");
        System.out.println(" [x] Received '" + message + "'");
      }
    };

    //6. 設(shè)置 Channel 消費(fèi)者綁定隊(duì)列
    channel.basicConsume(queueName, true, consumer);
  }
}
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class FanoutProducer {

  public static void main(String[] args) throws Exception {
    //1. 創(chuàng)建一個(gè) ConnectionFactory 并進(jìn)行設(shè)置
    ConnectionFactory factory = new ConnectionFactory();
    factory.setHost("localhost");
    factory.setVirtualHost("/");
    factory.setUsername("guest");
    factory.setPassword("guest");

    //2. 通過連接工廠來創(chuàng)建連接
    Connection connection = factory.newConnection();

    //3. 通過 Connection 來創(chuàng)建 Channel
    Channel channel = connection.createChannel();

    //4. 聲明
    String exchangeName = "test_fanout_exchange";
    String routingKey1 = "item.update";
    String routingKey2 = "";
    String routingKey3 = "ookjkjjkhjhk";//任意routingkey

    //5. 發(fā)送
    String msg = "this is fanout msg";
    channel.basicPublish(exchangeName, routingKey1, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey2, null, msg.getBytes());
    channel.basicPublish(exchangeName, routingKey3, null, msg.getBytes());
    System.out.println("Send message : " + msg);

    //6. 關(guān)閉連接
    channel.close();
    connection.close();
  }
}
Send message : this is fanout msg

[x] Received 'this is fanout msg'
[x] Received 'this is fanout msg'
[x] Received 'this is fanout msg'

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • 解決mac最新版intellij idea崩潰閃退crash的問題

    解決mac最新版intellij idea崩潰閃退crash的問題

    這篇文章主要介紹了解決mac最新版intellij idea崩潰閃退crash的問題,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Java使用keySet方法獲取Map集合中的元素

    Java使用keySet方法獲取Map集合中的元素

    這篇文章主要為大家詳細(xì)介紹了Java使用keySet方法獲取Map集合中的元素,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-08-08
  • 完美解決Spring Boot前端的Access-Control-Allow-Origin跨域問題

    完美解決Spring Boot前端的Access-Control-Allow-Origin跨域問題

    這篇文章主要介紹了完美解決Spring Boot前端的Access-Control-Allow-Origin跨域問題,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-05-05
  • java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼

    java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)詳解及實(shí)例代碼

    這篇文章主要介紹了java 實(shí)現(xiàn)單鏈表逆轉(zhuǎn)實(shí)例代碼的相關(guān)資料,需要的朋友可以參考下
    2017-02-02
  • java實(shí)現(xiàn)銀行家算法(Swing界面)

    java實(shí)現(xiàn)銀行家算法(Swing界面)

    這篇文章主要為大家詳細(xì)介紹了銀行家算法的java代碼實(shí)現(xiàn),Swing寫的界面,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2019-12-12
  • springboot內(nèi)置tomcat之NIO處理流程一覽

    springboot內(nèi)置tomcat之NIO處理流程一覽

    這篇文章主要介紹了springboot內(nèi)置tomcat之NIO處理流程,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2021-12-12
  • Spring源碼解析后置處理器梳理總結(jié)

    Spring源碼解析后置處理器梳理總結(jié)

    這篇文章主要介紹了Spring源碼解析后置處理器梳理總結(jié),在前面幾篇文章中梳理了Spring中bean的創(chuàng)建過程,在這個(gè)過程中各式各樣的后置處理器發(fā)揮了不同的作用,可以說后置處理器貫穿了bean的實(shí)例化以及初始化過程
    2022-07-07
  • Springboot Vue可配置調(diào)度任務(wù)實(shí)現(xiàn)示例詳解

    Springboot Vue可配置調(diào)度任務(wù)實(shí)現(xiàn)示例詳解

    這篇文章主要為大家介紹了Springboot Vue可配置調(diào)度任務(wù)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-01-01
  • Java中注解@JsonFormat與@DateTimeFormat的使用

    Java中注解@JsonFormat與@DateTimeFormat的使用

    從數(shù)據(jù)庫獲取時(shí)間傳到前端進(jìn)行展示的時(shí)候,我們有時(shí)候可能無法得到一個(gè)滿意的時(shí)間格式的時(shí)間日期,本文主要介紹了Java中注解@JsonFormat與@DateTimeFormat的使用,文中通過示例代碼介紹的非常詳細(xì),需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-08-08
  • 詳解在Spring中如何自動(dòng)創(chuàng)建代理

    詳解在Spring中如何自動(dòng)創(chuàng)建代理

    這篇文章主要介紹了詳解在Spring中如何自動(dòng)創(chuàng)建代理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-07-07

最新評論

河池市| 遂川县| 格尔木市| 苗栗县| 临漳县| 高密市| 博湖县| 资溪县| 洛南县| 南昌市| 贡嘎县| 洪雅县| 时尚| 包头市| 莱西市| 崇左市| 迭部县| 高阳县| 准格尔旗| 南靖县| 吉水县| 水城县| 红安县| 乐都县| 二连浩特市| 灌南县| 曲靖市| 和田县| 榆树市| 思茅市| 东兴市| 闽侯县| 汤原县| 绍兴市| 龙州县| 榕江县| 离岛区| 莒南县| 峨边| 鄂州市| 河源市|