本节主要演示交换机的另一种类型:主题类型topic,直连接类型direct必须是生产者发布消息指定的routingKey和消费者在队列绑定时指定的routingKey完全相等时才能匹配到队列上,与direct不同,topic可以进行模糊匹配,可以使用星号*和井号#这两个通配符来进行模糊匹配,其中星号可以代替一个单词;主题类型的转发器的消息不能随意的设置选择键(routing_key),必须是由点隔开的一系列的标识符组成。标识符可以是任何东西,但是一般都与消息的某些特性相关。一些合法的选择键的例子:”quick.orange.rabbit”,你可以定义任何数量的标识符,上限为255个字节。 #井号可以替代零个或更多的单词,只要能模糊匹配上就能将消息映射到队列中。当一个队列的绑定键为#的时候,这个队列将会无视消息的路由键,接收所有的消息。
@Test
public void produce() throws IOException, TimeoutException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(AMQP.PROTOCOL.PORT);
factory.setUsername("guest");
factory.setPassword("guest");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
// 声明一个主题交换机
String EXCHANGE_NAME = "exchange.topic.x";
// 声明一个路由键数组,和主题交换机绑定
String[] routings = {"quick.orange.rabbit", "lazy.orange.elephant", "mq.erlang.rabbit", "lazy.brown.fox", "lazy."};
// 通过主题交换机,循环发布消息到每一个路由键上
for (String routingKey : routings) {
String message = "RabbitMQ " + routingKey;
channel.basicPublish(EXCHANGE_NAME, routingKey, null, message.getBytes());
}
channel.close();
conn.close();
}
@Test
public void consume1() throws IOException, TimeoutException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(AMQP.PROTOCOL.PORT);
factory.setUsername("guest");
factory.setPassword("guest");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
// 声明一个主题交换机
String EXCHANGE_NAME = "exchange.topic.x";
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
// 声明一个队列
String QUEUE_NAME = "queue.topic.q2";
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 声明一个路由键数组
String[] routingKeys = {"*.*.rabbit", "lazy.#"};
// 循环绑定队列、交换机、路由键
for (int i = 0; i < routingKeys.length; i++) {
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKeys[i]);
}
System.out.println("Consumer1 Wating Receive Message");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println("[C] received " + message);
}
};
channel.basicConsume(QUEUE_NAME, false, consumer);
Thread.sleep(1000000);
}
@Test
public void consume2() throws IOException, TimeoutException, InterruptedException {
ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(AMQP.PROTOCOL.PORT);
factory.setUsername("guest");
factory.setPassword("guest");
Connection conn = factory.newConnection();
Channel channel = conn.createChannel();
// 声明一个主题交换机
String EXCHANGE_NAME = "exchange.topic.x";
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.TOPIC);
// 声明一个队列
String QUEUE_NAME = "queue.topic.q1";
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 声明一个路由键数组
String[] routingKeys = {"*.orange.*"};
// 循环绑定队列、交换机、路由键
for (int i = 0; i < routingKeys.length; i++) {
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKeys[i]);
}
System.out.println("Consumer2 Wating Receive Message");
Consumer consumer = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body)
throws IOException {
String message = new String(body, "UTF-8");
System.out.println("[C] received " + message);
}
};
channel.basicConsume(QUEUE_NAME, false, consumer);
Thread.sleep(1000000);
}
先启动消费者,再启动生产者。
有的消息可以满足多个模糊的路由键,具体会路由到哪个队列,关于同时满足条件以后再研究。
下面是一张topic的图,描述的比较准确