接收消息,并根据路由键转发消息所绑定的队列
这是官网的一个交换机模型图,其中:
1、direct
所有发送到direct exchange的消息被转发到routingkey中指定的queue
注意
direct模式可以使用rabbitmq自带的exchange:default exchange,所以不需要将exchange进行任何的绑定(binding)操作,消息传递时,routingkey必须完全匹配才会被队列接受,否则该消息会被抛弃。
生产者
public static final String EXCHANGE_NAME = "test_exchange_direct";
public static final String ROUTING_KEY = "routingkey.direct";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 发送消息
String message = "test message for exchange_direct";
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY, null, message.getBytes());
System.out.println("发送成功:" + message);
channel.close();
connection.close();
}
消费者
public static final String EXCHANGE_NAME = "test_exchange_direct";
public static final String EXCHANGE_TYPE = "direct";
public static final String ROUTING_KEY = "routingkey.direct";
public static final String QUEUE_NAME = "test_queue";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 创建消费者进行监听获取消息
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收消息message: " + new String(body));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
首先运行消费者,启动监听。再运行生产者,此时查看消费者控制台,可看到已接收到消息
如果修改其中任一的routingkey,使得两者routingkey不一致,那么再次执行上述步骤,消费者则无法接受到消息。
2、topic
所有发送到topic exchange的消息被转发到所有关心routingkey中指定topic的queue上
exchange将routingkey 和某个topic进行模糊匹配,此时队列需要绑定一个topic
可以使用通配符
public static final String EXCHANGE_NAME = "test_exchange_topic";
public static final String ROUTING_KEY1 = "routingkey.topic";
public static final String ROUTING_KEY2 = "routingkey.topic.a";
public static final String ROUTING_KEY3 = "routingkey2.topic";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 发送消息
String message1 = "test message for exchange_topic 1";
String message2 = "test message for exchange_topic 2";
String message3 = "test message for exchange_topic 3";
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY1, null, message1.getBytes());
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY2, null, message2.getBytes());
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY3, null, message3.getBytes());
System.out.println("发送成功");
channel.close();
connection.close();
}
创建三个消费者,分别使用不同的routingkey
// 消费者1 接受消息1和消息2
public static final String EXCHANGE_NAME = "test_exchange_topic";
public static final String EXCHANGE_TYPE = "topic";
public static final String ROUTING_KEY = "routingkey.#";
public static final String QUEUE_NAME = "test_queue1";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 创建消费者进行监听获取消息
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收消息message: " + new String(body));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
// 消费者2 接受消息1
public static final String EXCHANGE_NAME = "test_exchange_topic";
public static final String EXCHANGE_TYPE = "topic";
public static final String ROUTING_KEY = "routingkey.*";
public static final String QUEUE_NAME = "test_queue2";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 创建消费者进行监听获取消息
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收消息message: " + new String(body));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
// 消费者3 接受消息1和消息3
public static final String EXCHANGE_NAME = "test_exchange_topic";
public static final String EXCHANGE_TYPE = "topic";
public static final String ROUTING_KEY = "*.topic";
public static final String QUEUE_NAME = "test_queue3";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 创建消费者进行监听获取消息
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收消息message: " + new String(body));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
我们分别启动三个消费者,然后启动生产者后,查看三个消费者的控制台输出
3、fanout
不处理任何路由键,只需要简单的将队列绑定到交换机上
发送到交换机的消息都会被转发到与该交换机绑定的所有队列上
fanout交换机转发消息的效率是最快的
生产者
public static final String EXCHANGE_NAME = "test_exchange_fanout";
public static final String ROUTING_KEY1 = "routingkey.fanout";
public static final String ROUTING_KEY2 = "routingkey.fanout.a";
public static final String ROUTING_KEY3 = "routingkey2.fanout";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 发送消息
String message1 = "test message for exchange_fanout 1";
String message2 = "test message for exchange_fanout 2";
String message3 = "test message for exchange_fanout 3";
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY1, null, message1.getBytes());
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY2, null, message2.getBytes());
channel.basicPublish(EXCHANGE_NAME, ROUTING_KEY3, null, message3.getBytes());
System.out.println("发送成功");
channel.close();
connection.close();
}
消费者1
public static final String EXCHANGE_NAME = "test_exchange_fanout";
public static final String EXCHANGE_TYPE = "fanout";
public static final String ROUTING_KEY = "routingkey.#";
public static final String QUEUE_NAME = "test_queue1";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 创建消费者进行监听获取消息
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收消息message: " + new String(body));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
消费者2
public static final String EXCHANGE_NAME = "test_exchange_fanout";
public static final String EXCHANGE_TYPE = "fanout";
public static final String ROUTING_KEY = "routingkey.*";
public static final String QUEUE_NAME = "test_queue2";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 创建消费者进行监听获取消息
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收消息message: " + new String(body));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
消费者3
public static final String EXCHANGE_NAME = "test_exchange_fanout";
public static final String EXCHANGE_TYPE = "fanout";
public static final String ROUTING_KEY = "*.fanout";
public static final String QUEUE_NAME = "test_queue3";
public static void main(String[] args) throws IOException, TimeoutException {
// 获取连接
Connection connection = ConnectionsUtils.getConnection();
// 创建信道
Channel channel = connection.createChannel();
// 声明交换机
channel.exchangeDeclare(EXCHANGE_NAME, EXCHANGE_TYPE, true, false, false ,null);
// 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
// 绑定
channel.queueBind(QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY);
// 创建消费者进行监听获取消息
DefaultConsumer consumer = new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("接收消息message: " + new String(body));
System.out.println("consumerTag: " + consumerTag);
System.out.println("envelope: " + envelope);
System.out.println("properties: " + properties);
}
};
channel.basicConsume(QUEUE_NAME, true, consumer);
}
因为fanout模式不会处理任何的路由键,因此,这里我们将exchange模式设置为fanout后,无论设置routing可以与否,都会将消息发送到每个绑定该exchange的队列上去,因此,三个消费者都会接收到三条消息