一、
路由模式特点:
队列与交换机的绑定,不能是任意绑定了,而是要指定一个 RoutingKey (路由key)
消息的发送方在 向 Exchange发送消息时,也必须指定消息的 RoutingKey 。
Exchange不再把消息交给每一个绑定的队列,而是根据消息的 Routing Key 进行判断,只有队列的
Routingkey 与消息的 Routing key 完全一致,才会接收到消息
二、案例需求
一个频道
一个交换机(类型为:direct)
两个路由键(info and error)
两个队列(一个队列为info,一个队列为info and error)
一个生产者
两个消费者
三、代码
product:
package com.xiaoxu.direct;
import com.rabbitmq.client.BuiltinExchangeType;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
import com.xiaoxu.ConnectionUtis;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
/**
* Direct 路由模式
*/
public class Producter {
//定义两个消息队列
static final String QUEUE_DIRECT_1 = "queue_direct_1";
static final String QUEUE_DIRECT_2 = "queue_direct_2";
//defining a exchange
static final String EXCHANGE_NAME = "exchange_direct";
public static void main(String[] args) throws IOException, TimeoutException {
//1、创建连接工厂
//2. 创建连接;(抽取一个获取连接的工具类)
Connection connection = ConnectionUtis.getConnection();
// 3. 创建频道;
Channel channel = connection.createChannel();
//statement exchange
channel.exchangeDeclare(EXCHANGE_NAME, BuiltinExchangeType.DIRECT);
//5. 声明队列;
/**
* 参数1:队列名称
* 参数2:是否定义持久化队列
* 参数3:是否独占本次连接
* 参数4:是否在不使用的时候自动删除队列
* 参数5:队列其它参数
*/
channel.queueDeclare(QUEUE_DIRECT_1,true,false,false,null);
channel.queueDeclare(QUEUE_DIRECT_2,true,false,false,null);
// 5. 队列绑定交换机
channel.queueBind(QUEUE_DIRECT_1, EXCHANGE_NAME, "infor");
channel.queueBind(QUEUE_DIRECT_2, EXCHANGE_NAME, "error");
channel.queueBind(QUEUE_DIRECT_2, EXCHANGE_NAME, "infor");
// 6. 监听队列
/**
* 参数1:交换机名称,如果没有指定则使用默认Default Exchage
* 参数2:路由key,简单模式可以传递队列名称
* 参数3:消息其它属性
* 参数4:消息内容
*/
String message = "我是一个消息。。infor";
channel.basicPublish(EXCHANGE_NAME, "infor", null, message.getBytes());
System.out.println(message);
message = "我是一个消息。。error";
channel.basicPublish(EXCHANGE_NAME,"error",null,message.getBytes());
System.out.println(message);
//7、关闭资源
channel.close();
connection.close();
}
}
consumer1:
package com.xiaoxu.direct;
import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer1 {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtis.getConnection();
//创建频道
Channel channel = connection.createChannel();
//statement exchange
channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.FANOUT);
// create queue
channel.queueDeclare(Producter.QUEUE_DIRECT_1,true,false,false,null);
//bainding queue and exchange
channel.queueBind(Producter.QUEUE_DIRECT_1,Producter.EXCHANGE_NAME,"infor");
//creat consumer and set information
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException{
//路由key
System.out.println("路由key为:" + envelope.getRoutingKey());
//交换机
System.out.println("交换机为:" + envelope.getExchange());
//消息id
System.out.println("消息id为:" + envelope.getDeliveryTag());
//收到的消息
System.out.println("接收到的消息为:" + new String(body, "utf-8"));
System.out.println("我是消费者一号。。。");
}
};
//listen to the message
channel.basicConsume(Producter.QUEUE_DIRECT_1,true,consumer1);
}
}
consumer2:
package com.xiaoxu.direct;
import com.rabbitmq.client.*;
import com.xiaoxu.ConnectionUtis;
import java.io.IOException;
import java.util.concurrent.TimeoutException;
public class Consumer2 {
public static void main(String[] args) throws IOException, TimeoutException {
Connection connection = ConnectionUtis.getConnection();
//创建频道
Channel channel = connection.createChannel();
//statement exchange
channel.exchangeDeclare(Producter.EXCHANGE_NAME,BuiltinExchangeType.FANOUT);
// create queue
channel.queueDeclare(Producter.QUEUE_DIRECT_2,true,false,false,null);
//bainding queue and exchange
channel.queueBind(Producter.QUEUE_DIRECT_2,Producter.EXCHANGE_NAME,"infor");
channel.queueBind(Producter.QUEUE_DIRECT_2,Producter.EXCHANGE_NAME,"error");
//creat consumer and set information
DefaultConsumer consumer1 = new DefaultConsumer(channel) {
@Override
public void handleDelivery(String consumerTag, Envelope envelope,
AMQP.BasicProperties properties, byte[] body) throws IOException{
//路由key
System.out.println("路由key为:" + envelope.getRoutingKey());
//交换机
System.out.println("交换机为:" + envelope.getExchange());
//消息id
System.out.println("消息id为:" + envelope.getDeliveryTag());
//收到的消息
System.out.println("接收到的消息为:" + new String(body, "utf-8"));
System.out.println("我是消费者二号。。。");
}
};
//listen to the message
channel.basicConsume(Producter.QUEUE_DIRECT_2,true,consumer1);
}
}
四、测试
五、小结
启动所有消费者,然后使用生产者发送消息;在消费者对应的控制台可以查看到生产者发送对应routing key对应队
列的消息;到达按照需要接收的效果。
Routing模式要求队列在绑定交换机时要指定routing key,消息会转发到符合routing key的队列。