Springboot集成RabbitMq@RabbitListener不自动生成队列
时间: 2023-12-08 22:37:10 浏览: 130
rabbitmq根据配置文件动态生成队列
以下是Springboot集成RabbitMq@RabbitListener不自动生成队列的解决方法:
1. 确保已经定义了RabbitAdmin的Bean,并且已经将其注入到了ConnectionFactory中。可以参考如下代码:
```java
@Configuration
@EnableRabbit
public class RabbitConfig {
@Bean
public RabbitAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
RabbitAdmin rabbitAdmin = new RabbitAdmin(connectionFactory);
// 服务启动时候开启自动启动
rabbitAdmin.setAutoStartup(true);
return rabbitAdmin;
}
@Bean
public SimpleRabbitListenerContainerFactory rabbitListenerContainerFactory(ConnectionFactory connectionFactory) {
SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
factory.setConnectionFactory(connectionFactory);
factory.setConcurrentConsumers(3);
factory.setMaxConcurrentConsumers(10);
factory.setAcknowledgeMode(AcknowledgeMode.MANUAL);
return factory;
}
}
```
2. 确保在@RabbitListener注解中指定了正确的队列名称。例如:
```java
@RabbitListener(queues = "testQueue")
public void receiveMessage(String message) {
System.out.println("Received message: " + message);
}
```
3. 确保在@RabbitListener注解中指定了正确的Exchange和RoutingKey。例如:
```java
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = "testQueue", durable = "true"),
exchange = @Exchange(value = "testExchange", type = ExchangeTypes.TOPIC),
key = "testRoutingKey"
))
public void receiveMessage(String message) {
System.out.println("Received message: " + message);
}
```
阅读全文