0

我有以下兩個配置類:如何在Spring RabbitMQ項目中使用多個虛擬主機?

@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類定義它連接到不同的虛擬主機ConnectionFactory的。
當啓動應用程序時(在Tomcat中),所有交換都在中創建,兩者均爲 VHosts。

問題是如何定義某個特定的Exchange/Queue是由特定的ConnectionFactiory創建的?

因此,VHost A只包含Exchange_A,而VHost B只包含Exchange_B?

回答

1

請參閱conditional declaration

具體做法是:

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

我剛剛發現這個還可以,但也不敢這麼快回答我的問題......但無論如何,非常感謝! –

相關問題