spring-boot - 通过 ConfigurationProperties 的 RabbitListener 注释队列名称

标签 spring-boot rabbitmq

我已经通过 application.yaml 和 spring configurationProperties 配置了我的 rabbit 属性。 因此,当我配置交换、队列和绑定(bind)时,我可以使用我的属性的 getter

@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 以从队列中记录消息时,我必须使用完整的属性名称,例如

 @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,如:

@RabbitListener(queues = "${properties.getQueue()}") 

我有一个 similar issue曾经与@EventListener 一起使用bean 引用“@bean.method()”有帮助,但它在这里不起作用,bean 表达式只是被解释为队列名称,因为队列名称“@bean ....”而失败不存在。

是否可以使用 ConfigurationProperty-Beans 进行 RabbitListener 队列配置?

最佳答案

在我刚刚使用 Bean 和 SpEL 的情况下,类似的方法对我有用。

@Autowired
Queue queue;

@RabbitListener(queues = "#{queue.getName()}")

关于spring-boot - 通过 ConfigurationProperties 的 RabbitListener 注释队列名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49055634/

相关文章:

java - Spring Boot Maven 插件 : Exclude properties file from final jar

java - 如何在java spring websocket中实现ping pong

java - 带有 @JsonProperty defaultValue 处理的 Jackson 模块

python - 多服务器生产环境中的django-celery

java - Spring API REST 和 Cors 以及 AngularJS

spring-boot - Spring Boot中JMS消费者的动态扩展

java - 执行器服务 RabbitMQ 中只有一个线程同时运行

java - 如何运行 RabbitMQ Java 接收器?

reflection - 定义具有非常多消息类型的消息传递域

docker - 如何修复我的 docker-compose.yml 我收到的rabbitmq.config 是目录错误?