package com.lovecoding.rabbitmq.consumer;

import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ConsumerTemplate {

    //自动应答消息， 自动处理消息
    @RabbitListener(queues = "testQueue")
    public void t1( String msg ){
        System.out.println( "T1 消费了消息： " + msg );
    }

    //自动应答消息， 自动处理消息
    @RabbitListener(queues = "testQueue")
    public void t2( String msg ){
        System.out.println( "T2 消费了消息： " + msg );
    }

    /**
     * 广播模式 能接收所有消息
     * 只要 队列和交换机绑定了 就能收消息
     * @param msg
     */
    @RabbitListener(queues = "fanoutDemo")
    public void t3( String msg ){
        System.out.println( "T3 消费了消息 ：" + msg );
    }

    /**
     * 广播模式 能接收所有消息
     * 只要 队列和交换机绑定了 就能收消息
     * @param msg
     */
    @RabbitListener(queues = "fanoutDemoT4")
    public void t4( String msg ){
        System.out.println( "T4 消费了消息 ：" + msg );
    }

    /**
     * Direct 消息模式 routeKey 是 北京
     * 只接收 key 是北京的消息
     * @param msg
     */
    @RabbitListener(queues = "directQueue")
    public void t5( String msg ) {
        System.out.println( "北京天气：" + msg );
    }

    /**
     * Direct 消息模式 routeKey 是 哈尔滨
     * 只接收 key 是哈尔滨的消息
     * @param msg
     */
    @RabbitListener(queues = "directQueueT6")
    public void t6( String msg){
        System.out.println( "哈尔滨天气：" + msg );
    }

    @RabbitListener(queues = "topicQueue")
    public void t7( String msg){
        System.out.println( "北京天气：" + msg );
    }

    @RabbitListener(queues = "topicQueueT8")
    public void t8( String msg){
        System.out.println( "哈尔滨天气：" + msg );
    }

    @RabbitListener(queues = "topicQueueT9")
    public void t9( String msg){
        System.out.println( "通用天气：" + msg );
    }

    @RabbitListener(queues = "deadQueueDemo")
    public void t10(Message msg, Channel channel){

        long deliveryTag = msg.getMessageProperties().getDeliveryTag();

        try {
            channel.basicNack(deliveryTag, false, false);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println( "收到消息：" + msg );
    }

//    @RabbitListener(queues = "deadQueue")
//    public void t11(String msg){
//        System.out.println( "收到死信：" + msg );
//    }

}
