package com.bwie.auth.config;

import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * @ClassName:
 * @Description:  消息发送到交换件确认的  回调
 * @Created by 杨鹏
 */
@Component
public class ConfirmCallbackConfig implements RabbitTemplate.ConfirmCallback {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    /**
     * bean 初始化方法
     */
    @PostConstruct
    public void init() {
        this.rabbitTemplate.setConfirmCallback(this);
    }

    /**
     *  消息发送到交换机 成功 或者 失败 都会执行
     * @param correlationData correlation data for the callback.
     * @param ack true for ack, false for nack
     * @param cause An optional cause, for nack, when available, otherwise null.
     */
    @Override
    public void confirm(CorrelationData correlationData, boolean ack, String cause) {
        if (ack) {
            System.out.println("消息发送到交换机成功...");
        } else {
            System.out.println("消息发送到交换机失败，失败的原因：" + cause);
        }
    }

}
