cloud.thrift.client.config.ZooKeeperConfig.java Source code

Java tutorial

Introduction

Here is the source code for cloud.thrift.client.config.ZooKeeperConfig.java

Source

/**
 * @(#)ZooKeeperConfig.java, ?? 02, 2016.
 * <p>
 * Copyright 2016 fenbi.com. All rights reserved.
 * FENBI.COM PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */
package cloud.thrift.client.config;

import cloud.thrift.UserService;
import org.I0Itec.zkclient.IZkChildListener;
import org.apache.helix.manager.zk.ZkClient;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransportException;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author zhangpeng
 */

@Configuration
public class ZooKeeperConfig {

    @Value("${service.name}")
    String serviceName;

    @Value("${zookeeper.server.list}")
    String zookeeperList;

    ExecutorService executor = Executors.newSingleThreadExecutor();

    // thrift
    public static Map<String, UserService.Client> serviceMap = new HashMap<String, UserService.Client>();

    @PostConstruct
    private void init() {
        executor.execute(new Runnable() {
            @Override
            public void run() {
                startZooKeeper();
                try {
                    Thread.sleep(1000 * 60 * 60 * 24 * 360 * 10);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    // ?
    private void startZooKeeper() {
        List<String> currChilds = new ArrayList<String>();
        String servicePath = "/" + serviceName;// 
        ZkClient zkClient = new ZkClient(zookeeperList);
        boolean serviceExists = zkClient.exists(servicePath);
        if (serviceExists) {
            currChilds = zkClient.getChildren(servicePath);
        } else {
            throw new RuntimeException("service not exist!");
        }

        for (String instanceName : currChilds) {
            // ??
            if (!serviceMap.containsKey(instanceName)) {
                serviceMap.put(instanceName, createUserService(instanceName));
            }
        }
        // ?
        zkClient.subscribeChildChanges(servicePath, new IZkChildListener() {
            // @Override
            public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
                // (path):???
                for (String instanceName : currentChilds) {
                    // ??
                    if (!serviceMap.containsKey(instanceName)) {
                        serviceMap.put(instanceName, createUserService(instanceName));
                    }
                }
                for (Map.Entry<String, UserService.Client> entry : serviceMap.entrySet()) {
                    // ?
                    if (!currentChilds.contains(entry.getKey())) {
                        UserService.Client c = serviceMap.get(entry.getKey());
                        try {
                            c.getInputProtocol().getTransport().close();
                            c.getOutputProtocol().getTransport().close();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        serviceMap.remove(entry.getKey());
                    }
                }
                System.out.println(parentPath + "?");
            }
        });
    }

    // ?
    private UserService.Client createUserService(String serviceInstanceName) {
        String ip = serviceInstanceName.split("-")[1];
        TSocket transport = new TSocket(ip, 7911);
        try {
            transport.open();
        } catch (TTransportException e) {
            e.printStackTrace();
        }
        return new UserService.Client(new TBinaryProtocol(transport));
    }
}