TestMain.java 3.29 KB
package com.yoho.kisjob.demo;

import java.nio.charset.Charset;
import java.util.List;
import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.CuratorFrameworkFactory.Builder;
import org.apache.curator.framework.api.ACLProvider;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.data.ACL;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.base.Strings;
import com.yoho.kisjob.common.locator.ServiceLocator;
import com.yoho.kisjob.reg.zk.ZkConfig;
import com.yoho.kisjob.reg.zk.ZkExceptionHandler;
import com.yoho.kisjob.reg.zk.ZkHelper;

public class TestMain {

	public static void main(String[] args) {
		remove();

		//start();
	}

	public static void start() {
		ApplicationContext app = new ClassPathXmlApplicationContext("classpath:META-INF/spring/spring_registry.xml",
				"classpath:META-INF/spring/spring_*.xml");
		ServiceLocator.initApplicationContext(app);
	}

	private static void remove() {
		ZkConfig config = new ZkConfig();
		config.setNamespace("YohoJobs");
		config.setServerLists("localhost:2181");
		CuratorFramework client = initZkClient(config);
		removePath(client, "/JobNodeGroups");
		client.close();
	}

	private static void removePath(CuratorFramework client, String path) {
		ZkHelper zkHelper = new ZkHelper(client);
		if (!zkHelper.isExisted(path)) {
			return;
		}

		List<String> children = zkHelper.getChildrenKeys(path);
		if (children != null && !children.isEmpty()) {
			for (String node : children) {
				removePath(client, path + "/" + node);
			}
		}

		zkHelper.remove(path);
	}

	private static CuratorFramework initZkClient(ZkConfig zkConfig) {
		Builder builder = CuratorFrameworkFactory
				.builder()
				.connectString(zkConfig.getServerLists())
				.retryPolicy(
						new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(),
								zkConfig.getMaxSleepTimeMilliseconds())).namespace(zkConfig.getNamespace());
		if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
			builder.sessionTimeoutMs(zkConfig.getSessionTimeoutMilliseconds());
		}
		if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
			builder.connectionTimeoutMs(zkConfig.getConnectionTimeoutMilliseconds());
		}
		if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
			builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName("UTF-8"))).aclProvider(
					new ACLProvider() {

						@Override
						public List<ACL> getDefaultAcl() {
							return ZooDefs.Ids.CREATOR_ALL_ACL;
						}

						@Override
						public List<ACL> getAclForPath(final String path) {
							return ZooDefs.Ids.CREATOR_ALL_ACL;
						}
					});
		}
		CuratorFramework client = builder.build();
		client.start();
		try {
			client.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(),
					TimeUnit.MILLISECONDS);
			if (!client.getZookeeperClient().isConnected()) {
				throw new KeeperException.OperationTimeoutException();
			}
		} catch (Exception ex) {
			ZkExceptionHandler.handleException(ex);
		}

		return client;
	}
}