RabbitMQ初学之一:exchange与queue的绑定

时间:2021-10-20 14:44:50

最近公司需要使用RabbitMQ,但我之前一直使用的是ActiveMQ,对RabbitMQ进行了初步的学习,但是还不系统,自己做了一些小测试,怕自己以后忘了

一. 背景

  拿到代码以后,发现,生产者在向外发送消息时,指定了exchange(交换机)和routing key,但是没有指定queue(队列)也没有将queue(队列)绑定到exchange,刚开始因为不熟悉rabbitMQ,所有不知道怎么回事,后来知道了:消费者在消费消息时,需要声明队列(队列名字随便),并将声明的队列通过routing key绑定到exchange,这样才能接收到数据,因此,生产者方需要将exchange和routing key实现告知消费者方。

二. 代码实例:生产者方指定了exchange(交换机)和routing key,但是不指定queue(队列)也不将queue(队列)绑定到exchange,队列声明和绑定队列到exchange的工作由消费者方完成

  1. 生产者方

    ① 生产者方代码

 import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; public class Producer {
private final static String QUEUE_NAME = "QUEUE8"; public static void main(String[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest"); Connection connection = factory.newConnection();
Channel channel = connection.createChannel(); String message = "Hello World!"; // 指定exchange和routing key,并发送消息到exchange
channel.basicPublish("FILETOPIC", "KEY.FILE", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'"); channel.close();
connection.close();
}
}

    ② 生产者方代码运行后,可在rabbiteMQ managerment 管理界面看到相应exchange,如下图所示:

---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

RabbitMQ初学之一:exchange与queue的绑定

--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

  2. 消费者方:消费者声明队列,队列名称随便起,并将该队列通过生产者指定的routing key绑定的其指定的exchange上

    ① 消费者方代码

 import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; public class Reqv { private final static String QUEUE_NAME = "QUEUE8"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory();
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setHost("localhost"); Connection connection = factory.newConnection();
Channel channel = connection.createChannel(); // 声明队列
channel.queueDeclare(QUEUE_NAME, false, false, false, null);
System.out.println(" [*] Waiting for messages. To exit press CTRL+C"); // 绑定队列到交换机
channel.queueBind(QUEUE_NAME, "FILETOPIC", "KEY.FILE"); QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
while(true){
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody(),"UTF-8");
System.out.println(" 【[x] Received 】:" + message);
}
}
}

    ② 运行效果,消费者方代码运行后,在rabbiteMQ managerment 管理界面可以看到声明的队列,并发现该队列已经绑定到了生产者指定的exchange上

---------------------------------------------------------------------------------------------------------------------------------------------------------------RabbitMQ初学之一:exchange与queue的绑定

--------------------------------------------------------------------------------------------------------------------------------------------------------------------

RabbitMQ初学之一:exchange与queue的绑定

---------------------------------------------------------------------------------------------------------------------------------------------------------------

三. 代码实例:生产者指定exchange和routing key,声明队列并将队列绑定到exchange,消费者只需从生产者绑定的队列消费即可

  1. 生产者

    ① 生产者代码

 import java.io.IOException;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory; public class Producer {
private final static String QUEUE_NAME = "QUEUE1"; public static void main(String[] args) throws IOException { ConnectionFactory factory = new ConnectionFactory();
factory.setHost("localhost");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest"); Connection connection = factory.newConnection();
Channel channel = connection.createChannel(); String message = "Hello World!"; // 声明队列
channel.queueDeclare(QUEUE_NAME, true, false, false, null); // 绑定队列到交换机
channel.queueBind(QUEUE_NAME, "FILETOPIC", "KEY.FILE"); // 指定exchange和routing key,并发送消息到exchange
channel.basicPublish("FILETOPIC", "KEY.FILE", null, message.getBytes());
System.out.println(" [x] Sent '" + message + "'"); channel.close();
connection.close();
}
}

    ② 生产者运行效果:生产者代码运行后,在rabbiteMQ managerment 管理界面可以看到生产者声明的队列,并发现该队列已经绑定到了生产者指定的exchange上,如下图所示:

RabbitMQ初学之一:exchange与queue的绑定

  2. 消费者

    ① 消费者代码:消费者现在直接消费队列中的消息即可,既不用声明队列,也不用绑定

 import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.QueueingConsumer;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection; public class Reqv { private final static String QUEUE_NAME = "QUEUE1"; public static void main(String[] argv) throws Exception { ConnectionFactory factory = new ConnectionFactory();
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setHost("localhost"); Connection connection = factory.newConnection();
Channel channel = connection.createChannel(); QueueingConsumer consumer = new QueueingConsumer(channel);
channel.basicConsume(QUEUE_NAME, true, consumer);
while(true){
QueueingConsumer.Delivery delivery = consumer.nextDelivery();
String message = new String(delivery.getBody(),"UTF-8");
System.out.println(" 【[x] Received 】:" + message);
}
}
}

    ② 消费者运行效果:在rabbiteMQ managerment 管理界面可以看到,生产者指定的队列已经有了消费者

RabbitMQ初学之一:exchange与queue的绑定