SpringBoot3整合RabbitMQ之三_工作队列模型案例

SpringBoot3整合RabbitMQ之三_工作队列模型案例

文章目录

  • SpringBoot3整合RabbitMQ之三_工作队列模型案例
  • [2. 工作队列模型](#2. 工作队列模型)
    • [1. 消息发布者](#1. 消息发布者)
      • [1. 创建工作队列的配置类](#1. 创建工作队列的配置类)
      • [2. 发布消费Controller](#2. 发布消费Controller)
    • [2. 消息消费者One](#2. 消息消费者One)
    • [3. 消息消费者Two](#3. 消息消费者Two)
    • [4. 消息消费者Three](#4. 消息消费者Three)
    • [5. 输出结果](#5. 输出结果)

2. 工作队列模型

1. 消息发布者

1. 创建工作队列的配置类

java 复制代码
package com.happy.msg.config;

import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.QueueBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * <p>
 *
 * @Description: 工作队列模型_创建名称为 work_queue 的队列 <br>
 * </p>
 * @Datetime: 2024/3/27 18:18
 * @Author: Yuan · JinSheng <br>
 * @Since 2024/3/27 18:18
 */
@Configuration
public class WorkQueueConfig {
    @Bean
    Queue workQueue() {
        return QueueBuilder.durable("work_queue").build();
    }
}

2. 发布消费Controller

java 复制代码
package com.happy.msg.publisher;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *
 * @Description: 生产消息的控制器 <br>
 * </p>
 * @Datetime: 2024/3/27 10:53
 * @Author: Yuan · JinSheng <br>
 * @Since 2024/3/27 10:53
 */
@RestController
@RequestMapping("/work")
public class WorkQueuePublisherController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/send")
    public String sentMessage() {
        for (int i = 1; i <=10 ; i++) {
            rabbitTemplate.convertAndSend("work_queue", "work_queue队列第["+i+"]条消息,hello,rabbitmq"+i);
        }
        return "发送成功";
    }
}

2. 消息消费者One

java 复制代码
package com.happy.msg.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
/**
 * <p>
 * @Description: 工作队列模型_消息消费者一 <br>
 * </p>
 * @Datetime: 2024/3/28 20:28
 * @Author: Yuan · JinSheng <br>
 * @Since 2024/3/28 20:28
 */
@Slf4j
@Component
public class WorkQueueConsumerOne {

    /***
     * @param message 消息
     * @Description: 监听work_queue队列中的消息,当客户端启动后,work_queue队列中的所有的消息都被此消费者消费并打印
     * @Author: Yuan · JinSheng
     */

    @RabbitListener(queues = "work_queue")
    public void msg(Message message){
        byte[] messageBody = message.getBody();
        String msg = new String(messageBody);
        log.info("WorkQueueConsumerOne接收到work_queue队列中的消息==={},===接收时间==={}",msg, LocalDateTime.now());
    }
}
  1. 输出结果
java 复制代码
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[1]条消息,hello,rabbitmq1,===接收时间===2024-03-29T10:34:34.468074100
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[3]条消息,hello,rabbitmq3,===接收时间===2024-03-29T10:34:34.469081600
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[5]条消息,hello,rabbitmq5,===接收时间===2024-03-29T10:34:34.469976
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[7]条消息,hello,rabbitmq7,===接收时间===2024-03-29T10:34:34.469976
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[9]条消息,hello,rabbitmq9,===接收时间===2024-03-29T10:34:34.470811800

3. 消息消费者Two

java 复制代码
package com.happy.msg.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
/**
 * <p>
 * @Description: 工作队列模型_消息消费者二<br>
 * </p>
 * @Datetime: 2024/3/28 20:30
 * @Author: Yuan · JinSheng <br>
 * @Since 2024/3/28 20:30
 */
@Slf4j
@Component
public class WorkQueueConsumerTwo {

    /***
     * @param message 消息
     * @Description: 监听work_queue队列中的消息,当客户端启动后,work_queue队列中的所有的消息都被此消费者消费并打印
     * @Author: Yuan · JinSheng
     */

    @RabbitListener(queues = "work_queue")
    public void msg(Message message){
        byte[] messageBody = message.getBody();
        String msg = new String(messageBody);
        log.info("WorkQueueConsumerTwo接收到work_queue队列中的消息==={},===接收时间==={}",msg, LocalDateTime.now());
    }
}

4. 消息消费者Three

java 复制代码
package com.happy.msg.consumer;

import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
/**
 * <p>
 * @Description: 工作队列模型_消息消费者三<br>
 * </p>
 * @Datetime: 2024/3/28 20:30
 * @Author: Yuan · JinSheng <br>
 * @Since 2024/3/28 20:30
 */
@Slf4j
@Component
public class WorkQueueConsumerTwo {

    /***
     * @param message 消息
     * @Description: 监听work_queue队列中的消息,当客户端启动后,work_queue队列中的所有的消息都被此消费者消费并打印
     * @Author: Yuan · JinSheng
     */

    @RabbitListener(queues = "work_queue")
    public void msg(Message message){
        byte[] messageBody = message.getBody();
        String msg = new String(messageBody);
        //log.info("WorkQueueConsumerTwo接收到work_queue队列中的消息==={},===接收时间==={}",msg, LocalDateTime.now());
        System.out.println("WorkQueueConsumerTwo接收到work_queue队列中的消息==="+msg+",===接收时间==="+LocalDateTime.now());
    }
}

5. 输出结果

可看到消息被三个消费者按相等数量消费,总共10条消息,消费者1消费了4条,其他两个消费者消费了3条消息

java 复制代码
WorkQueueConsumerTwo接收到work_queue队列中的消息===work_queue队列第[3]条消息,hello,rabbitmq3,===接收时间===2024-03-29T10:39:32.942546200
WorkQueueConsumerThree接收到work_queue队列中的消息===work_queue队列第[2]条消息,hello,rabbitmq2,===接收时间===2024-03-29T10:39:32.942546200
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[1]条消息,hello,rabbitmq1,===接收时间===2024-03-29T10:39:32.942037700
WorkQueueConsumerTwo接收到work_queue队列中的消息===work_queue队列第[6]条消息,hello,rabbitmq6,===接收时间===2024-03-29T10:39:32.943806300
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[4]条消息,hello,rabbitmq4,===接收时间===2024-03-29T10:39:32.944336900
WorkQueueConsumerTwo接收到work_queue队列中的消息===work_queue队列第[9]条消息,hello,rabbitmq9,===接收时间===2024-03-29T10:39:32.944336900
WorkQueueConsumerThree接收到work_queue队列中的消息===work_queue队列第[5]条消息,hello,rabbitmq5,===接收时间===2024-03-29T10:39:32.944336900
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[7]条消息,hello,rabbitmq7,===接收时间===2024-03-29T10:39:32.944336900
WorkQueueConsumerOne接收到work_queue队列中的消息===work_queue队列第[10]条消息,hello,rabbitmq10,===接收时间===2024-03-29T10:39:32.944336900
WorkQueueConsumerThree接收到work_queue队列中的消息===work_queue队列第[8]条消息,hello,rabbitmq8,===接收时间===2024-03-29T10:39:32.944336900
相关推荐
听忆.8 分钟前
RabbitMQ消息可靠性等机制详解(精细版三)
java·开发语言·spring boot·后端·spring·java-ee·rabbitmq
吃货智1 小时前
Kafka搭建(集群版)
分布式·kafka
赫萝的红苹果2 小时前
基于Redisson实现分布式锁
java·spring boot·分布式
Java追光着3 小时前
谷粒商城笔记-03-分布式基础概念
笔记·分布式·谷粒商城
空青7266 小时前
AOP与IOC详解
java·服务器·分布式·后端·中间件·面试·架构
我非夏日6 小时前
基于Hadoop平台的电信客服数据的处理与分析③项目开发:搭建基于Hadoop的全分布式集群---任务6:安装并配置Hadoop
大数据·hadoop·分布式
ZJ_.6 小时前
Node.js 使用 gRPC:从定义到实现
java·开发语言·javascript·分布式·rpc·架构·node.js
我非夏日7 小时前
基于Hadoop平台的电信客服数据的处理与分析③项目开发:搭建基于Hadoop的全分布式集群---任务5:ZooKeeper集群安装
大数据·hadoop·分布式·zookeeper·大数据技术开发·大数据项目·电信客服数据的处理与分析
喻师傅8 小时前
Hadoop权威指南-读书笔记-03-Hadoop分布式文件系统
大数据·hadoop·分布式
尾巴尖上的阳光10 小时前
分布式数据库HBase:从零开始了解列式存储
数据库·分布式·hbase