package com.pojo.prj.config;

import com.rabbitmq.client.Channel;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
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.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.amqp.SimpleRabbitListenerContainerFactoryConfigurer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

@Data
@Component("mq2RabbitmqConfig")
@ConfigurationProperties(prefix = "spring.rabbitmq.mq2") //读取mq2的配置信息
@Slf4j
@ConditionalOnProperty(name = "spring.rabbitmq.mq2.enable", havingValue = "true") //是否启用
public class MQ2RabbitConfiguration {

    public static final String UAV_QUEUE = "uav_msg_queue.yuanhang";

    public static final String UAV_QUEUE_EXCHANGE = "uav_msg_queue.yuanhang_exchange";

    private String host;
    private Integer port;
    private String username;
    private String password;

    @Autowired
    private ReturnCallBack2 returnCallBack2;
    @Autowired
    private ConfirmCallBack2 confirmCallBack2;

    @Bean(name = "mq2ConnectionFactory")   //命名mq1的ConnectionFactory,如果项目中只有一个mq则不必如此
    public ConnectionFactory createConnectionFactory() {
        CachingConnectionFactory connectionFactory = new CachingConnectionFactory();
        connectionFactory.setHost(host);
        connectionFactory.setPort(port);
        connectionFactory.setUsername(username);
        connectionFactory.setPassword(password);
        //开启发送到交换机和队列的回调
        connectionFactory.setPublisherConfirmType(CachingConnectionFactory.ConfirmType.CORRELATED);
//
//        Channel channel = connectionFactory.createConnection().createChannel(false);
//        // 声明queue,exchange,以及绑定
//        try {
//            channel.exchangeDeclare(UAV_QUEUE_EXCHANGE /* exchange名称 */, "topic"/* 类型 */);
//            // durable,exclusive,autodelete
//            channel.queueDeclare(UAV_QUEUE, true, false, false, null); // (如果没有就)创建Queue
//            channel.queueBind(UAV_QUEUE, UAV_QUEUE_EXCHANGE, "*");
//        } catch (Exception e) {
//            log.error("mq declare queue exchange fail ", e);
//        } finally {
//            try {
//                channel.close();
//            } catch (Exception e) {
//                log.error("mq channel close fail", e);
//            }
//
//        }
        return connectionFactory;
    }

    @Bean(name = "mq2RabbitTemplate") //命名mq1的RabbitTemplate,如果项目中只有一个mq则不必如此
    public RabbitTemplate brainRabbitTemplate(@Qualifier("mq2ConnectionFactory") ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        //发送消息时设置强制标志,仅当提供了returnCallback时才适用
        rabbitTemplate.setMandatory(true);
        //确保消息是否发送到交换机,成功与失败都会触发
        rabbitTemplate.setConfirmCallback(confirmCallBack2);
        //确保消息是否发送到队列,成功发送不触发,失败触发
        rabbitTemplate.setReturnCallback(returnCallBack2);
        return rabbitTemplate;
    }

    @Bean(name = "simpleRabbitListenerContainerFactory2")
    public SimpleRabbitListenerContainerFactory secondFactory(
            SimpleRabbitListenerContainerFactoryConfigurer configurer,
            @Qualifier("mq2ConnectionFactory") ConnectionFactory connectionFactory
    ) {
        SimpleRabbitListenerContainerFactory factory = new SimpleRabbitListenerContainerFactory();
        configurer.configure(factory, connectionFactory);
        return factory;
    }


    @Bean(name = "uavTopicExchange")
    public TopicExchange topicExchange() {
        return new TopicExchange(UAV_QUEUE_EXCHANGE);
    }

    @Bean(name = "uavTopicQueue")
    public Queue topicQueue() {
        Queue queue = new Queue(UAV_QUEUE, true, false, false, null);
        return queue;
    }


    @Bean(name = "uavTopicBinding")
    public Binding topicBinding(@Qualifier("uavTopicQueue") Queue queue, @Qualifier("uavTopicExchange") TopicExchange exchange) {
        return BindingBuilder.bind(queue).to(exchange).with(UAV_QUEUE);
    }


}

更多推荐