我有以下两个配置类:

@Configuration
@EnableRabbit
@Import({ LocalRabbitConfigA.class, CloudRabbitConfigA.class })
public class RabbitConfigA {
    @Autowired
    @Qualifier("rabbitConnectionFactory_A")
    private ConnectionFactory rabbitConnectionFactory;

    @Bean(name = "admin_A")
    AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(rabbitConnectionFactory);
    }

    @Bean(name = "Exchange_A")
    DirectExchange receiverExchange() {
        return new DirectExchange("Exchange_A", true, false);
    }
}




@Configuration
@EnableRabbit
@Import({ LocalRabbitConfigB.class, CloudRabbitConfigB.class })
public class RabbitConfigB {
    @Autowired
    @Qualifier("rabbitConnectionFactory_B")
    private ConnectionFactory rabbitConnectionFactory;

    @Bean(name = "admin_B")
    AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(rabbitConnectionFactory);
    }

    @Bean(name = "Exchange_B")
    DirectExchange receiverExchange() {
        return new DirectExchange("Exchange_B", true, false);
    }
}


请注意,LocalRabbitConfigA和LocalRabbitConfigB类定义了连接到不同VHost的connectionFactory。

启动应用程序时(在Tomcat中),所有Exchange均在两个VHost中创建。

问题是如何定义特定的ConnectionFactiory创建特定的Exchange /队列?

这样VHost A仅包含Exchange_A,而VHost B仅包含Exchange_B?

最佳答案

请参见conditional declaration

特别:

@Bean(name = "Exchange_B")
DirectExchange receiverExchange() {
    DirectExchange exchange = new DirectExchange("Exchange_B", true, false);
    exchange.setAdminsThatShouldDeclare(amqpAdmin());
    return exchange;
}

10-08 01:35