package tech.easyflow.common.mq.support;

import org.springframework.data.redis.connection.RedisConnectionFactory;
import tech.easyflow.common.mq.exception.MQException;

public class MQHealthSupport {

    private final RedisConnectionFactory redisConnectionFactory;

    public MQHealthSupport(RedisConnectionFactory redisConnectionFactory) {
        this.redisConnectionFactory = redisConnectionFactory;
    }

    public boolean available() {
        try (var connection = redisConnectionFactory.getConnection()) {
            String pong = connection.ping();
            return pong != null;
        } catch (Exception e) {
            return false;
        }
    }

    public void assertAvailable() {
        if (!available()) {
            throw new MQException("MQ Redis 不可用");
        }
    }
}
