Spring整合RabbitMQ-配制文件方式-3-消息拉模式

拉消息的消费者

java 复制代码
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class ConsumerGet {

    public static void main(String[] args) throws Exception {
        AbstractApplicationContext context = new ClassPathXmlApplicationContext("spring-rabbit.xml");

        RabbitTemplate template = context.getBean(RabbitTemplate.class);


        Message receive = template.receive("queue.msg");

        //报文头中的消息编码
        String encoding = receive.getMessageProperties().getContentEncoding();

        System.out.println("收到的消息:" + new String(receive.getBody(), encoding));


        context.close();
    }
}

spring-rabbit.xml

java 复制代码
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:rabbit="http://www.springframework.org/schema/rabbit"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/rabbit
    http://www.springframework.org/schema/rabbit/spring-rabbit.xsd">

    <!--配制连接工厂-->
    <rabbit:connection-factory id="connectFactory"
                               host="node1" virtual-host="/"
                               username="root" password="123456"
                               port="5672"
    ></rabbit:connection-factory>


    <!--用于自动向RabbitMQ声明队列、交换器、绑定 等操作工具类-->
    <rabbit:admin id="rabbitAdmin" connection-factory="connectFactory"></rabbit:admin>


    <!--用于简化操作的模板类-->
    <rabbit:template connection-factory="connectFactory" id="rabbitTemplate"/>


    <!--声明队列队列-->
    <rabbit:queue id="msg1" name="queue.msg" durable="false" exclusive="false" auto-delete="false"></rabbit:queue>


</beans>

当启动消费者后,便可获取到发送至队列的消息

sh 复制代码
收到的消息:hello world

检查队列的消息的情况:

复制代码
[root@nullnull-os ~]# rabbitmqctl list_queues  --formatter pretty_table
Timeout: 60.0 seconds ...
Listing queues for vhost / ...
┌───────────┬──────────┐
│ name      │ messages │
├───────────┼──────────┤
│ queue.msg │ 0        │
└───────────┴──────────┘

经过检查确认,发现消息已经被消费了。

至此拉模式的消费者完成。

相关推荐
播播资源1 小时前
MiniMax-M4 API 选型指南:接入、思考参数与缓存避坑(含实测数据)
java·spring·缓存
飞奔的瓜牛1 小时前
RabbitMQ 消息消费延迟排查指南:从现象到根因的系统化定位
分布式·rabbitmq·消费延迟
skywoodsky19 小时前
rabbitmq联邦模式
rabbitmq
Thanks_ks20 小时前
单例模式与工厂模式精讲:设计模式的意图、实现与在 Java 项目中的实际应用
java·spring·单例模式·设计模式·工厂模式·线程安全·面向对象设计
CoderYanger20 小时前
Java EE 进阶:16. Spring原理(第二弹)
java·服务器·spring boot·后端·spring·面试·java-ee
Rain的Java大神之路21 小时前
读懂 Spring 源码,从"能编译"开始:Spring 7.0.8 环境搭建 + 阅读方法全攻略
java·后端·spring
nice_lcj5201 天前
在 Spring Boot 里学会使用 RabbitMQ:从入门到实战
java·spring boot·spring·rabbitmq·java-rabbitmq
安易算力1 天前
大模型推理部署实战:vLLM与TensorRT-LLM配置参数深度解析
java·spring·vllm
风中芦苇啊1 天前
Spring AI 项目实战:从零搭建一个智能问答服务
java·人工智能·spring
萧瑟余晖1 天前
Spring Framework详解篇三:Spring依赖注入与循环依赖详解
java·后端·spring