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

import redis.clients.jedis.Jedis;

import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * Created by test203 on 2019/4/17.
 */
public class RedisPublisher {
    private final String channel_Name = "Config";
    private final String channel_digName = "DigGroup";
    private final Jedis publisherJedis;

    public RedisPublisher(Jedis publisherJedis) {
        this.publisherJedis = publisherJedis;
    }


    public void startPublish(String content) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content.getBytes())));
            String lineTxt = null;
            while ((lineTxt = reader.readLine()) != null) {

                publisherJedis.publish(channel_Name, lineTxt);

            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    public void startDigPublish(String content) {
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(content.getBytes())));
            String lineTxt = null;
            while ((lineTxt = reader.readLine()) != null) {

                publisherJedis.publish(channel_digName, lineTxt);

            }
            reader.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

//    @Override
//    public void run() {
//        BufferedReader reader = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(sendMsg.getBytes())));
//        Jedis jedis = jedisPool.getResource();   //连接池中取出一个连接
//
//        while (true) {
//            String line = null;
//            try {
//                line = reader.readLine();
//                if (!"quit".equals(line)) {
//                    jedis.publish(channel_Name, line);   //从 channel_Name 的频道上推送消息
//                } else {
//                    break;
//                }
//            } catch (IOException e) {
//                e.printStackTrace();
//            }
//        }
//    }
}
