package com.inspect.metadata.mqutil;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ConfirmCallback;
import org.springframework.amqp.rabbit.core.RabbitTemplate.ReturnCallback;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;

@Component
public class Sender {
    private Logger log = LoggerFactory.getLogger(Sender.class);

    @Resource
    private RabbitTemplate rabbitTemplate;
    final ConfirmCallback confirmCallback = (correlationData, ack, cause) -> {
        log.info("correlationData: {}", correlationData);
        log.info("confirm ack: {}", ack);
        if (!ack) {
            log.error("Exception handled");
        }

    };
    final ReturnCallback returnCallback = (message, replyCode, replyText, exchange, routingKey) -> log.error("return exchange: " + exchange + ", routingKey: " + routingKey + ", replyCode: " + replyCode + ", replyText: " + replyText);

    public void sendclass(String order, String exchange, String routingKey) throws Exception {
        this.rabbitTemplate.setConfirmCallback(this.confirmCallback);
        this.rabbitTemplate.setReturnCallback(this.returnCallback);
        this.rabbitTemplate.convertAndSend(exchange, routingKey, order);
    }
}
