本文介绍了如何在Spring RabbitMQ项目中使用多个虚拟主机?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下两个 Configuration 类:

@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均在两个虚拟主机中创建.

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.

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

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

以便VHost A仅包含Exchange_A,而VHost B仅包含Exchange_B吗?

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

推荐答案

请参见条件声明.

特别是:

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

这篇关于如何在Spring RabbitMQ项目中使用多个虚拟主机?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-06 06:55