【RabbitMQ】03 订阅模式

Pub / Sub 订阅模式

特点是 一条消息可以给多个消费者接收了

 

 

首先创建订阅模式生产者发生一些代码变动:

package cn.dzz.pubSub;

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

import java.nio.charset.StandardCharsets;

public class PubSubInProducer {

    public static void main(String[] args) throws Exception{
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.2.121");
        connectionFactory.setPort(ConnectionFactory.DEFAULT_AMQP_PORT); // 5672
        connectionFactory.setVirtualHost("/dzz"); // 虚拟主机? 默认值 /
        connectionFactory.setUsername("test"); // guest
        connectionFactory.setPassword("123456"); // guest

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        /**
         *  多了一个创建交换机的过程
         *  public DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments) throws IOException {
         *      return this.exchangeDeclare(exchange, type.getType(), durable, autoDelete, internal, arguments);
         *  }
         *  String exchange 交换机名称
         *  String type 交换机类型,这里换成枚举类型,方便查找 com.rabbitmq.client.BuiltinExchangeType
         *      DIRECT("direct"), 定向  简单模式 和 工作模式
         *      FANOUT("fanout"), 扇形  广播(通知给所有和这个交换机绑定的队列)
         *      TOPIC("topic"), 通配符 ?
         *      HEADERS("headers"); 参数匹配, 视频暂不讲解
         *  boolean durable 持久化
         *  boolean autoDelete 自动删除
         *  boolean internal 内部使用 一般false
         *  Map<String, Object> arguments
         *
         *
        */
        String exchangeName = "test_fanout";
        channel.exchangeDeclare(
                exchangeName,
                BuiltinExchangeType.FANOUT,
                true,
                false,
                false,
                null
        );

        // 订阅模式案例需要两个队列支持
        String queueName1 = "pub&sub - 1";
        String queueName2 = "pub&sub - 2";
        channel.queueDeclare(queueName1, true, false, false, null);
        channel.queueDeclare(queueName2, true, false, false, null);

        /**
         * 将交换机和队列绑定
         *  public com.rabbitmq.client.AMQP.Queue.BindOk queueBind(String queue, String exchange, String routingKey) throws IOException {
         *      return this.queueBind(queue, exchange, routingKey, (Map)null);
         *  }
         *  String queue 队列名称
         *  String exchange 交换机名称
         *  String routingKey 路由键,绑定规则
         *      如果是fanout模式, 设置""即可,默认就是给所有队列绑定
         *
         */
        channel.queueBind(queueName1, exchangeName, "");
        channel.queueBind(queueName2, exchangeName, "");

        // 发送消息
        // for (int i = 0; i < 10; i++) {
        //     String body = "send sub&pub msg" + i;
        //     channel.basicPublish("", "work_queue", null, body.getBytes(StandardCharsets.UTF_8));
        // }

        // 释放资源
        // channel.close();
        // connection.close();
    }

}

启动之后,可以发现在面板Exchange上面就能找到我们创建的交换机了

 Queue面板上面有我们的两个订阅队列:

 

之后开始发送消息,注意下面发送消息的参数改动:

再次启动,将消息发送到RabbitMQ中

package cn.dzz.pubSub;

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

import java.nio.charset.StandardCharsets;

public class PubSubInProducer {

