路由模式(Routing):路由模式跟发布订阅模式类似,然后在发布订阅模式的基础上改变了交换机类型(fanout => direct),订阅模式是分发到所有绑定到交换机的队列,路由模式只分发到绑定在交换机上面指定路由key的队列上。
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 = "direct-exchange";
String type = "direct";
channel.exchangeDeclare(exchangeName,type,true);
//创建队列
// 队列名称,是否持久化,是否独占,是否自动删除(当最后一条消息消费完毕后自动删除),附加属性
channel.queueDeclare("direct-queue1", true, false, false, null);
channel.queueDeclare("direct-queue2", true, false, false, null);
channel.queueDeclare("direct-queue3", true, false, false, null);
channel.queueBind("direct-queue1",exchangeName,"sms");
channel.queueBind("direct-queue2", exchangeName,"wechat");
channel.queueBind("direct-queue3",exchangeName,"sms");
// 发送消息到队列
channel.basicPublish(exchangeName, "sms", 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(),"direct-queue1").start();
new Thread(new C(),"direct-queue2").start();
new Thread(new C(),"direct-queue3").start();
}
}
4. 以上代码运行结果,可以看出只有 direct-queue1和direct-queue3收到消息,所以路由模式,只有路由key匹配的队列才能收到消息,其他的收不到