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


    拉消息的消费者
    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();
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    spring-rabbit.xml

    <?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>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

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

    收到的消息:hello world
    
    • 1

    检查队列的消息的情况:

    [root@nullnull-os ~]# rabbitmqctl list_queues  --formatter pretty_table
    Timeout: 60.0 seconds ...
    Listing queues for vhost / ...
    ┌───────────┬──────────┐
    │ name      │ messages │
    ├───────────┼──────────┤
    │ queue.msg │ 0        │
    └───────────┴──────────┘
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

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

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

  • 相关阅读:
    本周Github有趣项目:draw-a-ui等
    关于求直线交点的问题。
    pod详解
    神经网络控制与matlab仿真,matlab神经网络拟合预测
    SQL Server 防病毒软件配置
    Scheduled定时任务
    跨时钟域问题(一)(建立时间保持时间和亚稳态)
    深入理解Java消息中间件-消息的可靠性
    解决Windows环境下的docker中修改了mysql的配置文件之后启动不了的问题
    Chrome自动播放限制策略
  • 原文地址:https://blog.csdn.net/bug_null/article/details/132701206