Example usage for java.util.concurrent ThreadFactory ThreadFactory

List of usage examples for java.util.concurrent ThreadFactory ThreadFactory

Introduction

In this page you can find the example usage for java.util.concurrent ThreadFactory ThreadFactory.

Prototype

ThreadFactory

Source Link

Usage

From source file:com.oneapm.base.SparkAggregation.java

private static void startZookeeperService(final FlowConstant flowConstant,
        final ZookeeperClient zookeeperClient) throws IOException {

    final Date startTime = new Date();
    ScheduledExecutorService service = Executors.newScheduledThreadPool(3, new ThreadFactory() {
        @Override/*ww w.  j  a  v a  2  s . co m*/
        public Thread newThread(Runnable r) {
            return new Thread(r, "Zookeeper-Writer-ThreadPool");
        }
    });

    final String zookeeper = Config.getConfig("alert.cnf").getProperty("kafaka.zoo", "127.0.0.1:2181");
    final String kafaka = Config.getConfig("alert.cnf").getProperty("kafaka.zoo", "127.0.0.1:9092");
    zookeeperClient.setLicense("/ni/license");
    zookeeperClient.createNode("/ni", "", true);
    zookeeperClient.createNode("/ni/process", "", true);
    zookeeperClient.createNode("/ni/process/Alarm", "", false);
    service.scheduleAtFixedRate(new Runnable() {
        @Override
        public void run() {
            try {
                //?
                checkComponent();
                writeData();
            } catch (Exception e) {
                LOG.warn("zookeeper?:" + e.getMessage());
            }
        }

        //?
        private void checkComponent() {
            Socket clientSocket = null;
            try {
                String[] zookeeperInfo = zookeeper.split(":");
                clientSocket = new Socket(zookeeperInfo[0], Integer.valueOf(zookeeperInfo[1]));

                flowConstant.isZookeeperOkFlag = true;
                flowConstant.isKafkaOkFlag = true;
            } catch (Exception e) {
                flowConstant.isZookeeperOkFlag = false;
                flowConstant.isKafkaOkFlag = false;
            } finally {
                if (clientSocket != null) {
                    try {
                        clientSocket.close();
                    } catch (IOException e) {
                        LOG.warn("socket:" + e.getMessage());
                    }
                }
            }
            //                //kafka?
            //                try {
            //                    String[] kafakaInfo = kafaka.split(":");
            //                    clientSocket = new Socket(kafakaInfo[0], Integer.valueOf(kafakaInfo[1]));
            //
            //                    flowConstant.isKafkaOkFlag = true;
            //                } catch (Exception e) {
            //                    flowConstant.isKafkaOkFlag = false;
            //                } finally {
            //                    try {
            //                        clientSocket.close();
            //                    } catch (IOException e) {
            //                        LOG.warn("socket:" + e.getMessage());
            //                    }
            //                }
        }

        private void writeData() {
            Map<String, String> processInfoMap = new HashMap<String, String>();

            try {
                processInfoMap.put("host", String.valueOf(InetAddress.getLocalHost()));
            } catch (UnknownHostException e) {
                e.printStackTrace();
            }
            processInfoMap.put("setupLocation", System.getProperty("user.dir"));
            processInfoMap.put("startupTime", String.valueOf(startTime.getTime()));
            String licence = "no license";
            if (Integer.parseInt(zookeeperClient.getLicense().split("\n")[2].split("=")[1]) == 1) {
                licence = "ok";
            }
            processInfoMap.put("licence", licence);
            if (flowConstant.isEsOKFlag && flowConstant.isKafkaOkFlag && flowConstant.isZookeeperOkFlag) {
                processInfoMap.put("status", "ok");
            } else {
                List<String> components = new ArrayList<String>();
                if (!flowConstant.isEsOKFlag) {
                    components.add("es is not available!");
                }
                if (!flowConstant.isZookeeperOkFlag) {
                    components.add("zookeeper is not available!");
                }
                if (!flowConstant.isKafkaOkFlag) {
                    components.add("kafka is not available!");
                }

                processInfoMap.put("status", StringUtils.join(components, ";"));
            }
            String processInfo = JSON.toJSONString(processInfoMap);

            if (zookeeperClient.isExists("/ni/process/Alarm")) {
                zookeeperClient.writeData("/ni/process/Alarm", processInfo);
            } else {
                zookeeperClient.createNode("/ni", "", true);
                zookeeperClient.createNode("/ni/process", "", true);
                zookeeperClient.createNode("/ni/process/Alarm", "", false);
                zookeeperClient.writeData("/ni/process/Alarm", processInfo);
            }
        }

    }, 10, 60, TimeUnit.SECONDS);
}