package com.pqs9900.listener;


import com.njcn.utils.Kafka.ZookeeperServerStatus;
import com.njcn.utils.PubUtils;
import com.pqs9900.controller.configuration.ConstantValueController;
import com.pqs9900.util.Kafka.JavaKafkaConsumer;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import java.io.InputStream;
import java.util.Properties;

public class ContextListener implements ServletContextListener {

	private JavaKafkaConsumer javaKafkaConsumer = null;
	// 日志记录
    private static final Logger logger = LoggerFactory.getLogger(ConstantValueController.class);


	@Override
	public void contextInitialized(ServletContextEvent sce) {
		/*try {
			 //读取配置文件
			 Properties pros = PubUtils.readProperties(getClass().getClassLoader(),"java.properties");
			 String zookeeper = pros.get("ZOOKEEPER").toString();
	         String groupId =  pros.get("GROUPID").toString();
	         String topic = pros.get("TOPIC").toString();
	         String kafka_log = pros.get("KAFKA_LOG").toString();
             //192.168.1.12:2181
			 String[] strArray=zookeeper.split(":");
			 if(strArray.length == 2){
				 String host =strArray[0];
			     int port =Integer.parseInt(strArray[1]);
				 ZookeeperServerStatus zookeeperServerStatus=new ZookeeperServerStatus();
				 String strModel = zookeeperServerStatus.serverStatus(host,port);
				 //不为空表示能获取到相对应的模式，则启动Kafka监听
				 if(!strModel.equals("")){
					 int threads = 1;
					 javaKafkaConsumer = new JavaKafkaConsumer(topic, threads, zookeeper, groupId,kafka_log);
					 new Thread(javaKafkaConsumer).start();

				 }
			 }
        } catch (Exception e) {
            logger.error("Kafka启动失败,具体原因如下：" + e.getMessage());
        }*/
	}
	@Override
	public void contextDestroyed(ServletContextEvent sce) {
		if(javaKafkaConsumer !=null){
			 // 关闭
			javaKafkaConsumer.shutdown();
		}

	}
}
