动态创建 Exchange spring 启动 rabbitmq
Creating an Exchange dynamically spring boot rabbitmq
在我的应用程序中使用带有 Spring 引导的 rabbitmq -
如何在我的 spring 引导应用程序中创建一个 ExchangeType 定义为属性的 Exchange。
我看到 RabbitAdmin/AmqpAdmin 有一个方法“declareExchange”,但没有声明 ExchangeType 的选项.
我可以设置一些条件来检查 if else endif 是否基于 属性 值创建它,但我想避免这种情况。
您可以在 application.properties 文件中声明交换名称和路由键,并使用 @ConditionalOnProperty 有条件地创建交换和绑定 bean,如下所示:-
@Configuration
class AMQPConfig {
//the following beans creates Queues & Exchanges & Bindings programmatically
@Bean
public Queue queue(@Value("${my-queue}") String queueName) {
return new Queue(queueName, false);
}
@Bean
@ConditionalOnProperty(value = "my-direct-exchange")
DirectExchange directExchange(@Value("${my-direct-exchange}") String exchangeName) {
return new DirectExchange(exchangeName);
}
@Bean
@ConditionalOnProperty(value = "my-direct-exchange")
Binding directExchangeBinding(Queue queue, DirectExchange directExchange,
@Value("${direct-exchange-routing-key}") String routingKey) {
return BindingBuilder.bind(queue).to(directExchange).with(routingKey);
}
@Bean
@ConditionalOnProperty(value = "my-topic-exchange")
TopicExchange topicExchange(@Value("${my-topic-exchange}") String exchangeName) {
return new TopicExchange(exchangeName);
}
@Bean
@ConditionalOnProperty(value = "my-topic-exchange")
Binding topicExchangeBinding(Queue queue, TopicExchange topicExchange,
@Value("${topic-exchange-routing-key}") String routingKey) {
return BindingBuilder.bind(queue).to(topicExchange).with(routingKey);
}
}
application.properties
my-queue=rates
my-direct-exchange=my-direct-exchange
my-topic-exchange=my-topic-exchange
direct-exchange-routing-key=rates.euro
topic-exchange-routing-key=rates.*
如果您在属性文件中注释了 my-direct-exchange 属性,则不会创建直接交换及其绑定。
在我的应用程序中使用带有 Spring 引导的 rabbitmq -
如何在我的 spring 引导应用程序中创建一个 ExchangeType 定义为属性的 Exchange。
我看到 RabbitAdmin/AmqpAdmin 有一个方法“declareExchange”,但没有声明 ExchangeType 的选项.
我可以设置一些条件来检查 if else endif 是否基于 属性 值创建它,但我想避免这种情况。
您可以在 application.properties 文件中声明交换名称和路由键,并使用 @ConditionalOnProperty 有条件地创建交换和绑定 bean,如下所示:-
@Configuration
class AMQPConfig {
//the following beans creates Queues & Exchanges & Bindings programmatically
@Bean
public Queue queue(@Value("${my-queue}") String queueName) {
return new Queue(queueName, false);
}
@Bean
@ConditionalOnProperty(value = "my-direct-exchange")
DirectExchange directExchange(@Value("${my-direct-exchange}") String exchangeName) {
return new DirectExchange(exchangeName);
}
@Bean
@ConditionalOnProperty(value = "my-direct-exchange")
Binding directExchangeBinding(Queue queue, DirectExchange directExchange,
@Value("${direct-exchange-routing-key}") String routingKey) {
return BindingBuilder.bind(queue).to(directExchange).with(routingKey);
}
@Bean
@ConditionalOnProperty(value = "my-topic-exchange")
TopicExchange topicExchange(@Value("${my-topic-exchange}") String exchangeName) {
return new TopicExchange(exchangeName);
}
@Bean
@ConditionalOnProperty(value = "my-topic-exchange")
Binding topicExchangeBinding(Queue queue, TopicExchange topicExchange,
@Value("${topic-exchange-routing-key}") String routingKey) {
return BindingBuilder.bind(queue).to(topicExchange).with(routingKey);
}
}
application.properties
my-queue=rates
my-direct-exchange=my-direct-exchange
my-topic-exchange=my-topic-exchange
direct-exchange-routing-key=rates.euro
topic-exchange-routing-key=rates.*
如果您在属性文件中注释了 my-direct-exchange 属性,则不会创建直接交换及其绑定。