一、动态路由模型
topic模型中, 路由名由 .
和被其隔开的 路由名称
所组成 例如aaa.bbb.ccc
, 在动态路由模式下, 通过通配符来动态的指定路由。其包含两种通配符*
和 #
*
表示匹配一个单词#
表示匹配一个或多个由.
隔开的单词*
和#
可以在任意位置
其匹配示例如下图
二、代码实现
①生产者:
- 绑定通道和交换机, 声明交换机的名称和类型(不存在时创建)。
- 通过通道,指定消息发布的交换机, 消息的路由规则,其他的配置参数以及消息本体。
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);
}
}
}
②消费者
- 通道绑定交换机, 声明交换机名称和类型
- 通过
String queue = channel.queueDeclare().getQueue()
生成临时队列并获取消息队列名称 - 通过通道,绑定队列,交换机以及配置路由规则, 可以重复配置,和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();
}
}