springboot中集成多个rabbitmq

package com.demo.config;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.amqp.core.*;
import org.springframework.amqp.rabbit.config.SimpleRabbitListenerContainerFactory;
import org.springframework.amqp.rabbit.connection.CachingConnectionFactory;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.listener.RabbitListenerContainerFactory;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.amqp.support.converter.MessageConverter;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;

import javax.annotation.Resource;



@Configuration
public class TestMQConfig {

    @Resource
    private ObjectMapper objectMapper;
   @Value("${spring.rabbitmq.host:10.1.1.1}")
    private  String host;
    @Value("${spring.rabbitmq.port:5672}")
    private  int port;
    @Value("${spring.rabbitmq.username:test}")
    private  String username;
    @Value("${spring.rabbitmq.virtual-host:test}")
    private  String virtualHost;
    @Value("${spring.rabbitmq.password:test}")
    private  String password;

    @Bean(name = "decorationConnectionFactory")
    @Primary
    public ConnectionFactory pmsConnectionFactory()  {
        return connectionFactory(host, port, username, password, virtualHost);
    }

    public CachingConnectionFactory connectionFactory(String host, int port, String username, String password, String virtual_host) {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        connectionFactory.setVirtualHost(virtual_host);
        return connectionFactory;
    }

    @Bean(name = "decorationRabbitListenerContainerFactory")
    @Primary
    public RabbitListenerContainerFactory pmsRabbitListenerContainerFactory(@Qualifier("decorationConnectionFactory") ConnectionFactory connectionFactory) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        factory.setConnectionFactory(connectionFactory);
        factory.setMessageConverter(messageConverter());
        return factory;
    }


    @Bean
    public MessageConverter messageConverter() {
        return new Jackson2JsonMessageConverter(objectMapper);
    }

    @Bean(name = "BPMRabbitMQTemplate")
    @Primary
    public RabbitTemplate rabbitTemplate(
            @Qualifier("decorationConnectionFactory") ConnectionFactory connectionFactory
    ) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(messageConverter());
        return rabbitTemplate;
    }


    /**
     * 定义队列
     *
     * @return
     */
    @Bean
    public Queue BPMQueue() {
        return new Queue("bpmstart.entry.group.0");
    }

    /**
     * 定义交换机
     *
     * @return
     */
    @Bean
    TopicExchange BPMExchange() {
        return new TopicExchange("bpmstart.entry");
    }


    /**
     * 交换机绑定队列
     *
     * @return
     */
    @Bean
    Binding bindingExchangeHandleEnergyMeter() {
        return BindingBuilder.bind(BPMQueue()).to(BPMExchange()).with("BPM");
    }

}

 

posted @ 2021-06-11 16:17  甜菜波波  阅读(960)  评论(0编辑  收藏  举报