實現功能:一條消息發送給多個消費者java
交換機模式:topicide
相比於direct匹配模式,匹配routingKey時,topic模式下不只支持徹底匹配,還支持兩種特殊的匹配方式debug
#:能夠匹配一個或多個字符3d
*:能夠匹配一個字符code
生產者:blog
package com.example.demo.queue.exchangeToQueue.topic; import java.io.IOException; import java.util.concurrent.TimeoutException; import com.example.demo.utils.ConnectionUtil; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; public class Producer { private static final String EXCHANGE_NAME = "exchange_topic"; public static void main(String[] args) { Connection connection = null; Channel channel = null; try { // 獲取鏈接 connection = ConnectionUtil.getConnection(); // 建立通道 channel = connection.createChannel(); // 聲明交換機 channel.exchangeDeclare(EXCHANGE_NAME, "topic"); String routingKey = "debug"; // 生產者發送的信息 String msg = "routingKey="+routingKey; System.out.println("send msg : "+msg); // 發送信息 channel.basicPublish(EXCHANGE_NAME, routingKey, null, msg.getBytes()); } catch (Exception e) { e.printStackTrace(); } finally { // 關閉通道 try { channel.close(); } catch (IOException e) { e.printStackTrace(); } catch (TimeoutException e) { e.printStackTrace(); } // 關閉鏈接 try { connection.close(); } catch (IOException e) { e.printStackTrace(); } } } }
消費者1:rabbitmq
package com.example.demo.queue.exchangeToQueue.topic; import java.io.IOException; import com.example.demo.utils.ConnectionUtil; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer01 { private static final String EXCHANGE_NAME = "exchange_topic"; private static final String QUEUE_NAME = "topic_exchange_to_queue_01"; public static void main(String[] args) { try { // 獲取鏈接 Connection connection = ConnectionUtil.getConnection(); // 建立通道 final Channel channel = connection.createChannel(); // 聲明隊列 channel.queueDeclare(QUEUE_NAME, true, false, false, null); String routingKey = "debug"; // 綁定隊列到交換機 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey); // 定義消費者 DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("[1]:receive msg:"+msg); System.out.println("[1]:deal msg successful. routingKey="+routingKey); } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } } }
消費者2:隊列
package com.example.demo.queue.exchangeToQueue.topic; import java.io.IOException; import com.example.demo.utils.ConnectionUtil; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer02 { private static final String EXCHANGE_NAME = "exchange_topic"; private static final String QUEUE_NAME = "topic_exchange_to_queue_02"; public static void main(String[] args) { try { // 獲取鏈接 Connection connection = ConnectionUtil.getConnection(); // 建立通道 final Channel channel = connection.createChannel(); // 聲明隊列 channel.queueDeclare(QUEUE_NAME, true, false, false, null); String routingKey = "debug.*"; // 綁定隊列到交換機 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey); // 定義消費者 DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("[2]:receive msg:"+msg); System.out.println("[2]:deal msg successful. routingKey="+routingKey); } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } } }
消費者3:get
package com.example.demo.queue.exchangeToQueue.topic; import java.io.IOException; import com.example.demo.utils.ConnectionUtil; import com.rabbitmq.client.AMQP.BasicProperties; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.DefaultConsumer; import com.rabbitmq.client.Envelope; public class Consumer03 { private static final String EXCHANGE_NAME = "exchange_topic"; private static final String QUEUE_NAME = "topic_exchange_to_queue_03"; public static void main(String[] args) { try { // 獲取鏈接 Connection connection = ConnectionUtil.getConnection(); // 建立通道 final Channel channel = connection.createChannel(); // 聲明隊列 channel.queueDeclare(QUEUE_NAME, true, false, false, null); String routingKey = "debug.#"; // 綁定隊列到交換機 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, routingKey); // 定義消費者 DefaultConsumer consumer = new DefaultConsumer(channel) { @Override public void handleDelivery(String consumerTag, Envelope envelope, BasicProperties properties, byte[] body) throws IOException { String msg = new String(body,"UTF-8"); System.out.println("[3]:receive msg:"+msg); System.out.println("[3]:deal msg successful. routingKey="+routingKey); } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } } }
和以前同樣,涉及到了交換機,而在消費者類中中並無聲明交換機,因此須要先執行生產者類的main方法it
接下來,咱們就能夠依次執行三個消費者類的main方法,以後再執行生產者類的main方法。
生產者指定的routingKey爲【debug】,而消費者指定的routingKey依次爲【debug】【debug.*】【debug.#】,理論上應該只有消費者1才能收到消息。下面來展現結果:
消費者1終端:
消費者2終端:
消費者3終端:
我也看不懂了,消費者3怎麼也收到了消息,暈那。。。。。。。。