package com.tashow.cloud.mq.retry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 消息重试任务抽象实现
 *
 * @param <T> 失败记录类型
 * @author tashow
 */
public abstract class AbstractMessageRetryTask<T> {

    private static final Logger log = LoggerFactory.getLogger(AbstractMessageRetryTask.class);



    /**
     * 获取消息重试服务
     *
     * @return 消息重试服务
     */
    protected abstract MessageRetryService<T> getMessageRetryService();

    /**
     * 获取记录ID
     *
     * @param record 记录对象
     * @return 记录ID
     */
    protected abstract Integer getRecordId(T record);

    /**
     * 执行重试
     */
    public void retryFailedMessages() {
        try {
            List<T> unprocessedRecords = getMessageRetryService().getUnprocessedRecords();
            for (T record : unprocessedRecords) {
                    Integer recordId = getRecordId(record);
                    getMessageRetryService().retryFailedMessage(recordId);
            }
        } catch (Exception e) {
            log.error("[MQ重试] 执行消息重试任务异常", e);
        }
    }
} 