实现功能:一条消息发送给多个消费者java
交换机模式:directide
相比于以前的fanout模式,能够进一步的筛选获取消息的消费者。debug
fanout模式下,只要消费者监听的队列,已经与接收生产者消息的交换机绑定,那消费者就能收到消息。code
direct模式下,消费者监听的队列,不只须要已经与接收生产者消息的交换机进行绑定,并且绑定交换机时指定的routingKey要匹配生产者发送消息时指定的routingKey(彻底匹配)。blog
生产者:rabbitmq
package com.example.demo.queue.exchangeToQueue.direct; 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_direct"; public static void main(String[] args) { Connection connection = null; Channel channel = null; try { // 获取链接 connection = ConnectionUtil.getConnection(); // 建立通道 channel = connection.createChannel(); // 声明交换机 channel.exchangeDeclare(EXCHANGE_NAME, "direct"); 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:队列
package com.example.demo.queue.exchangeToQueue.direct; 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_direct"; private static final String QUEUE_NAME = "direct_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); // 绑定队列到交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info"); // 定义消费者 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."); } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } } }
消费者2:get
package com.example.demo.queue.exchangeToQueue.direct; 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_direct"; private static final String QUEUE_NAME = "direct_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); // 绑定队列到交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info"); channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error"); // 定义消费者 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."); } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } } }
消费者3:it
package com.example.demo.queue.exchangeToQueue.direct; 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_direct"; private static final String QUEUE_NAME = "direct_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); // 绑定队列到交换机 channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "info"); channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "error"); channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, "debug"); // 定义消费者 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."); // 反馈消息处理完毕 channel.basicAck(envelope.getDeliveryTag(), false); } }; // 接收信息 channel.basicConsume(QUEUE_NAME, true, consumer); } catch (Exception e) { e.printStackTrace(); } } }
和以前同样,涉及到了交换机,而在消费者类中中并无声明交换机,因此须要先执行生产者类的main方法io
接下来,咱们就能够依次执行三个消费者类的main方法,以后再执行生产者类的main方法。
上述生产者类指定的routingKey是debug,采用的是彻底匹配,理论上是只有消费者3能收到消息
执行生产者类的main方法获得的结果以下
消费者1终端:
消费者2终端:
消费者3终端:
结果与理论一致。