package com.gz;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

/**
 * 消费者
 */
public class MsgProducter {
    private final static String QUEUE_NAME = "ARCHIVE_OCR_QUEUE";
    private final static String EXCHANGES_NAME = "ARCHIVE_DATA_EXCHANGE";

    public static void main(String[] argv) throws Exception {
        // 创建连接工厂
        ConnectionFactory factory = new ConnectionFactory();
        // 设置RabbitMQ地址
        factory.setHost("47.97.217.112");
        factory.setUsername("admin");
        factory.setPassword("admin");
        factory.setPort(5672);
        // 创建一个新的连接
        Connection connection = factory.newConnection();
        // 创建一个通道
        final Channel channel = connection.createChannel();
        HashMap<String, Object> arg = new HashMap<String, Object>() {{
            put("x-dead-letter-exchange", "ARCHIVE_DLX_DATA_EXCHANGE");
            put("x-dead-letter-routing-key", "DLX_ARCHIVE_OCR");
        }};
        // 声明要关注的队列
        channel.queueDeclare(QUEUE_NAME, true, false, false, arg);
        channel.basicQos(1);//保证一次只分发一个
        System.out.println("Customer Waiting Received messages");

        // DefaultConsumer类实现了Consumer接口，通过传入一个频道，
        // 告诉服务器我们需要那个频道的消息，如果频道中有消息，就会执行回调函数handleDelivery
        Consumer consumer = new DefaultConsumer(channel) {
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties,
                                       byte[] body) throws IOException {
                String message = new String(body, StandardCharsets.UTF_8);
                System.out.println("Customer Received '" + message + "'");
            }
        };

        // 自动回复队列应答 -- RabbitMQ中的消息确认机制
        channel.basicConsume(QUEUE_NAME, true, consumer);
    }
}
