《RabbitMQ系列教程-第三章-RabbitMQ快速入门》 秒速五厘米 2023-01-02 03:20 157阅读 0赞 # 教程说明 # * 本系列教程目录大纲:[《RabbitMQ系列教程-目录大纲》][RabbitMQ_-] -------------------- * 本系列教程配套代码:[https://gitee.com/lvshuichangliu/rabbitmt.git][https_gitee.com_lvshuichangliu_rabbitmt.git](码云地址) -------------------- # 第三章 RabbitMQ快速入门 # ## 3.1 消息生产者 ## ### pom.xml: ### <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>com.lscl</groupId> <artifactId>01_rabbitm1_producer</artifactId> <version>1.0-SNAPSHOT</version> <dependencies> <!--rabbitmq依赖--> <dependency> <groupId>com.rabbitmq</groupId> <artifactId>amqp-client</artifactId> <version>5.6.0</version> </dependency> <!--测试单元--> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.12</version> </dependency> </dependencies> </project> ### 生产者类: ### 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(); } } 查看RabbitMQ面板: ![在这里插入图片描述][20210104152250898.png_pic_center] ### RabbitMQ面板参数说明 ### * `Virtual host`:该队列所在的虚拟主机 * `Name`:队列名称 * `Features`:队列的特性,如是否持久化、是否是过期队列等 * `State`:此队列工作状态 * `running`:队列处于运行状态(正在创建队列、接受消息、消费消息等) * `idle`:队列处于空闲状态 * `Ready`:已经准备好的消息(还未被消费) * `Unacked`:未处理的消息(被拒绝签收) * `Total`:总的消息数量 * `incoming`:消息的每秒接收个数 * `deliver / get`:消息的每秒消费个数 * `ack`:每秒手动签收消息个数 ## 3.2 消息消费者 ## ### 引入依赖: ### 和消息生产的依赖一致 ### 消费者类: ### 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一直监听 } } -------------------- 下一篇:[《RabbitMQ系列教程-第四章-01-RabbitMQ工作模式之Simple模式》][RabbitMQ_-_-01-RabbitMQ_Simple] -------------------- [RabbitMQ_-]: https://blog.csdn.net/Bb15070047748/article/details/112194912 [https_gitee.com_lvshuichangliu_rabbitmt.git]: https://gitee.com/lvshuichangliu/rabbitmt.git [20210104152250898.png_pic_center]: /images/20221120/6cb551e7e80645ad8e0b8534d127a93e.png [RabbitMQ_-_-01-RabbitMQ_Simple]: https://blog.csdn.net/Bb15070047748/article/details/112180640
还没有评论,来说两句吧...