• RabbitMQ 六种基本队列模式 通配符(Topic)


    https://img2022.cnblogs.com/blog/1106277/202201/1106277-20220128160605927-762812266.jpg

    这是消息模式的最后一种,通配符模式。总算是都写完了,真不容易。在通配符模式下可以按照 * 或者 # 进行匹配

    • * 只能替代一个词,例如:*.error.log 或者 *.log
    • # 可以替代0个或更多的词,例如:#.log

    通配符模式和路由模式代码很像,只需要修改发送方式就可。

    使用 Java 来实现发送者和接收者

    • 发送者

      private static final String EXCHANGE_NAME = "logs_topic";
      
          public static void main(String[] args) {
              try {
                  ConnectionFactory factory = new ConnectionFactory();
                  // 设置参数
                  factory.setHost("192.168.3.64");
                  factory.setPort(5672);
                  factory.setVirtualHost("/default_virtual_host");
                  factory.setUsername("admin");
                  factory.setPassword("admin");
      
                  Connection connection = factory.newConnection();
                  Channel channel = connection.createChannel();
                  // 创建交换机 此处不能用 FANOUT 作为交换机的类型了 ,需要换成 TOPIC
                  channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC, true, false, false, null);
                  // 声明两个队列
                  channel.queueDeclare("log_topic_queue1", false, false, false, null);
                  channel.queueDeclare("log_topic_queue2", false, false, false, null);
                  // 绑定两个队列 (此处必须要要指定 routingkey)
                  // 队列1只接收 *.info
                  channel.queueBind("log_topic_queue1", EXCHANGE_NAME, "*.info");
                  // 队列2接收所有
                  channel.queueBind("log_topic_queue2", EXCHANGE_NAME, "#");
      
      //            String message = "[日志][支付信息][订单号:" + new Date().getTime() + "][支付ID:10]";
                  // 由于要使用 routing 模式发送所以就需要执行 routingkey
                  channel.basicPublish(EXCHANGE_NAME, "member.comment.info", null, "用户评论了订单:10026".getBytes(StandardCharsets.UTF_8));
                  channel.basicPublish(EXCHANGE_NAME, "operate.info", null, "用户操作了一个按钮".getBytes(StandardCharsets.UTF_8));
                  // 释放资源
                  channel.close();
                  connection.close();
              } catch (IOException | TimeoutException e) {
                  e.printStackTrace();
              }
          }
      
    • 接收者1

      private static final String EXCHANGE_NAME = "logs_topic";
      
          public static void main(String[] args) {
              try {
                  ConnectionFactory factory = new ConnectionFactory();
                  // 设置参数
                  factory.setHost("192.168.3.64");
                  factory.setPort(5672);
                  factory.setVirtualHost("/default_virtual_host");
                  factory.setUsername("admin");
                  factory.setPassword("admin");
      
                  Connection connection = factory.newConnection();
      
                  Channel channel = connection.createChannel();
                  channel.exchangeDeclare(EXCHANGE_NAME,
                          BuiltinExchangeType.TOPIC,
                          true,
                          false, false,
                          null);
      
                  String queue1Name = "log_topic_queue1";
                  DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                      String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
                      System.out.println("Received '" + message + "'");
                  };
                  channel.basicConsume(queue1Name, true, deliverCallback, consumerTag -> {
                  });
              } catch (IOException | TimeoutException e) {
                  e.printStackTrace();
              }
      
          }
      
    • 接收者2

      private static final String EXCHANGE_NAME = "logs_topic";
      
          public static void main(String[] args) {
              try {
                  ConnectionFactory factory = new ConnectionFactory();
                  // 设置参数
                  factory.setHost("192.168.3.64");
                  factory.setPort(5672);
                  factory.setVirtualHost("/default_virtual_host");
                  factory.setUsername("admin");
                  factory.setPassword("admin");
      
                  Connection connection = factory.newConnection();
      
                  Channel channel = connection.createChannel();
                  channel.exchangeDeclare(EXCHANGE_NAME,
                          BuiltinExchangeType.TOPIC,
                          true,
                          false, false,
                          null);
      
                  String queue1Name = "log_topic_queue2";
                  DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                      String message = new String(delivery.getBody(), StandardCharsets.UTF_8);
                      System.out.println("Received '" + message + "'");
                  };
                  channel.basicConsume(queue1Name, true, deliverCallback, consumerTag -> {
                  });
              } catch (IOException | TimeoutException e) {
                  e.printStackTrace();
              }
      
          }
      
  • 相关阅读:
    bzoj2115: [Wc2011] Xor
    bzoj2844: albus就是要第一个出场
    hdu3949
    bzoj2487: Super Poker II
    bzoj3456: 城市规划
    bzoj3992: [SDOI2015]序列统计
    ubuntu 使用命令行登录oracle
    ubuntu安装docker
    linux查询硬件信息
    ubuntu oracle 环境搭建
  • 原文地址:https://www.cnblogs.com/l5gw/p/15853004.html
Copyright © 2020-2023  润新知