欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

SpringBoot 动态监听RabbitMq队列

程序员文章站 2022-06-28 16:22:51
每个人遇到的场景可能不同,我本次遇到的就是队列已存在,我只需要监听即可。话不多说,上码!application.propertiesserver.port=9009spring.rabbitmq.host=192.168.1.118spring.rabbitmq.port=5672spring.rabbitmq.username=rabbit_devspring.rabbitmq.password=Dd.1111pom.xml...

每个人遇到的场景可能不同,我本次遇到的就是队列已存在,我只需要监听即可。话不多说,上码!

application.properties

server.port=9009
spring.rabbitmq.host=192.168.1.118
spring.rabbitmq.port=5672
spring.rabbitmq.username=rabbit_dev
spring.rabbitmq.password=Dd.1111

pom.xml

<dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <!--amqp-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <version>2.0.3.RELEASE</version>
        </dependency>

        <!-- https://mvnrepository.com/artifact/cn.hutool/hutool-extra -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-extra</artifactId>
            <version>5.5.6</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
    </dependencies>
RbMQReceiverHand.java

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageListener;
import org.springframework.stereotype.Component;

/**
 * @author ...
 * @version 1.0
 * @date 2020/12/29 18:02
 * @Email ...@163.com
 * @DESC:
 */
/**
 * 监听接收消息
 */
@Component
public class RbMQReceiverHand implements MessageListener {

    @Override
    public void onMessage(Message message) {
        System.out.println("====接收到One" + message.getMessageProperties().getConsumerQueue() + "队列的消息=====");
        System.out.println(message.getMessageProperties().toString());
        System.out.println(new String(message.getBody()));
    }
}
RabbitMQConfig.java


import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

import javax.annotation.Resource;

/**
 * @author ...
 * @version 1.0
 * @date 2020/12/29 18:05
 * @Email ...@163.com
 * @DESC:
 */
@Configuration
@Import(cn.hutool.extra.spring.SpringUtil.class)
public class RabbitMQConfig {
    @Resource
    RbMQReceiverHand RbMQReceiverHand;

    @Bean
    public Queue bw_Cabinet_OnOff() { return new Queue("Cabinet_OnOff",true); }
    @Bean
    public SimpleMessageListenerContainer messageListenerContainer(ConnectionFactory connectionFactory) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames("Cabinet_OnOff");
        container.setMessageListener(RbMQReceiverHand);
        return container;
    }
}

RbTestController.java
package com.bw.Test;

import cn.hutool.extra.spring.SpringUtil;
import org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer;
import org.springframework.web.bind.annotation.*;

/**
 * @author ...
 * @version 1.0
 * @date 2020/12/30 17:59
 * @Email ...@163.com
 * @DESC:
 */
@RestController
@RequestMapping("/test")
public class RbTestController {
    @PostMapping
    public String addQueue(@RequestParam String queueNmae) {
        SimpleMessageListenerContainer container = SpringUtil.getBean("messageListenerContainer");
        container.addQueueNames(queueNmae);
        return "添加" + queueNmae + " ok";
    }

    @DeleteMapping
    public String delQueue(@RequestParam String queueNmae) {
        SimpleMessageListenerContainer container = SpringUtil.getBean("messageListenerContainerTwo");
        container.removeQueueNames(queueNmae);
        return "删除 " + queueNmae + " ok";
    }
}

到此结束,就算完成了。

我这里用getBean好处是可扩展

本文地址:https://blog.csdn.net/damo01/article/details/112021830

相关标签: Spring Boot