RabbitMQ-通配符模式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Anbang713/article/details/82892151

一、简介

上一篇,我们说到《RabbitMQ-路由模式》实现了一条消息被多个消费者消费。在路由模式中我们通过指定routingKey,消费者只有订阅了该key的队列才能消费消息。今天我们学习通配符模式,它可以说是路由模式的升级版,反过来说路由模式就是通配符模式的特殊情况。

通配符模式:将路由键和某模式进行匹配,此时队列需要绑定一个模式上。符号“#”匹配一个或多个词,符号“*”匹配一个词。比如“hello.#”能够匹配到“hello.123.456”,但是“hello.*”只能匹配到“hello.123”。

二、 编码实现

2.1、生产者

public class Producer {

  public static void main(String[] argv) throws Exception {
    // 获取到连接以及mq通道
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();
    // 声明exchange
    channel.exchangeDeclare(QueueUtil.EXCHANGE_NAME_TOPIC, "topic");
    // 消息内容
    String message = "Hello World!";
    channel.basicPublish(QueueUtil.EXCHANGE_NAME_TOPIC, "hello.123.456", null, message.getBytes());
    channel.close();
    connection.close();
  }
}

2.2、消费者1

public class Receiver1 {

  public static void main(String[] argv) throws Exception {
    // 获取到连接以及mq通道
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();
    // 声明队列
    channel.queueDeclare(QueueUtil.QUEUE_NAME_TOPIC1, false, false, false, null);
    // 绑定队列到交换机
    channel.queueBind(QueueUtil.QUEUE_NAME_TOPIC1, QueueUtil.EXCHANGE_NAME_TOPIC, "hello.*");
    // 同一时刻服务器只会发一条消息给消费者
    channel.basicQos(1);
    // 定义队列的消费者
    QueueingConsumer consumer = new QueueingConsumer(channel);
    // 监听队列,手动返回完成
    channel.basicConsume(QueueUtil.QUEUE_NAME_TOPIC1, false, consumer);
    // 获取消息
    while (true) {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      String message = new String(delivery.getBody());
      System.out.println("Receiver1 Received:" + message);
      Thread.sleep(10);

      channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
  }
}

2.3、消费者2

public class Receiver2 {

  public static void main(String[] argv) throws Exception {
    // 获取到连接以及mq通道
    Connection connection = ConnectionUtil.getConnection();
    Channel channel = connection.createChannel();
    // 声明队列
    channel.queueDeclare(QueueUtil.QUEUE_NAME_TOPIC2, false, false, false, null);
    // 绑定队列到交换机
    channel.queueBind(QueueUtil.QUEUE_NAME_TOPIC2, QueueUtil.EXCHANGE_NAME_TOPIC, "hello.#");
    // 同一时刻服务器只会发一条消息给消费者
    channel.basicQos(1);
    // 定义队列的消费者
    QueueingConsumer consumer = new QueueingConsumer(channel);
    // 监听队列,手动返回完成
    channel.basicConsume(QueueUtil.QUEUE_NAME_TOPIC2, false, consumer);
    // 获取消息
    while (true) {
      QueueingConsumer.Delivery delivery = consumer.nextDelivery();
      String message = new String(delivery.getBody());
      System.out.println("Receiver2 Received:" + message);
      Thread.sleep(10);
      channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
    }
  }
}

三、测试

(1)通过管理界面可以看到topic_exchange有两个队列。

(2)生产者发送一条hello.123.456的消息,只有消费者2消费。 

(3)生产者发送一条hello.123的消息,消费者1和消费者2都能消费。

 

源代码地址:https://gitee.com/chengab/RabbitMQ  

猜你喜欢

转载自blog.csdn.net/Anbang713/article/details/82892151