Topic模式:主题模式

本文介绍了RabbitMQ中Topic交换机的工作原理,通过web端和代码操作展示了如何新建队列、交换机及绑定,并探讨了路由Key的含义。在测试中发现,路由Key使用#和*作为通配符,其中#匹配任意级目录,*匹配一级目录。生产者和消费者代码示例分别展示了如何发送和接收消息,强调了路由Key在消息路由中的关键作用。
摘要由CSDN通过智能技术生成

图解

在这里插入图片描述

1、web端操作

1、新建3个队列

在这里插入图片描述

2、新建一个交换机

在这里插入图片描述

3、交换机绑定队列

在这里插入图片描述

这里的路由key是什么意思呢?留一个悬念

4、测试发送消息

发送消息前:

在这里插入图片描述

发送消息:
在这里插入图片描述
在这里插入图片描述

再次测试
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

经过多次测试发现了规律吗?

5、小结

#:任意级目录,任意目录什么意思呢?就是可以为空也可以多个 举例: .com.sky 或者 com.com.com.sky

*:一级目录,一级就是com。就没了

2、代码操作

生产者

package com.sky.rabbitmq.topics;

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

/**
 * @author 尹稳健~
 * @version 1.0
 */
public class Producer {
    public static void main(String[] args) {
        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("admin");
        connectionFactory.setPassword("admin");
        connectionFactory.setVirtualHost("/");
        Connection connection = null;
        Channel channel = null;
        try {
            connection = connectionFactory.newConnection();
            channel = connection.createChannel();
            // 消息
            String message = "谢谢你发消息给我";
            // 交换机名字
            String exchange_name = "topic-exchange";
            // 路由key
            String routeKey = "com.user.sky";
            // 交换机类型
            String type = "topic";
            // 发送消息给中间件rabbitmq-server
            // @params1: 交换机exchange
            // @params2: 队列名称/routingkey
            // @params3: 属性配置
            // @params4: 发送消息的内容
            channel.basicPublish(exchange_name,routeKey,null,message.getBytes());
            System.out.println("消息发送成功!");
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("发送消息出现异常...");
        }  finally {
            if (channel != null && channel.isOpen()) {
                try {
                    channel.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
            if (connection != null) {
                try {
                    connection.close();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
    }
}

消费者

package com.sky.rabbitmq.topics;

import com.rabbitmq.client.*;

import java.io.IOException;

/**
 * @author 尹稳健~
 * @version 1.0
 */
public class Consumer {
    private static Runnable runnable = new Runnable() {
        public void run() {
            ConnectionFactory connectionFactory = new ConnectionFactory();
            connectionFactory.setHost("localhost");
            connectionFactory.setPort(5672);
            connectionFactory.setUsername("admin");
            connectionFactory.setPassword("admin");
            connectionFactory.setVirtualHost("/");
            //获取队列的名称
            final String queueName = Thread.currentThread().getName();
            Connection connection = null;
            Channel channel = null;
            try {
                connection = connectionFactory.newConnection();
                channel = connection.createChannel();
                // 交换机名字
                String exchange_name = "topic-exchange";
                // 路由key
                String routeKey = "";
                // 交换机类型
                String type = "topic";
                // 6: 定义接受消息的回调
                channel.basicConsume(queueName, true, new DeliverCallback() {
                    public void handle(String s, Delivery delivery) throws IOException {
                        System.out.println(queueName + ":收到消息是:" + new String(delivery.getBody(), "UTF-8"));
                    }
                }, new CancelCallback() {
                    public void handle(String s) throws IOException {
                        System.out.println("异常");
                    }
                });
                System.out.println(queueName + ":开始接受消息");
                System.in.read();
            } catch (Exception e) {
                e.printStackTrace();
                System.out.println("发送消息出现异常...");
            } finally {
                if (channel != null && channel.isOpen()) {
                    try {
                        channel.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
                if (connection != null) {
                    try {
                        connection.close();
                    } catch (Exception ex) {
                        ex.printStackTrace();
                    }
                }
            }
        }
    };

    public static void main(String[] args) {
        new Thread(runnable,"queue1").start();
        new Thread(runnable,"queue2").start();
        new Thread(runnable,"queue3").start();
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

毕竟尹稳健

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值