zookeeper LeaderElection(leader 选举)

package zookeeper;  
  
import org.apache.zookeeper.*;  
import org.apache.zookeeper.data.Stat;  
  
import java.net.InetAddress;  
import java.net.UnknownHostException;  
import java.util.List;  
import java.util.concurrent.CountDownLatch;  
  
/** 
 * Modify by bamboo.hey
 * User: EDENYIN 
 * Date: 1/4/11 
 * Time: 3:13 PM 
 * To change this template use File | Settings | File Templates. 
 */  
public class LeaderElection implements Watcher, Runnable {  
  
  
    private String zookeeperConnectionString;  
    private String rootPath;  
  
    private ZooKeeper zk;  
  
    private byte[] hostAddress;  
  
  
    public LeaderElection(String zookeeperConnectionString, String rootPath) {  
        this.zookeeperConnectionString = zookeeperConnectionString;  
        this.rootPath = rootPath;  
        try {  
            hostAddress = InetAddress.getLocalHost().getHostAddress().getBytes();  
        } catch (UnknownHostException e) {  
            e.printStackTrace();  
        }  
        buildZK();  
    }  
  
  
    private void buildZK() {  
        System.out.println("Build zk client");  
        try {  
            zk = new ZooKeeper(zookeeperConnectionString, 10000, this);  
            Stat s = zk.exists(rootPath, false);  
            if (s == null) {  
                zk.create(rootPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);  
                zk.create(rootPath + "/ELECTION", new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);  
            }  
            String value = zk.create(rootPath + "/ELECTION/n_", hostAddress, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);  
        } catch (Exception e) {  
            e.printStackTrace();  
            System.err.println("Error connect to zoo keeper");  
        }  
    }  
  
  
    public void process(WatchedEvent event) {  
        System.out.println(event);  
        if (event.getState() == Event.KeeperState.Disconnected || event.getState() == Event.KeeperState.Expired) {  
            System.out.println("Zookeeper connection timeout.");  
            buildZK();  
        }  
  
    }  
  
    public void run() {  
            while (true) {  
                try {  
                    List<String> children = zk.getChildren(rootPath + "/ELECTION", false);  
                    String leaderPath = "Not found";  
                    int minValue = -1;  
                    for (int i=0;i<children.size();i++) {  
                        String child = children.get(i);  
                        int index = Integer.parseInt(child.substring(2));  
                        if (i == 0) {  
                            minValue = index;  
                            leaderPath = child;  
                        }else if (index < minValue) {  
                            minValue = index;  
                            leaderPath = child;  
                        }  
                    }  
                    LatchChildWatcher latchChildWatcher = new LatchChildWatcher();  
                    byte[] data = zk.getData(rootPath + "/ELECTION/" + leaderPath, latchChildWatcher, null);  
                    System.out.println("find the leader on the path:" + leaderPath + " whose host address is " + new String(data));  
                    latchChildWatcher.await();  
                    System.out.println("aaaaaaaaaaaaaaaa");
                } catch (Exception e) {  
                    e.printStackTrace();  
                    System.err.println("Error get the leader." + e.getMessage());  
                }  
        }  
    }  
  
    private class LatchChildWatcher implements Watcher {  
  
        CountDownLatch latch;  
  
        public LatchChildWatcher(){  
            latch = new CountDownLatch(1);  
        }  
  
        public void process(WatchedEvent event){  
            System.out.println("Watcher fired on path: " + event.getPath() + " state: " +  
                    event.getState() + " type " + event.getType());  
            latch.countDown();  
        }  
        public void await() throws InterruptedException {  
            latch.await();  
        }  
    }  
    public static void main(String[] args) 
    {
    String zookeeperConnectionString="inc-dw-hadoop-151-7:2181,inc-dw-hadoop-151-8:2181,inc-dw-hadoop-151-9:2181";
        String rootPath="/le";  
    LeaderElection le=new LeaderElection(zookeeperConnectionString,rootPath);
    le.run();
    }
}  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值