RabbitMQ的七种工作模式-主题模式(五)

5.主题模式

在这里插入图片描述

主题模式对应交换机是Topic和Headers,接收者的routeKey对应如果只有一个单词,如topickey,topic*/topic#是无法匹配上的,应有”.“隔开的这种键值。

*:有且只匹配一个单词

#:0或多个单词

生产者:

    import com.rabbitmq.client.BuiltinExchangeType;
    import com.rabbitmq.client.Channel;
    import com.rabbitmq.client.Connection;
    import com.rabbitmq.client.ConnectionFactory;
    
    /**
     * @ClassName : Publisher
     * @Description : 生产者
     * @Author : Becolette
     * @Date: 2020/9/27  23:08
     */
    
    public class TopicSent {
    
        private static final String EXCHANGE_NAME = "topic_exchange";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
    
            Channel channel = connection.createChannel();
    
            /**
             * @param exchange the name of the exchange
             *                 交换机名称
             * @param type the exchange type
             *             交换机类型
             */        
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
    
            String message = argv.length < 1 ? "info: Hello World!" : String.join(" ", argv);
    
            /**
             * @param exchange the exchange to publish the message to
             *                 交换机
             * @param routingKey the routing key
             *                   连接键
             * @param props other properties for the message - routing headers etc
             *              消息其他属性
             * @param body the message body
             *             消息体
             */
            channel.basicPublish(EXCHANGE_NAME, "aa.topic.aa", null, message.getBytes("UTF-8"));
    
            System.out.println(" [x] Sent '" + message + "'");
        }
    
    }

消费者:

    import com.rabbitmq.client.*;
    
    /**
     * @ClassName : Subscriber
     * @Description : 消费者
     * @Author : Becolette
     * @Date: 2020/9/27  23:09
     */
    
    public class TopicRecv {
    
        private static final String EXCHANGE_NAME = "topic_exchange";
    
        public static void main(String[] argv) throws Exception {
            ConnectionFactory factory = new ConnectionFactory();
            factory.setHost("localhost");
            Connection connection = factory.newConnection();
    
            Channel channel = connection.createChannel();
            // 此处如果用BuiltinExchangeType.HEADERS,对应sent也相应修改,只不过channel.queueBind中的routeKey需要修改成aa.*.*或aa.*.#或aa.#.#或aa.#.*
            channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
            /**
             * 由于交换机采用的是群发,也就是广播的形式,所以自动生成queue
             * 并声明binding
             */
            String queueName = channel.queueDeclare().getQueue();
            channel.queueBind(queueName, EXCHANGE_NAME, "*.topic.#");
    
            System.out.println(" [*] Waiting for messages. To exit press CTRL+C");
    
            DeliverCallback deliverCallback = (consumerTag, delivery) -> {
                String message = new String(delivery.getBody(), "UTF-8");
                System.out.println(" [x] Received '" + message + "'");
            };
            channel.basicConsume(queueName, true, deliverCallback, consumerTag -> {
            });
        }
    
    }
©️2020 CSDN 皮肤主题: 数字20 设计师:CSDN官方博客 返回首页