topic类型的交换器允许在RabbitMQ中使用模糊匹配来绑定自己感兴趣的信息
通过匹配交换器,我们可以配置更灵活的消息系统
匹配交换器的匹配符
*(星号)表示一个单词
#(井号)表示零个或者多个单词
这次的例子中,我们使用三个段式的路由关键字,有三个单词和两个点组成。
第一个词是速度,第二个词是颜色,第三个是动物名称
三个关键字来绑定,消费者C1绑定关键字是【*.orange.*】,消费者C2绑定关键字是【*.*.rabbit】和【lazy.#】
消费者C1会收到所有orange这种颜色相关的消息
消费者C2会收到所有rabbit这个动物相关的消息和所有速度lazy的动物的消息
交换器在匹配模式下:
如果消费者端的路由关键字只使用【#】来匹配消息,在匹配【topic】模式下,它会变成一个分发【fanout】模式,接收所有消息。
如果消费者端的路由关键字中没有【#】或者【*】,它就变成直连【direct】模式来工作。
生产者
import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; public class Producer { private static final String EXCHANGE_NAME = "myexchange"; // 路由关键字 private static final String[] routingKeys = new String[] { "quick.orange.rabbit", "lazy.orange.elephant", "quick.orange.fox", "lazy.brown.fox", "quick.brown.fox", "quick.orange.male.rabbit", "lazy.orange.male.rabbit" }; public static void main(String[] argv) throws Exception { Connection connection = null; Channel channel = null; try { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); connection = factory.newConnection(); channel = connection.createChannel(); // 声明一个匹配模式的交换器 channel.exchangeDeclare(EXCHANGE_NAME, "topic"); // 发送消息 for (String severity : routingKeys) { String message = "From " + severity + " routingKey‘ s message!"; channel.basicPublish(EXCHANGE_NAME, severity, null, message.getBytes()); System.out.println("p Sent ‘" + severity + "‘:‘" + message + "‘"); } } catch (Exception e) { e.printStackTrace(); } finally { if (connection != null) { try { connection.close(); } catch (Exception ignore) { } } } } }
消费者1
import java.io.IOException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer1 { // 交换器名称 private static final String EXCHANGE_NAME = "myexchange"; // 路由关键字 private static final String[] routingKeys = new String[] { "*.orange.*" }; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 声明一个匹配模式的交换器 channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); // 绑定路由关键字 for (String bindingKey : routingKeys) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); System.out.println("ReceiveTopic1 exchange:" + EXCHANGE_NAME + ", queue:" + queueName + ", BindRoutingKey:" + bindingKey); } System.out.println("Consumer1 Waiting for messages..."); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("Consumer1 Received ‘" + envelope.getRoutingKey() + "‘:‘" + message + "‘"); } }; channel.basicConsume(queueName, true, consumer); } }
消费者2
import java.io.IOException; import com.rabbitmq.client.AMQP; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import com.rabbitmq.client.Consumer; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer2 { // 交换器名称 private static final String EXCHANGE_NAME = "myexchange"; // 路由关键字 private static final String[] routingKeys = new String[]{"*.*.rabbit", "lazy.#"}; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory(); factory.setHost("localhost"); Connection connection = factory.newConnection(); Channel channel = connection.createChannel(); // 声明一个匹配模式的交换器 channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String queueName = channel.queueDeclare().getQueue(); // 绑定路由关键字 for (String bindingKey : routingKeys) { channel.queueBind(queueName, EXCHANGE_NAME, bindingKey); System.out.println("ReceiveTopic2 exchange:" + EXCHANGE_NAME + ", queue:" + queueName + ", BindRoutingKey:" + bindingKey); } System.out.println("Consumer2 Waiting for messages..."); Consumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException { String message = new String(body, "UTF-8"); System.out.println("Consumer2 Received ‘" + envelope.getRoutingKey() + "‘:‘" + message + "‘"); } }; channel.basicConsume(queueName, true, consumer); } }
生产者发送“quick.orange.rabbit”的消息,两个消费者都会收到。
生产者发送“lazy.orange.elephant”,两个消费者都会收到。
生产者发送"quick.orange.fox",那么只有C1能收到。
生产者发送"lazy.brown.fox",那么只有C2能收到。
生产者发送"quick.brown.fox",那么这条消息会被丢弃,谁也收不到。
生产者发送"quick.orange.male.rabbit",这个消息也会被丢弃,谁也收不到。
生产者发送"lazy.orange.male.rabbit",这个消息会被C2的【lazy.#】规则匹配上,发送到C2中。
原文地址:https://www.cnblogs.com/zengnansheng/p/10389660.html