教程说明
- 本系列教程目录大纲:《RabbitMQ系列教程-目录大纲》
- 本系列教程配套代码:https://gitee.com/lvshuichangliu/rabbitmt.git(码云地址)
RabbitMQ工作模式之Simple模式
4.1.1 简介
simple模式就是我们之前做的快速入门案例
简单模式中表现为一个生产者对应一个消费者,生产者(Producer)生产消息发送到队列,消费者(Consumer)监听此队列,进行消息的消费;
Simple模式官网介绍:https://www.rabbitmq.com/tutorials/tutorial-one-java.html
4.1.2 生产者
package com.lscl.rabbitmq;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.rabbitmq.client.ConnectionFactory;
public class Producer01_Hello {
public static void main(String[] args) throws Exception{
// 1. 创建连接工厂
ConnectionFactory factory = new ConnectionFactory();
// 2. 设置连接参数
factory.setHost("192.168.133.147");
factory.setPort(5672);
factory.setVirtualHost("/");
factory.setUsername("guest");
factory.setPassword("guest");
// 3. 获取连接对象
Connection connection = factory.newConnection();
// 4. 通过connection获取Channel
Channel channel = connection.createChannel();
// 5. 定义一个交换机
/*
参数1(queue): 队列名称
参数2(durable): 是否要持久化队列(mq重启之后还在)
参数3(exclusive): 是否是独占队列(只能有一个消费者监听此队列,没有也不行)
参数4(autoDelete): 是否自动删除(当没有Consumer时,队列自动删除)
参数5(arguments): 队列的其他参数(过期时间、最大消息容量等)
*/
channel.queueDeclare("hello_world",true,false,false,null);
String body="hello world";
// 6.发送消息
/*
参数1(exchange): 交换机的名称,简单模式下交换机会使用默认的交换机""
参数2(routingKey): routingKey,路由名称,在简单模式下,routingKey就是队列名称
参数3(props): 消息的一些配置信息
参数4(body): 发送的消息(字节)
*/
channel.basicPublish("","hello_world",null,body.getBytes());
// 7. 释放资源
channel.close();
connection.close();
}
}
4.1.3 消费者
package com.lscl.rabbitmq;
import com.rabbitmq.client.*;
import java.io.IOException;
public class Consumer01_Hello {
public static void main(String[] args) throws Exception{
// 创建连接工厂,用于获取频道channel
ConnectionFactory factory = new ConnectionFactory();
// 设置连接参数
factory.setHost("192.168.40.132");
factory.setPort(5672);
factory.setUsername("guest");
factory.setPassword("guest");
factory.setVirtualHost("/");
// 2.创建连接
Connection connection = factory.newConnection();
// 3.创建频道
Channel channel = connection.createChannel();
// 4.创建队列
/*
定义队列,如果没有此队列则创建,如果有则不创建
queueDeclare(String queue, boolean durable, boolean exclusive, boolean autoDelete, Map<String, Object> arguments)
queue: 队列的名称
durable: 是否持久化队列(mq重启之后还在)
exclusive: 是否独占(只能有一个消费者监听此队列)
autoDelete: 是否自动删除(当没有Consumer时,自动删除掉)
arguments: 其他参数
*/
channel.queueDeclare("hello_world", true, false, false, null);
// 5. 接收消息
/*
basicConsume(String queue, boolean autoAck, Consumer callback)
queue: 队列名称
autoAck: 是否开启自动确认
callback: 回调对象
*/
channel.basicConsume("hello_world", true, new DefaultConsumer(channel) {
// 回调方法,当收到消息之后,会自动执行该方法
public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
/*
1. consumerTag:标识
2. envelope:获取一些信息,交换机,路由key...
3. properties:配置信息
4. body:数据
*/
System.out.println("consumerTag:" + consumerTag);
System.out.println("Exchange:" + envelope.getExchange());
System.out.println("RoutingKey:" + envelope.getRoutingKey());
System.out.println("properties:" + properties);
System.out.println("body:" + new String(body));
}
});
// 不释放资源,让rabbitmq一直监听
}
}
4.1.4 小结
在Simple交换模式中,一个生产者对应一个消费者,不能对应多个消费者(有多个消费者就不是Simple模式了),生产者发送消息给默认的交换机(空字符串""
),消息由默认的交换机(空字符串)路由到队列,消费者监听队列进行消息的消费;
下一篇:《RabbitMQ系列教程-第四章-02-RabbitMQ工作模式之Work模式》