来自数据库的RabbitMQ配置

时间:2018-07-17 09:22:41

标签: spring-mvc rabbitmq

我对rabbit MQ in spring MVC具有以下配置。如您所见,我在以下配置中定义了exchange name, queue and routing key name,并将其定义为静态。

但是我需要从数据库绑定这个参数。

例如:

将以QUEUE_MASTER WITH PARAMETER (QUEUE_NAME, EXCHANGE_NAME, ROUTING_KEY)的形式创建一个表,并将根据表数据以动态方式创建以下配置。

请提出对RabbitMQ动态配置最好的选择。甚至我也在connectionFactory中传递了多个IP,但不知道如何在connectionFactory中传递多个IP。

@Configuration
public class RabbitMqConfiguration implements RabbitListenerConfigurer {

    public static final String EXCHANGE_NAME = "appExchange";

    public static final String QUEUE_GENERIC_NAME = "appGenericQueue";

    public static final String QUEUE1 = "Queue1";

    public static final String QUEUE2 = "Queue2";

    public static final String ROUTING_KEY = "messages.key";

    @Bean
    public TopicExchange appExchange() {
        return new TopicExchange(EXCHANGE_NAME);
    }

    @Bean
    public Queue appQueueGeneric() {
        return new Queue(QUEUE_GENERIC_NAME);
    }

    @Bean
    public Queue appQueue1() {
        return new Queue(QUEUE1);
    }

    @Bean
    public Queue appQueue2() {
        return new Queue(QUEUE2);
    }

    @Bean
    public Binding declareBindingGeneric() {
        return BindingBuilder.bind(appQueueGeneric()).to(appExchange()).with(ROUTING_KEY);
    }

    @Bean
    public Binding declareBindingQueue1() {
        return BindingBuilder.bind(appQueue1()).to(appExchange()).with(ROUTING_KEY);
    }

    @Bean
    public Binding declareBindingQueue2() {
        return BindingBuilder.bind(appQueue2()).to(appExchange()).with(ROUTING_KEY);
    }

    @Bean
    public ConnectionFactory connectionFactory() {
        return new CachingConnectionFactory("localhost");
    }

    @Bean
    public AmqpAdmin amqpAdmin() {
        return new RabbitAdmin(connectionFactory());
    }

    @Bean
    public RabbitTemplate rabbitTemplate() {
        return new RabbitTemplate(connectionFactory());
    }

    @Bean
    public Jackson2JsonMessageConverter producerJackson2MessageConverter() {
        return new Jackson2JsonMessageConverter();
    }
}

0 个答案:

没有答案