主题模式(topic):主题模式其实就是在路由模式的基础上改变了交换机类型(direct => topic),支持了对key的通配符匹配(星号以及井号),以满足更加复杂的消息分发场景。
* 代表任意 一个单词
# 0个或者多个单词
P:消息的生产者
C:消息的消费者
红色:队列
X:交换机
1. pom.xml引入rabbitmq依赖
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.13.1</version>
</dependency>
2. 生产者
public class Producer {
public static void main(String[] args) {
// 创建链接
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("admin");
connectionFactory.setPassword("123456");
connectionFactory.setVirtualHost("/");
// 创建Connection
Connection connection = null;
Channel channel = null;
try {
connection = connectionFactory.newConnection("生产者");
channel = connection.createChannel();
String message = "hello message";
String exchangeName = "topic-exchange";
String type = "topic";
channel.exchangeDeclare(exchangeName,type,true);
//创建队列
// 队列名称,是否持久化,是否独占,是否自动删除(当最后一条消息消费完毕后自动删除),附加属性
channel.queueDeclare("topic-queue1", true, false, false, null);
channel.queueDeclare("topic-queue2", true, false, false, null);
channel.queueDeclare("topic-queue3", true, false, false, null);
// * 代表任意 一个单词
// # 0个或者多个单词
channel.queueBind("topic-queue1",exchangeName,"#.wechat");
channel.queueBind("topic-queue2", exchangeName,"*.sms.#");
channel.queueBind("topic-queue3",exchangeName,"*.sms.*");
// 发送消息到队列
channel.basicPublish(exchangeName, "wechat.sms.a", null, message.getBytes(StandardCharsets.UTF_8));
System.out.println("消息发送成功");
} catch (IOException | TimeoutException e) {
e.printStackTrace();
} finally {
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
if (connection != null && connection.isOpen()) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
3. 消费者
public class Consumer {
public static class C implements Runnable {
@Override
public void run() {
Thread thread = Thread.currentThread();
// 创建链接
ConnectionFactory connectionFactory = new ConnectionFactory();
connectionFactory.setHost("127.0.0.1");
connectionFactory.setPort(5672);
connectionFactory.setUsername("admin");
connectionFactory.setPassword("123456");
connectionFactory.setVirtualHost("/");
// 创建Connection
Connection connection = null;
Channel channel = null;
try {
connection = connectionFactory.newConnection("消费者");
channel = connection.createChannel();
String queueName = thread.getName();
// 监听发送来的消息
// 队列名称,是否自动确认消息,接收消息的回调,消费者取消时的回调
channel.basicConsume(queueName, true,
(consumerTag, message) -> System.out.println(queueName+":收到消息:" + new String(message.getBody(), StandardCharsets.UTF_8)),
consumerTag -> System.out.println("消费者取消订阅"));
while (true) {
//阻塞进程, 防止消费者程序退出
}
} catch (IOException | TimeoutException e) {
e.printStackTrace();
} finally {
if (channel != null && channel.isOpen()) {
try {
channel.close();
} catch (IOException | TimeoutException e) {
e.printStackTrace();
}
}
if (connection != null && connection.isOpen()) {
try {
connection.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
public static void main(String[] args) {
new Thread(new C(),"topic-queue1").start();
new Thread(new C(),"topic-queue2").start();
new Thread(new C(),"topic-queue3").start();
}
}
4. 以上代码运行结果,可以看出只有 topic-queue2和topic-queue3收到消息,所有可以看出主题模式是在路由模式上增加了模糊匹配。