package com.casic.missiles.modular.system.redis;

import com.casic.missiles.config.DeviceRedisProperties;
import com.casic.missiles.core.util.SpringContextHolder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import redis.clients.jedis.Protocol;


/**
 * Created by test203 on 2019/4/17.
 */
public class RedisPublisherUtil {
    private final Logger logger = LoggerFactory.getLogger(this.getClass().getName());
    private static RedisPublisherUtil util = null;
    private static RedisPublisher redisPublisher;
    private JedisPool jedisPool;
    private RedisPublisherUtil() {
        if (null == jedisPool) {
            DeviceRedisProperties deviceRedisProperties = SpringContextHolder.getBean("deviceRedisProperties");
            String messageHost = deviceRedisProperties.getHost();
            String password = deviceRedisProperties.getPassword();
            int messagePort = Integer.parseInt(deviceRedisProperties.getPort());
            jedisPool = new JedisPool(new JedisPoolConfig(), messageHost, messagePort, Protocol.DEFAULT_TIMEOUT,password);
        }
        redisPublisher = new RedisPublisher(jedisPool.getResource());    //发布者
    }

    public static RedisPublisherUtil getInstance() {
        if (null == util)
            util = new RedisPublisherUtil();
        return util;
    }

    public void publishMessage(String content) {
        if (null != redisPublisher) {
            redisPublisher.startPublish(content);
            jedisPool.getResource().close();
        } else {
            logger.debug("发布服务未建立！！！" + content);
        }
    }


    public void publishDigMessage(String content) {
        if (null != redisPublisher) {
            redisPublisher.startDigPublish(content);
            jedisPool.getResource().close();
        } else {
            logger.debug("开挖发布服务未建立！！！" + content);
        }
    }
}
