package com.muyu.processing.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;

/**
 * 确认回调配置
 */
@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("消息发送到 broker 成功");
        } else {
            System.out.println("消息发送到 broker 失败，失败的原因：" + cause);
        }
    }

}
