How to use multiple vhosts in a Spring RabbitMQ project?

拟墨画扇 提交于 2019-12-11 04:38:04

问题


I've the following two Configuration classes:

@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);
    }
}

And

@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);
    }
}

Note that the LocalRabbitConfigA and LocalRabbitConfigB classes define the connectionFactory which connects to a different VHost.
When starting the application (within Tomcat), all the Exchanges are created in both VHosts.

The question is how to define that a certain Exchange/Queue is created by a specific ConnectionFactiory ?

So that VHost A contains only the Exchange_A, and VHost B only Exchange_B ?


回答1:


See conditional declaration.

Specifically:

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


来源:https://stackoverflow.com/questions/39930385/how-to-use-multiple-vhosts-in-a-spring-rabbitmq-project

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!