zookeeper curator获取唯一序列

使用 curator获取唯一序列

package com.cn.zookeeper.t;

import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.locks.InterProcessMutex;
import org.apache.curator.utils.CloseableUtils;

public class SeqTask{
	
   public static String getSequence(){
	   CuratorFramework client = new ZookeeperClient().getZookeeperInstance().build();
	   client.start();
       InterProcessMutex lock = new InterProcessMutex(client, CommParam.LOCK_ZNODE);
       try {
           boolean retry=true;
           byte[] newData = null;
           do{
               if (lock.acquire(1000, TimeUnit.MILLISECONDS)) {
                   byte[] oldData = client.getData().forPath(CommParam.LOCK_ZNODE);
                   String s = new String(oldData);
                   if("".equals(s)){
                   	s=CommParam.INIT_VAL;
                   }
                   int d = Integer.parseInt(s);
                   d = d + 1;
                   s = String.valueOf(d);
                   newData = s.getBytes();
                   client.setData().forPath(CommParam.LOCK_ZNODE, newData);
                   retry=false;
               }
           }while(retry);
           
           return new String(newData);
           
       } catch (Exception e) {
           e.printStackTrace();
           
           return null;
       } finally {
           try {
               if (lock.isAcquiredInThisProcess()) {
                   lock.release();
               }
           } catch (Exception e) {
               e.printStackTrace();
           } finally {
               CloseableUtils.closeQuietly(client);
           }
       }
   }
   
   public static void main(String[] args) {
	System.out.println(getSequence());
}
}
package com.cn.zookeeper.t;

public class CommParam {
	public static final String LOCK_ZNODE = "/zook_lock";
	public static final String CONNECT_URL="127.0.0.1:2181";
	public static final String INIT_VAL="10000";
}

package com.cn.zookeeper.t;

import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class ZookeeperClient {
	public static CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory
			.builder().connectionTimeoutMs(1000)
			.connectString(CommParam.CONNECT_URL).defaultData("0".getBytes())
			// .namespace("curator")
			.retryPolicy(new ExponentialBackoffRetry(3000, 3))
			.maxCloseWaitMs(5000).threadFactory(new ThreadFactory() {
				public final AtomicInteger counter = new AtomicInteger(0);

				public Thread newThread(Runnable r) {
					Thread thread = new Thread(r, "curator-"
							+ counter.getAndIncrement());
					thread.setDaemon(true);
					return thread;
				}
			});;

	ZookeeperClient() {
	}

	public CuratorFrameworkFactory.Builder getZookeeperInstance() {
		return builder;
	}
}



你可能感兴趣的:(zookeeper curator获取唯一序列)