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中。