【发布时间】:2020-11-16 11:38:12
【问题描述】:
我有一个 Spring Boot 应用程序,我的目标是在应用程序启动时声明队列、交换和绑定。应用程序将向各种队列生成消息,应用程序上没有消费者。
我已将这些依赖项包含在我的 pom.xml 中
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework.amqp</groupId>
<artifactId>spring-rabbit</artifactId>
<version>2.2.12.RELEASE</version>
</dependency>
我的配置类
@Configuration
public class RabbitConfiguration {
@Bean
public ConnectionFactory connectionFactory() {
CachingConnectionFactory connectionFactory = new CachingConnectionFactory("myhost", 5672);
connectionFactory.setUsername("example_name");
connectionFactory.setPassword("example_pass");
return connectionFactory;
}
@Bean
public AmqpAdmin rabbitAdmin(ConnectionFactory connectionFactory) {
return new RabbitAdmin(connectionFactory);
}
@Bean
public Queue declareQueue() {
return new Queue("test_queue", true, false, false);
}
@Bean
public DirectExchange declareDirectExchange() {
return new DirectExchange("test_direct_exchange", true, false);
}
@Bean
public Declarables declareBindings() {
return new Declarables(
new Binding("test_queue", DestinationType.QUEUE, "test_direct_exchange", "test_routing_key", null)
);
}
}
我的问题是队列、交换和绑定不是在应用程序启动时创建的。 Spring Boot 甚至不打开连接。只有当我向队列生成消息时,才会创建连接、队列等。
【问题讨论】:
标签: java spring spring-boot rabbitmq spring-amqp