RabbitMq学习(6) 灵活动态的Topic(主题)动态路由模型

一、动态路由模型

topic模型中, 路由名由 . 和被其隔开的 路由名称 所组成 例如aaa.bbb.ccc, 在动态路由模式下, 通过通配符来动态的指定路由。其包含两种通配符*#

  1. *表示匹配一个单词
  2. #表示匹配一个或多个由.隔开的单词
  3. *# 可以在任意位置

其匹配示例如下图
在这里插入图片描述

二、代码实现

①生产者:
  1. 绑定通道和交换机, 声明交换机的名称和类型(不存在时创建)。
  2. 通过通道,指定消息发布的交换机, 消息的路由规则,其他的配置参数以及消息本体。
public class Provider extends Thread {
    @Override
    public void run() {
        Connection connection = null;
        Channel channel = null;
        try {
            connection = RabbitMqUtil.getConnection();
            channel = connection.createChannel();

            //通道绑定交换机, 声明交换机名称和类型
            channel.exchangeDeclare("exchange-topic","topic");

            //指定交换机,路由,配置参数,消息体
            channel.basicPublish("exchange-topic","goods.phone",null,"sell phone".getBytes());
            channel.basicPublish("exchange-topic","goods.pc",null,"sell pc".getBytes());
            channel.basicPublish("exchange-topic","goods.pad",null,"sell pad".getBytes());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            RabbitMqUtil.closeChannelAndConnection(channel,connection);
        }
    }
}
②消费者
  1. 通道绑定交换机, 声明交换机名称和类型
  2. 通过String queue = channel.queueDeclare().getQueue() 生成临时队列并获取消息队列名称
  3. 通过通道,绑定队列,交换机以及配置路由规则, 可以重复配置,和direct模式一样, 但确实没有必要。

PcCustomer代码:

public class PcCustomer extends Thread {

    @Override
    public void run() {
        Connection connection = null;
        Channel channel = null;
        
        try {
            connection = RabbitMqUtil.getConnection();
            channel = connection.createChannel();
            
            channel.exchangeDeclare("exchange-topic", "topic");

            String queue = channel.queueDeclare().getQueue();

            channel.queueBind(queue, "exchange-topic", "goods.pc");
            channel.queueBind(queue, "exchange-topic", "goods.pad");

            channel.basicConsume(queue, true , new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("PcCustomer 消费了:" + new String(body));
                }
            });


        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

TotalCustomer代码:

public class TotalCustomer extends Thread {

    @Override
    public void run() {
        Connection connection = null;
        Channel channel = null;
        try {
            connection = RabbitMqUtil.getConnection();
            channel  = connection.createChannel();

            channel.exchangeDeclare("exchange-topic", "topic");

            //获取临时队列
            String queue = channel.queueDeclare().getQueue();

            //绑定临时队列和交换机,声明路由规则(可以同时声明多个), *号匹配1个单词, 该消费者可以消费,goods的所有子路由
            channel.queueBind(queue, "exchange-topic", "goods.*");

            //消费消息
            channel.basicConsume(queue, true , new DefaultConsumer(channel){
                @Override
                public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                    System.out.println("TotalCustomer: 消费了:"+ new String(body));
                }
            });

        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
Main 启动类

还是要确保消费者先于生产者启动的, 这样交换机在接受到生产者发送的消息时,不会因为没有队列关注某一路由而导致这些消息丢失。

public class Main {
    public static void main(String[] args) throws InterruptedException {
        Thread pc = new PcCustomer();
        Thread total = new TotalCustomer();
        pc.start();
        total.start();

        //确定消费者先于生产者启动

        Thread.sleep(2000);
        Thread provider = new Provider();
        provider.start();
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值