package com.persagy.server.jms;

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.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Created by Administrator on 2018/4/10.
 * 1. yaml
 * 2. RabbitMqConfig
 *      a  创建queue
 *      b. 创建交换机TopicExchange
 *      c. 根据绑定规则将队列绑定到相应的交换机上
 * 3. sender
 * 4. test
 * 5. receiver
 */

@Configuration
public class RabbitMqConfig {
 
    //  a  创建queue
    @Bean(name = "message") //  指定该参数名是message  见下面Qualifier（"message"）
    public Queue queueMessage(){
        return new Queue("topic.message");  //  topic.message  是rounting-key,匹配规则
    }
 
    @Bean(name = "notes")
    public Queue queueMessages(){
        return new Queue("topic.notes");
    }
 
    //  b. 创建交换机TopicExchange
    @Bean
    public TopicExchange exchange(){
        return new TopicExchange("topicExchange");
    }
 
    /*
      c. 根据绑定规则将队列绑定到相应的交换机上（bindingKey）--Exchange Queues
 
     */
    @Bean
    public Binding bindingExchangeMessage(@Qualifier("message") Queue queueMessage, TopicExchange exchange){
        return BindingBuilder.bind(queueMessage).to(exchange).with("topic.message");
    }
 
    @Bean
    /**
     * 将队列"messages" 绑定到交换机上，绑定规则是 topic.messages
     *
     */
    public  Binding bindingExchangeMessages(@Qualifier("notes")Queue queueMessages,TopicExchange exchange){
        return BindingBuilder.bind(queueMessages).to(exchange).with("topic.#");
    }
 
}