package com.itheima.mq.rabbitmq;

import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageProperties;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.ConfigurableApplicationContext;

import java.util.HashMap;

/**
 * 启动类
 */
@SpringBootApplication
public class RabbitmqApplication {

    // 发送消息的模板类
    private static RabbitTemplate rabbitTemplate ;

    public static void main(String[] args) {

        // 启动程序
        ConfigurableApplicationContext applicationContext = SpringApplication.run(RabbitmqApplication.class, args);
        rabbitTemplate = applicationContext.getBean(RabbitTemplate.class);

        // 查看AMQP协议生产者消息流转过程
        // producerMessageTransport() ;

        // 测试FanoutExchange交换机的消息发送
        // fanoutExchangeMessageTransport() ;

        // 测试Direct类型的交换机
        // directExchangeMessageTransport();

        // 测试Topic类型的交换机的消息发送
        // topicExchangeMessageTransport() ;

        // 测试headers交换机的消息发送
        // headersExchangeMessageTransport();

    }

    // 测试headers交换机的消息发送
    private static void headersExchangeMessageTransport() {
        String message = "headers exchanges message" ;
        MessageProperties messageProperties = new MessageProperties() ;
        // messageProperties.setHeader("key1" , "value1");
        messageProperties.setHeader("key2" , "value2");
        Message obj = new Message(message.getBytes() , messageProperties) ;
        rabbitTemplate.convertAndSend("headers.exchange" , "" , obj);
    }

    // 测试Topic类型的交换机的消息发送
    private static void topicExchangeMessageTransport() {

        // routingKey: topic.email.queue  ---> topic.email
        // routingKey: topic.sms.queue  ---> topic.sms
        // routingKey: topic.email.sms  ---> topic.email && topic.sms
        rabbitTemplate.convertAndSend("topic.exchange" , "topic.sms.queue" , "topic exchange 测试数据");
    }

    // 测试Direct类型的交换机
    private static void directExchangeMessageTransport() {
        rabbitTemplate.convertAndSend("direct.exchange" , "delete" , "direct exchange 测试数据");
    }

    // 测试FanoutExchange交换机的消息发送
    private static void fanoutExchangeMessageTransport() {
        rabbitTemplate.convertAndSend("fanout.exchange" , "itheima" , "fanout exchange 测试数据");
    }

    // 查看AMQP协议生产者消息流转过程测试代码
    private static void producerMessageTransport() {
        rabbitTemplate.convertAndSend("fanout.exchange" , "" , "rabbitmq生产者消息流程过程测试");
    }

}
