/** * Acquire the leadership * @param leaderPath the leader node path * @param listener the leader listener * @return the leadership */ public Leader acquireLeader(String leaderPath, LeaderListener listener){ return acquireLeader(null, leaderPath, listener); }
@Override public void doStart(){ leader = zk.client().acquireLeader(host.get(), ZkPaths.LEADER, new LeaderListener() { @Override public void isLeader() { Logs.info("The server {} become the leader.", host.get()); // start the client failover clientFailover.start(); // start the zk cleaner zkCleaner.start(); // set the lead's host zk.client().update(ZkPaths.LEADER, host.get()); // start the leader check task // avoid that the leader disconnect, because of network jitter scheduler = Executors.newScheduledThreadPool(1); scheduler.scheduleAtFixedRate(new LeaderCheckTask(), 1, 5, TimeUnit.SECONDS); } }); // start the server failover with each server // avoid that the leader crashed and can't failover its jobs serverFailover.start(); }