可在 Spring 应用程序中使用 Rabbitmq 声明
Declarabes in Spring application using Rabbit MQ
我正在使用 spring aqmp 版本 2.2.6 创建到主题交换的多个绑定。
我从讨论和参考文档中了解到我可以使用可声明的。
但在我的例子中,我将应用程序配置中的路由键作为逗号分隔并且不断增长。
我将如何使用配置动态创建可声明的 bean。 ?
下面是应用程序配置条目。
rabbit.MQ_SUBSCRIBER_ROUTING_KEY = my.#,test.#
路由键将在服务需要时添加。
我应该循环 rabbit.MQ_SUBSCRIBER_ROUTING_KEY 并调用 bean 函数吗?
my.queues=foo,bar,baz
Spring 可以将其转换为 List<String>
.
@Bean
public Declarables queues(@Value("${my.queues}") List<String> queues) {
return new Declarables(queues
.stream()
.map(q -> new Queue(q))
.collect(Collectors.toList())
.toArray(new Queue[0]));
}
编辑
完整图片:
@Bean
public Declarables declarables(@Value("${my.queues}") List<String> queues,
@Value("${my.exchange}") String exch,
@Value("${my.routing.keys}") List<String> routingKeys) {
List<Declarable> declarables = queues
.stream()
.map(q -> new Queue(q))
.collect(Collectors.toList());
declarables.add(new DirectExchange(exch));
for (int i = 0; i < routingKeys.size(); i++) {
declarables.add(new Binding(queues.get(i), DestinationType.QUEUE, exch,
routingKeys.get(i), null));
}
return new Declarables(declarables
.toArray(new Declarable[0]));
}
my.queues=foo,bar,baz
my.exchange=qux
my.routing.keys=rk1,rk2,rk3
我正在使用 spring aqmp 版本 2.2.6 创建到主题交换的多个绑定。
我从讨论和参考文档中了解到我可以使用可声明的。
但在我的例子中,我将应用程序配置中的路由键作为逗号分隔并且不断增长。 我将如何使用配置动态创建可声明的 bean。 ? 下面是应用程序配置条目。 rabbit.MQ_SUBSCRIBER_ROUTING_KEY = my.#,test.#
路由键将在服务需要时添加。
我应该循环 rabbit.MQ_SUBSCRIBER_ROUTING_KEY 并调用 bean 函数吗?
my.queues=foo,bar,baz
Spring 可以将其转换为 List<String>
.
@Bean
public Declarables queues(@Value("${my.queues}") List<String> queues) {
return new Declarables(queues
.stream()
.map(q -> new Queue(q))
.collect(Collectors.toList())
.toArray(new Queue[0]));
}
编辑
完整图片:
@Bean
public Declarables declarables(@Value("${my.queues}") List<String> queues,
@Value("${my.exchange}") String exch,
@Value("${my.routing.keys}") List<String> routingKeys) {
List<Declarable> declarables = queues
.stream()
.map(q -> new Queue(q))
.collect(Collectors.toList());
declarables.add(new DirectExchange(exch));
for (int i = 0; i < routingKeys.size(); i++) {
declarables.add(new Binding(queues.get(i), DestinationType.QUEUE, exch,
routingKeys.get(i), null));
}
return new Declarables(declarables
.toArray(new Declarable[0]));
}
my.queues=foo,bar,baz
my.exchange=qux
my.routing.keys=rk1,rk2,rk3