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
推荐阅读
-
SpringBoot集成RabbitMQ的方法(死信队列)
-
springboot整合rabbitmq 五种模式及死信队列的demo代码
-
SpringBoot2.x系列教程63--SpringBoot整合消息队列之RabbitMQ详解
-
springboot~rabbitmq自己通过UI手动发布队列需要注意的地方
-
springboot 整合 RabbitMQ实现消息队列
-
SpringBoot 动态监听RabbitMq队列
-
RabbitMQ(基于Spring)通过设置队列的过期时间延时时间,监听死信队列来实现延时取消订单
-
使用SpringBoot中提供的的RabbitMQ的API来编写一个消息队列的例子
-
springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息处理
-
springboot整合rabbitmq实现生产者消息确认、死信交换器、未路由到队列的消息处理