Direct 模式是在 Fanout 模式基础上添加了 routing key,Fanout(发布/订阅)模式是交换机将消息存储到所有绑定的队列中,而 Direct 模式是在此基础上,添加了过滤条件,交换机只会将消息存储到满足 routing key 的队列中。
package com.tszr.direct; import com.rabbitmq.client.Channel; import com.rabbitmq.client.Connection; import com.rabbitmq.client.ConnectionFactory; import java.io.IOException; import java.util.concurrent.TimeoutException; public class Productor { private static final String EXCHANGE_NAME = "direct_exchange"; public static void main(String[] args) { // 1、创建连接工程 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); factory.setUsername("guest"); factory.setPassword("guest"); factory.setVirtualHost("/"); Connection connection = null; Channel channel = null; try { // 2、获取连接、通道 connection = factory.newConnection(); channel = connection.createChannel(); // 消息内容 String message = "hello direct mode"; // 指定路由key String routeKey = "email"; String type = "direct"; // 3、声明交换机 channel.exchangeDeclare(EXCHANGE_NAME, type); // 4、声明队列 channel.queueDeclare("queue1", false, false, false, null); channel.queueDeclare("queue2", false, false, false, null); channel.queueDeclare("queue3", false, false, false, null); // 5、绑定 channel 与 queue channel.queueBind("queue1", EXCHANGE_NAME, "email"); channel.queueBind("queue2", EXCHANGE_NAME, "sms"); channel.queueBind("queue3", EXCHANGE_NAME, "vx"); // 6、发布消息 channel.basicPublish(EXCHANGE_NAME, routeKey, null, message.getBytes("UTF-8")); System.out.println("消息发送成功!"); } catch (IOException | TimeoutException e) { e.printStackTrace(); System.out.println("消息发送异常"); } finally { // 关闭通道 if (channel != null && channel.isOpen()) { try { channel.close(); } catch (Exception e) { e.printStackTrace(); } } // 关闭连接 if (connection != null && connection.isOpen()) { try { connection.close(); } catch (Exception e) { e.printStackTrace(); } } } } }
package com.tszr.direct; import com.rabbitmq.client.*; import java.io.IOException; import java.util.concurrent.TimeoutException; public class Customer { private static Runnable runnable = new Runnable() { @Override public void run() { // 创建连接工厂 ConnectionFactory factory = new ConnectionFactory(); factory.setHost("127.0.0.1"); factory.setUsername("guest"); factory.setPassword("guest"); factory.setVirtualHost("/"); final String queueName = Thread.currentThread().getName(); Connection connection = null; Channel channel = null; try { // 获取连接、通道 connection = factory.newConnection(); channel = connection.createChannel(); Channel finalChannel = channel; finalChannel.basicConsume(queueName, true, new DeliverCallback() { @Override public void handle(String consumerTag, Delivery delivery) throws IOException { System.out.println(delivery.getEnvelope().getDeliveryTag()); System.out.println(queueName + ":收到消息是:" + new String(delivery.getBody(), "UTF-8")); } }, new CancelCallback() { @Override public void handle(String consumerTag) throws IOException { } }); System.out.println(queueName + ":开始接收消息"); } catch (IOException | TimeoutException e) { e.printStackTrace(); } finally {
try {
Thread.sleep(3);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭通道 if (channel != null && channel.isOpen()) { try { channel.close(); } catch (Exception e) { e.printStackTrace(); } } // 关闭连接 if (connection != null && connection.isOpen()) { try { connection.close(); } catch (Exception e) { e.printStackTrace(); } } } } }; public static void main(String[] args) throws IOException, TimeoutException { // 创建线程分别从3个队列中获取消息 new Thread(runnable, "queue1").start(); new Thread(runnable, "queue2").start(); new Thread(runnable, "queue3").start(); } }