【RabbitMQ】使用SpringAMQP的消息队列(Hello Word)和工作队列(Work Queue)

SpringAMQP

SpringAMQP中文文档

Hello Word

**案例1:**利用SpringAMQP实现HelloWord中的集成消息队列功能

项目结构,如图:

1.引入AMQP依赖(父工程中)

xml 复制代码
<!--AMQP依赖,包含RabbitMQ-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

2.publisher包的application.yml中添加MQ连接信息

yml 复制代码
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.174.129 # rabbitMQ的ip地址(我是在虚拟机上用docker安装的RabbitMQ)
    port: 5672 # 端口
    virtual-host: / # 虚拟主机
    username: itcast # 用户名
    password: 123321 # 密码

3.在消息发送者(publisher包)中新建一个测试类SpringAmqpTest,编写测试方法

java 复制代码
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringAmqpTest {
    @Autowired
    private RabbitTemplate rabbitTemplate
	@Test
	public void testSimpleQueue(){
		String queueName ="simple.queue"; //队列名称
		String message ="hello, spring amgp!"; //消息
		rabbitTemplate.convertAndSend(queueName,message);
    }
}

4.运行,结果

5.在consumer中编写消费逻辑,监听simple.queue

5.1consumer包的application.yml中添加MQ连接信息
yml 复制代码
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.174.129 # rabbitMQ的ip地址(我是在虚拟机上用docker安装的RabbitMQ)
    port: 5672 # 端口
    virtual-host: / # 虚拟主机
    username: itcast # 用户名
    password: 123321 # 密码
5.2在消费者(consumer包)新建一个类SpringRabbitListener,编写消费逻辑
java 复制代码
@Component
public class SpringRabbitListener {
    
    @RabbitListener(queues="simple.queue")
	public void listenSimple0ueueMessage(String msg) throws InterruptedException {
		System.out.println("spring 消费者接收到消息 :【"+ msq +"】");
    }
}
5.3运行,结果

simple.queue队列中的消息也为空了

Work Queue

案例2:WorkQueue工作队列,实现一个队列绑定多个消费者

Work queue,工作队列,可以提高消息处理速度,避免队列消息堆积

前提:在案例1的基础上

1.在SpringAmqpTest添加新的发布

java 复制代码
//消息发送50次 
@Test
    public void testSendMessage2WorkQueue() throws InterruptedException {
        String queueName = "simple.queue";
        String message = "hello, message__";
        for (int i = 1; i <= 50; i++) {
            rabbitTemplate.convertAndSend(queueName, message + i);
            Thread.sleep(20); //休眠20秒
        }
    }

2.注释掉SpringRabbitListener上原有方法,添加新的消费

java 复制代码
 @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue1(String msg) throws InterruptedException {
        System.out.println("消费者1接收到消息:【" + msg + "】" + LocalTime.now());
        Thread.sleep(20);
    }

    @RabbitListener(queues = "simple.queue")
    public void listenWorkQueue2(String msg) throws InterruptedException {
        System.err.println("消费者2........接收到消息:【" + msg + "】" + LocalTime.now());
        Thread.sleep(200);
    }

3.运行,结果

结果为消费,平均分配

4.消费预取限制,能者多劳---在消费者(consumer包)的application.yml修改

yml 复制代码
logging:
  pattern:
    dateformat: MM-dd HH:mm:ss:SSS
spring:
  rabbitmq:
    host: 192.168.174.129 # rabbitMQ的ip地址
    port: 5672 # 端口
    username: itcast
    password: 123321
    virtual-host: /
    listener:
      simple:
        prefetch: 1 # 每次只能获取一条消息,处理完成才能获取下一个消息

结果,如图:

相关推荐
墨着染霜华1 天前
Java实战:封装Redis非阻塞分布式锁,彻底解决表单重复提交主键冲突
java·redis·分布式
隔壁小邓1 天前
kafka怎么处理消息一致性
分布式·kafka
我喜欢就喜欢1 天前
Word 模板匹配与样式同步技术详解
开发语言·c++·qt·word·模板匹配
only-qi1 天前
主流分布式事务框架与方案:从 XA 到 Seata 四模式
分布式·seata·分布式事务·xa·tcc
猹叉叉(学习版)1 天前
【ASP.NET CORE】 14. RabbitMQ、洋葱架构
笔记·后端·架构·c#·rabbitmq·asp.net·.netcore
安逸sgr1 天前
MCP 协议深度解析(八):Prompts 提示模板与 Sampling 采样机制!
人工智能·分布式·学习·语言模型·协议·mcp
⑩-1 天前
Kafka 架构和工作原理?Kafka 如何保证高可用?
java·分布式·架构·kafka
CET中电技术1 天前
中压(公共连接点10kV及以上)分布式光伏项目,四可如何改造?
分布式
2501_933329551 天前
万字拆解Infoseek舆情监测系统:基于大模型+多模态的分布式舆情中台架构实践
人工智能·分布式·架构·媒体
一叶飘零_sweeeet1 天前
击穿分布式时钟底层:从时钟偏移到线性一致性,工业级时序设计全实战
分布式·分布式时钟