【问题标题】:Which RabbitAdmin is selected by default if "setAdminsThatShouldDeclare" is not called?如果不调用“setAdminsThatShouldDeclare”,则默认选择哪个 RabbitAdmin?
【发布时间】:2021-10-07 08:34:29
【问题描述】:

我想声明 QueueExchange,如果我不在 Queue 或 Exchange 上调用 setAdminsThatShouldDeclare,则会选择哪个 RabbitAdmin 实例:

@Bean
public Queue queue() {
    Queue queue = new Queue("foo");
    // queue.setAdminsThatShouldDeclare(admin1()); 
    return queue;
}

@Bean
public Exchange exchange() {
    DirectExchange exchange = new DirectExchange("bar");
    // exchange.setAdminsThatShouldDeclare(admin2());
    return exchange;
}

应用中有多个RabbitMQ连接(多个RabbitAdmin实例)

【问题讨论】:

    标签: spring-boot rabbitmq spring-amqp


    【解决方案1】:

    他们都将默认声明这个实体。

    RabbitAdmin中的逻辑是这样的:

    private <T extends Declarable> boolean declarableByMe(T dec) {
        return (dec.getDeclaringAdmins().isEmpty() && !this.explicitDeclarationsOnly) 
                || dec.getDeclaringAdmins().contains(this)
                || (this.beanName != null && dec.getDeclaringAdmins().contains(this.beanName));
    }
    

    另请参阅该属性说明:

    /**
     * Set to true to only declare {@link Declarable} beans that are explicitly configured
     * to be declared by this admin.
     * @param explicitDeclarationsOnly true to ignore beans with no admin declaration
     * configuration.
     * @since 2.1.9
     */
    public void setExplicitDeclarationsOnly(boolean explicitDeclarationsOnly) {
    

    这是一个关于此事的文档:https://docs.spring.io/spring-amqp/docs/current/reference/html/#conditional-declaration

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-03-20
      • 1970-01-01
      相关资源
      最近更新 更多