zookeeper唯一ID生成器

zookeeper唯一ID生成器

封装工具类代码

由于代码里有诸多的注释,一些理论什么的也不讲,一大推什么的,看的人头疼,我写也头疼,直接贴出代码。

public class IdMaker{
	private zkClient client = null;//客户端
	private final String server;//记录服务器地址
	private final String root;//顺序节点的父节点
	private final String nodeName;//顺序节点的名称
	private volatile boolean running = false;//运行状态
	
	private ExecutorService cleanExector = null;//线程池
	
	/**
	*规定删除方法:
	*不删除
	*立即删除
	*延迟删除
	*/
	public enum RemoveMethod{
		NONE,IMMEDIATELY,DALAY
	}
	
	/**
	*构造函数传入参数
	*/
	public IdMaker (String zkServer,String root,String nodeName){
		this.root = root;
		this.server = zkServer;
		this.nodeName = nodeName;
	}
	
	/**
	*启动服务器
	*/
	public void start() throws Exception{
		//启动之前检查服务器运行状态
		if(running){
			throw new Exception("服务器已经在运行");
		}
		running = true;
		init();
	}
	
	/**
	*停止服务器
	*/
	public void stop() throws Exception{
		//
		if(!running){
			throw new Exception("服务器已经停止");
		}
		running = false;
		//释放资源
		freeResource();
	}
	
	/**
	*初始化服务器
	*/
	private void init(){
		//实例化客户端对象,创建会话连接等			序列化器
		client = new zkClient(server,5000,5000,new BytesPushThroughSerializer());
		//实例化线程池
		cleanExector = Executor.newFixedThreadPool(15);
		try{
			//创建根节点
			client.createPersistent(root,true);
		}catch(zkNodeExistsException e){
			//忽略
		}
		
	}
	
	/**
	*释放资源
	*/
	private void freeResource(){
		//执行关机,释放线程池
		cleanExector.shutdown();
		try{
			cleanExector.awaitTermination(2,TimeUnit.SECONDS);
		}catch(InterruptedException e){
			e.printStackTrace();
		}finally{
			cleanExector = null;
		}
		
		//关闭客户端对象
		if(client!=null){
			client.close();
			client = null;
		}
	}
	
	/**
	*检查运行状态
	*/
	private void checkRunning() throws Exception{
		if(!running){
			throw new Exception("请先启用服务器");
		}
	}
	
	/**
	*提取顺序节点名称函数
	*/
	private String ExtractId(String str){
		//如果匹配,则为0
		int index = str.lastIndexOf(nodeName);
		if(index>=0){
			index+=nodeName.length();
			//通过三目运算符截取字符串
			return index<=str.length()?str.substrings(index):"";
		}
		return str;
	}
	
	/**
	*生成id
	*/
	public String generateId(RemoveMethod removeMethod)throws Exception{
		//先检查服务器运行状态,是否可用
		checkRunning();
		//构造顺序节点完整路径
		final String fullNodePath = root.concat("/").concat(nodeName);
		//创建带有顺序的持久节点,返回值为顺序节点完整路径
		final String ourPath = client.createPersistentSequential(fullNodePath,null);
		
		//删除节点,节约资源
		if(removeMethod.equals(removeMethod.IMMEDIATELY)){
			client.delete(ourPath);
		}else if(removeMethod.equals(removeMethod.DALAY)){
			cleanExector.execute(new Runnable(){
				public void run(){
					client.delete();
				}
			});
		}
		return ExtractId(ourPath);
	}
	
}

测试案例

public class TestIdMaker{
	public static void main(String[] args) throws Exception{
		IdMaker idMaker = new IdMaker("192.168.1.100","/NameService/IdGen","ID");
		idMaker.start();
		try{
			for(int i = 0;i<10;i++){
				String id = idMaker.generateId(RemoveMethod.DELAY);
				System.out.println(id);
			}
		}finally{
			idMaker.stop();
		}

	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值