ConfigurationProperties 的 RabbitListener 注释队列名称 [英] RabbitListener annotation queue name by ConfigurationProperties
问题描述
我已经通过 application.yaml 和 spring configurationProperties 配置了我的兔子属性.因此,当我配置交换、队列和绑定时,我可以使用我的属性的 getter
I have configured my rabbit properties via application.yaml and spring configurationProperties. Thus, when I configure exchanges, queues and bindings, I can use the getters of my properties
@Bean Binding binding(Queue queue, TopicExchange exchange) {
return BindingBuilder.bind(queue).to(exchange).with(properties.getQueue());
}
@Bean Queue queue() {
return new Queue(properties.getQueue(), true);
}
@Bean TopicExchange exchange() {
return new TopicExchange(properties.getExchange());
}
但是,当我配置 @RabbitListener 以从队列中记录消息时,我必须使用完整的属性名称,如
However, when I configure a @RabbitListener to log the messages on from the queue, I have to use the full properties name like
@RabbitListener(queues = "${some.long.path.to.the.queue.name}")
public void onMessage(
final Message message, final Channel channel) throws Exception {
log.info("receiving message: {}#{}", message, channel);
}
我想避免这种容易出错的硬编码字符串,并参考 configurationProperties bean:
I want to avoid this error prone hard coded String and refer to the configurationProperties bean like:
@RabbitListener(queues = "${properties.getQueue()}")
我有一个 类似的问题,其中使用了 bean 引用的 @EventListener "@bean.method()" 有帮助,但它在这里不起作用,bean 表达式只是被解释为队列名称,失败是因为队列 namde "@bean...." 不存在.
I had a similar issue once with @EventListener where using a bean reference "@bean.method()" helped, but it does not work here, the bean expression is just interpreted as queue name, which fails because a queue namde "@bean...." does not exist.
是否可以将 ConfigurationProperty-Beans 用于 RabbitListener 队列配置?
Is it possible to use ConfigurationProperty-Beans for RabbitListener queue configuration?
推荐答案
这样的事情对我有用,我只使用了 Bean 和 SpEL.
Something like this worked for me where I just used the Bean and SpEL.
@Autowired
Queue queue;
@RabbitListener(queues = "#{queue.getName()}")
这篇关于ConfigurationProperties 的 RabbitListener 注释队列名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!