    public static void main(String[] args) throws Exception{
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.2.121");
        connectionFactory.setPort(ConnectionFactory.DEFAULT_AMQP_PORT); // 5672
        connectionFactory.setVirtualHost("/dzz"); // 虚拟主机? 默认值 /
        connectionFactory.setUsername("test"); // guest
        connectionFactory.setPassword("123456"); // guest

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        /**
         *  多了一个创建交换机的过程
         *  public DeclareOk exchangeDeclare(String exchange, BuiltinExchangeType type, boolean durable, boolean autoDelete, boolean internal, Map<String, Object> arguments) throws IOException {
         *      return this.exchangeDeclare(exchange, type.getType(), durable, autoDelete, internal, arguments);
         *  }
         *  String exchange 交换机名称
         *  String type 交换机类型,这里换成枚举类型,方便查找 com.rabbitmq.client.BuiltinExchangeType
         *      DIRECT("direct"), 定向  简单模式 和 工作模式
         *      FANOUT("fanout"), 扇形  广播(通知给所有和这个交换机绑定的队列)
         *      TOPIC("topic"), 通配符 ?
         *      HEADERS("headers"); 参数匹配, 视频暂不讲解
         *  boolean durable 持久化
         *  boolean autoDelete 自动删除
         *  boolean internal 内部使用 一般false
         *  Map<String, Object> arguments
         *
         *
        */
        String exchangeName = "test_fanout";
        channel.exchangeDeclare(
                exchangeName,
                BuiltinExchangeType.FANOUT,
                true,
                false,
                false,
                null
        );

        // 订阅模式案例需要两个队列支持
        String queueName1 = "pub&sub - 1";
        String queueName2 = "pub&sub - 2";
        channel.queueDeclare(queueName1, true, false, false, null);
        channel.queueDeclare(queueName2, true, false, false, null);

        /**
         * 将交换机和队列绑定
         *  public com.rabbitmq.client.AMQP.Queue.BindOk queueBind(String queue, String exchange, String routingKey) throws IOException {
         *      return this.queueBind(queue, exchange, routingKey, (Map)null);
         *  }
         *  String queue 队列名称
         *  String exchange 交换机名称
         *  String routingKey 路由键,绑定规则
         *      如果是fanout模式, 设置""即可,默认就是给所有队列绑定
         *
         */
        channel.queueBind(queueName1, exchangeName, "");
        channel.queueBind(queueName2, exchangeName, "");

        // 发送消息
         for (int i = 0; i < 10; i++) {
             String body = "sending sub&pub msg " + i;
             // 现在反过来, 交换机名字有了, 路由则默认了
             channel.basicPublish(exchangeName, "", null, body.getBytes(StandardCharsets.UTF_8));
         }

        // 释放资源
         channel.close();
         connection.close();
    }

}

这样每个队列都有10条待送出的消息

创建两个订阅的消费者:

两个消费者接收都是一样的,注意切换队列名字

package cn.dzz.pubSubQueue;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;

public class PubSubQueueInConsumer1 {

    /**
     * 工作队列 消费者
     * @param args
     */
    public static void main(String[] args) throws Exception{
        ConnectionFactory connectionFactory = new ConnectionFactory();

        connectionFactory.setHost("192.168.2.121");
        connectionFactory.setPort(ConnectionFactory.DEFAULT_AMQP_PORT); // 5672
        connectionFactory.setVirtualHost("/dzz"); // 虚拟主机? 默认值 /
        connectionFactory.setUsername("test"); // guest
        connectionFactory.setPassword("123456"); // guest

        Connection connection = connectionFactory.newConnection();
        Channel channel = connection.createChannel();

        // 声明将不在需要
        // channel.queueDeclare("work_queue", true, false, false, null);

        // 从生产者复制过来需要的队列名称
        String queueName1 = "pub&sub - 1";
        String queueName2 = "pub&sub - 2";

        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("body(message) " + new String(body, StandardCharsets.UTF_8));
                System.out.println("- - - - - over - - - - -");
            }
        };

        channel.basicConsume(queueName1, true, consumer);
    }
}

订阅的消费者1和2都能收到同样的消息:

"C:\Program Files (x86)\Java\jdk1.8.0_291\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.1\lib\idea_rt.jar=58116:C:\Program Files\JetBrains\IntelliJ IDEA 2021.2.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\charsets.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\deploy.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\access-bridge-32.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\cldrdata.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\dnsns.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\jaccess.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\jfxrt.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\localedata.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\nashorn.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunec.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunjce_provider.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunmscapi.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\sunpkcs11.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\ext\zipfs.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\javaws.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jce.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jfr.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jfxswt.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\jsse.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\management-agent.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\plugin.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\resources.jar;C:\Program Files (x86)\Java\jdk1.8.0_291\jre\lib\rt.jar;C:\Users\Administrator\IdeaProjects\RabbitMQ\ConsumerService\target\classes;C:\Users\Administrator\.m2\repository\com\rabbitmq\amqp-client\5.6.0\amqp-client-5.6.0.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.7.25\slf4j-api-1.7.25.jar" cn.dzz.pubSubQueue.PubSubQueueInConsumer2
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
body(message) sending sub&pub msg 0
- - - - - over - - - - -
body(message) sending sub&pub msg 1
- - - - - over - - - - -
body(message) sending sub&pub msg 2
- - - - - over - - - - -
body(message) sending sub&pub msg 3
- - - - - over - - - - -
body(message) sending sub&pub msg 4
- - - - - over - - - - -
body(message) sending sub&pub msg 5
- - - - - over - - - - -
body(message) sending sub&pub msg 6
- - - - - over - - - - -
body(message) sending sub&pub msg 7
- - - - - over - - - - -
body(message) sending sub&pub msg 8
- - - - - over - - - - -
body(message) sending sub&pub msg 9
- - - - - over - - - - -

 

posted @ 2021-10-06 19:44  emdzz  阅读(71)  评论(0编辑  收藏  举报