Producers send messages to the queue, and random consumers receive messages from the queue
- Create users and virtual hosts
You can easily create users and virtual hosts through the user management interface provided by rabbitmq, and you need to bind users to the corresponding virtual hosts. With its own guest
user and /
virtual host, you can also directly use these two existing information. We created a virtual host named wuwl
user and /vh
, note that the virtual host needs to /
start with.
- Import dependencies
<dependency>
<groupId>com.rabbitmq</groupId>
<artifactId>amqp-client</artifactId>
<version>5.10.0</version>
</dependency>
- Create a connection. Here, ConnectionUtils is regarded as a tool class, and both producers and consumers need to connect objects
public class ConnectionUtils {
public static Connection getConnection() throws IOException, TimeoutException {
// 创建连接mq的连接工厂对象
ConnectionFactory connectionFactory = new ConnectionFactory();
// 设置连接rabbitmq主机
connectionFactory.setHost("192.168.20.128");
// 设置端口号
connectionFactory.setPort(5672);
// 设置虚拟主机
connectionFactory.setVirtualHost("/vh");
// 设置用户名和密码
connectionFactory.setUsername("wuwl");
connectionFactory.setPassword("123456");
// 获取连接对象
return connectionFactory.newConnection();
}
}
- Create producer
public class Provider {
public void send() throws IOException, TimeoutException {
Connection connection = null;
Channel channel = null;
try {
connection = ConnectionUtils.getConnection();
// 获取连接通道
channel = connection.createChannel();
/** 通道绑定对应的消息队列
* 参数一:队列名称,如果队列不存在,自动创建队列
* 参数二:定义队列是否持久化
* 参数三:是否独占队列
* 参数四:是否在消费完成后自动删除队列
*/
channel.queueDeclare("wuwl",true,false,false,null);
/**
* 发布消息
* 参数一: 交换机名称
* 参数二:队列名称
* 参数三:传递消息额外设置
* 参数四:消息的具体内容
*/
channel.basicPublish("","wuwl",null,"hello rabbitmq".getBytes());
}finally {
if (channel !=null && channel.isOpen()) {
channel.close();
}
if (connection != null && connection.isOpen()) {
connection.close();
}
}
}
public static void main(String[] args) throws IOException, TimeoutException {
Provider provider = new Provider();
provider.send();
}
}
- Create a consumer. If the consumer's channel and connection object are not closed, it will always be in the monitoring state. Only the producer sends a new message to the message queue, and the consumer will consume it immediately
public class Consumer {
public void consume() throws IOException, TimeoutException {
Connection connection = null;
Channel channel = null;
try {
connection = ConnectionUtils.getConnection();
// 获取连接通道
channel = connection.createChannel();
/** 通道绑定对应的消息队列
* 参数一:队列名称,如果队列不存在,自动创建队列
* 参数二:定义队列是否持久化
* 参数三:是否独占队列
* 参数四:是否在消费完成后自动删除队列
*/
channel.queueDeclare("wuwl", true, false, false, null);
/**
* 消费
* 参数一: 交换机名称
* 参数二:队列名称
* 参数三:传递消息额外设置
* 参数四:消息的具体内容
*/
channel.basicConsume("wuwl",true,new DefaultConsumer(channel){
@Override
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
System.out.println("消费消息:" + new String(body));
}
});
} finally {
// if (channel != null && channel.isOpen()) {
// channel.close();
// }
// if (connection != null && connection.isOpen()) {
// connection.close();
// }
}
}
public static void main(String[] args) throws IOException, TimeoutException {
Consumer consumer = new Consumer();
consumer.consume();
}
}
- Test
Run the producer alone, we can see the queue we defined in the management interface, and the benefits that have not been consumed
After starting the consumer, the message is consumed and is continuously monitored
At this time, the number of messages to be consumed in the queue is 0
Send a message to the message queue through the producer again, the consumer directly consumes, the number of messages to be consumed in the queue is 0