【rabbitmq】工作模式之通配符模式

文章展示了如何在Java中使用RabbitMQ实现生产者和两个消费者,利用TOPIC交换机进行消息路由。生产者声明了一个持久化的交换机,并绑定了两个队列,消费者1监听特定路由键的消息,消费者2则接收所有消息。
摘要由CSDN通过智能技术生成

1.图解

在这里插入图片描述

2.生产者
package com.producer;

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

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * @Author wangyouhui
 * @Description TODO
 **/
public class Producer_Topics {
    public static void main(String[] args) throws IOException, TimeoutException {
        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 2.设置参数
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/learning");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        // 3.创建连接
        Connection connection = connectionFactory.newConnection();
        // 4.创建channel
        Channel channel = connection.createChannel();
        // 5.创建交换机

        /**
         * 1.exchange:交换机名称
         * 2.type:交换机类型
         *     DIRECT("direct"):定向类型
         *     FANOUT("fanout"):扇形(广播),发送消息到每一个与之绑定的队列
         *     TOPIC("topic"):通配符的方式
         *     HEADERS("headers"):参数匹配
         * 3.durable:是否持久化
         * 4.autoDelete:是否自动删除
         * 5.internal:内部使用。一般false
         * 6.arguments:参数
         */
        String exchangeName = "test_topic";
        channel.exchangeDeclare(exchangeName, BuiltinExchangeType.TOPIC, true, false, false, null);
        // 6.创建队列
        String queue1 = "test_topic_queue_1";
        String queue2 = "test_topic_queue_2";
        channel.queueDeclare(queue1, true, false, false, null);
        channel.queueDeclare(queue2, true, false, false, null);
        // 7.绑定队列和交换机
        /**
         * 1.queue:队列名称
         * 2.exchange:交换机名称
         * 3.routingKey:路由键,绑定规则
         *      如果交换机的类型为fanout, routingKey设置为""
         */
        channel.queueBind(queue1, exchangeName, "#.error");
        channel.queueBind(queue1, exchangeName, "order.*");
        channel.queueBind(queue2, exchangeName, "*.*");
        // 8.发送消息
        String body = "发布订阅-扇形类型";
        channel.basicPublish(exchangeName,"order.error",null,body.getBytes());
        // 9.释放资源
        channel.close();
        connection.close();
    }
}


3.消费者
a.消费者1
package com.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author wangyouhui
 * @Description 消费者1
 **/
public class Consumer_Topic_1 {
    public static void main(String[] args) throws Exception {
        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 2.设置参数
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/learning");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        // 3. 创建连接 Connection
        Connection connection = connectionFactory.newConnection();
        // 4. 创建channel
        Channel channel = connection.createChannel();

        String queue1 = "test_topic_queue_1";

        // 6. 消费消息
        Consumer consumer = new DefaultConsumer(channel){
            // 回调方法,当收到消息后,会自动执行该方法

            /**
             * @param consumerTag  标识
             * @param envelope  获取一些信息,交换机、路由key
             * @param properties    配置信息
             * @param body  数据
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//               super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("消费者1打印");
                System.out.println("body:" + new String(body));
            }
        };
        channel.basicConsume(queue1, true, consumer);

    }
}

b.消费者2
package com.consumer;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @Author wangyouhui
 * @Description 消费者1
 **/
public class Consumer_Topic_2 {
    public static void main(String[] args) throws Exception {
        // 1.创建连接工厂
        ConnectionFactory connectionFactory = new ConnectionFactory();
        // 2.设置参数
        connectionFactory.setHost("127.0.0.1");
        connectionFactory.setPort(5672);
        connectionFactory.setVirtualHost("/learning");
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        // 3. 创建连接 Connection
        Connection connection = connectionFactory.newConnection();
        // 4. 创建channel
        Channel channel = connection.createChannel();

        String queue2 = "test_topic_queue_2";

        // 6. 消费消息
        Consumer consumer = new DefaultConsumer(channel){
            // 回调方法,当收到消息后,会自动执行该方法

            /**
             * @param consumerTag  标识
             * @param envelope  获取一些信息,交换机、路由key
             * @param properties    配置信息
             * @param body  数据
             * @throws IOException
             */
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
//               super.handleDelivery(consumerTag, envelope, properties, body);
                System.out.println("消费者1打印");
                System.out.println("body:" + new String(body));
            }
        };
        channel.basicConsume(queue2, true, consumer);

    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王佑辉

老板,赏点吧

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值