Created
January 20, 2015 14:33
-
-
Save diablowu/cc2ac3e0a5c4a44f4a38 to your computer and use it in GitHub Desktop.
zookeeper datamonitor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package test; | |
import java.util.concurrent.CountDownLatch; | |
import java.util.concurrent.TimeUnit; | |
import org.apache.zookeeper.KeeperException; | |
import org.apache.zookeeper.WatchedEvent; | |
import org.apache.zookeeper.Watcher; | |
import org.apache.zookeeper.Watcher.Event.EventType; | |
import org.apache.zookeeper.ZooKeeper; | |
public class ZKTest { | |
private static final int SESSION_TIMEOUT = 10000; | |
private static final String CONNECTION_STRING = "192.168.1.220:2181,192.168.1.220:2182,192.168.1.220:2183,192.168.1.220:2184"; | |
private static final String ZK_PATH = "/wx/data/access_token"; | |
public static void main(String[] args) throws Exception { | |
final ZooKeeper zk = new ZooKeeper(CONNECTION_STRING, SESSION_TIMEOUT, | |
null); | |
while (true) { | |
CountDownLatch c = new CountDownLatch(1); | |
Watcher w = new MyWatcher(c,zk); | |
zk.getData(ZK_PATH, w, null); | |
TimeUnit.SECONDS.sleep(1); | |
System.out.println("*"); | |
c.await(); | |
} | |
} | |
} | |
class MyWatcher implements Watcher { | |
private CountDownLatch c; | |
private ZooKeeper zk; | |
public MyWatcher(CountDownLatch c, ZooKeeper zk) { | |
this.c = c; | |
this.zk = zk; | |
} | |
@Override | |
public void process(WatchedEvent event) { | |
System.out.println(event.getType()); | |
System.out.println(event.getPath()); | |
System.out.println("==================="); | |
c.countDown(); | |
if (EventType.NodeDataChanged.equals(event.getType())) { | |
try { | |
System.out.println(new String(zk.getData(event.getPath(), | |
false, null))); | |
} catch (KeeperException e) { | |
e.printStackTrace(); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment