修改springboot配置文件让rocketMq不生效且@RocketMQMessageListener不报错
时间: 2023-12-20 09:03:56 浏览: 262
spring boot 集成 RocketMq消息中间件
要让 RocketMQ 不生效,需要在 Spring Boot 的配置文件中将 RocketMQ 的相关配置属性设置为 false 或为空。
比如,如果你使用了 RocketMQ 的 Starter,那么可以在 application.properties 或 application.yml 文件中添加以下配置:
```
# RocketMQ
rocketmq.name-server=127.0.0.1:9876
rocketmq.producer.group=my-group
rocketmq.consumer.group=my-group
rocketmq.consumer.topics=my-topic
rocketmq.consumer.tag=*
rocketmq.consumer.consume-thread-max=64
rocketmq.consumer.consume-message-batch-max-size=64
rocketmq.consumer.pull-interval=0
rocketmq.consumer.pull-threshold-for-queue=1000
rocketmq.consumer.pull-mode=0
rocketmq.consumer.consume-timeout=15
rocketmq.consumer.max-reconsume-times=16
rocketmq.listener.consume-mode=concurrently
rocketmq.listener.consume-thread-max=64
```
将以上配置属性都设置为 false 或为空即可,例如:
```
# RocketMQ
rocketmq.name-server=
rocketmq.producer.group=
rocketmq.consumer.group=
rocketmq.consumer.topics=
rocketmq.consumer.tag=
rocketmq.consumer.consume-thread-max=
rocketmq.consumer.consume-message-batch-max-size=
rocketmq.consumer.pull-interval=
rocketmq.consumer.pull-threshold-for-queue=
rocketmq.consumer.pull-mode=
rocketmq.consumer.consume-timeout=
rocketmq.consumer.max-reconsume-times=
rocketmq.listener.consume-mode=
rocketmq.listener.consume-thread-max=
```
这样就可以让 RocketMQ 不生效了。
如果你还有使用 `@RocketMQMessageListener` 注解的代码,可以在类或方法上使用 `@ConditionalOnProperty` 注解,判断 RocketMQ 相关配置属性是否为 false 或为空,如果是,则跳过对应的代码,例如:
```java
@Component
@ConditionalOnProperty(prefix = "rocketmq", name = "name-server", havingValue = "")
public class MyRocketMQListener {
@RocketMQMessageListener(topic = "my-topic", consumerGroup = "my-group")
public void onMessage(MessageExt message) {
// do something
}
}
```
这样,如果 RocketMQ 的 name-server 属性为空,则 MyRocketMQListener 类不会被实例化,即不会报错。
阅读全文