package com.muyu.common.rabbit.callback;

import jakarta.annotation.PostConstruct;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.stereotype.Component;

/**
 * @author Lenovo
 * @Description 消息发送失败时回调
 */

@Slf4j
@Component
public class ReturnsCallback implements RabbitTemplate.ReturnsCallback {

    @Resource
    private RabbitTemplate rabbitTemplate;

    /**
     * 初始化
     */
    @PostConstruct
    public void init() {
        rabbitTemplate.setReturnsCallback(this);
    }

    /**
     * 消息发送到队列失败时执行
     * @param returnedMessage 返回的消息和元数据
     */
    @Override
    public void returnedMessage(ReturnedMessage returnedMessage) {
        log.info("消息：{}被交换机：{}回退！回退原因：{}", returnedMessage.getMessage().toString(),
                returnedMessage.getExchange(), returnedMessage.getReplyText());
    }
}
