package com.hdkj.lt.core.mq.callback;

import com.aliyun.openservices.ons.api.OnExceptionContext;
import com.aliyun.openservices.ons.api.SendCallback;
import com.aliyun.openservices.ons.api.SendResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @Description: 发送消息的回调类，打印日志
 * @Author: wenhao
 * @Create: 2024/06/06 9:07
 * @Version: 1.0
 */
@Slf4j
//@Component //加此注解是为了方便从Spring IOC中获取，后期如果使用其他SendCallback时需要注入其他IOC中的对象时，RocketMQTemplate中就不方便用反射方式创建SendCallback了
public class PrintLogSendCallback implements SendCallback {

    @Override
    public void onSuccess(SendResult sendResult) {
        log.info("消息发送成功，当前时间：{}，消息ID：{}", LocalDateTime.now(), sendResult.getMessageId());
    }

    @Override
    public void onException(OnExceptionContext onExceptionContext) {
        log.info("消息发送失败，当前时间：{}，消息ID：{}，错误信息：{}", LocalDateTime.now(), onExceptionContext.getMessageId(), onExceptionContext.getException());
    }

}
