use of org.apache.zookeeper.Watcher in project disgear by yangbutao.
the class ClusterStateCacheManager method createClusterStateWatcher.
/**
* monitor to clusterstate.json
*/
public void createClusterStateWatcher() throws Exception {
byte[] datas = zkClient.getData(CLUSTER_STATE, new Watcher() {
public void process(WatchedEvent event) {
if (EventType.None.equals(event.getType())) {
return;
}
try {
synchronized (this) {
final Watcher thisWatch = this;
Stat stat = new Stat();
byte[] data = zkClient.getData(CLUSTER_STATE, thisWatch, stat);
List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this);
Set<String> liveNodesSet = new HashSet<String>();
liveNodesSet.addAll(liveNodes);
Set<String> ln = clusterState.getLiveNodes();
ClusterState newClusterState = ClusterState.load(stat.getVersion(), data, liveNodesSet);
clusterState = newClusterState;
CacheOperation.INSTANCE.clearJRedisPool();
System.out.println("CLUSTER_STATE changed");
System.out.println(clusterState.toString());
}
} catch (KeeperException e) {
if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
return;
}
// log.error("", e);
throw new RuntimeException(e);
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
return;
}
}
}, null);
Stat stat = new Stat();
List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, null);
Set<String> liveNodesSet = new HashSet<String>();
liveNodesSet.addAll(liveNodes);
clusterState = ClusterState.load(stat.getVersion(), datas, liveNodesSet);
}
use of org.apache.zookeeper.Watcher in project yyl_example by Relucent.
the class Test2 method main.
public static void main(String[] args) {
ZooKeeper zk = null;
try {
zk = new ZooKeeper("localhost:2181", 1000, new Watcher() {
// 监控所有被触发的事件
public void process(WatchedEvent event) {
System.out.println("已经触发了" + event.getType() + "事件!");
}
});
printChildren("/", zk);
} catch (Exception e) {
e.printStackTrace();
try {
if (zk != null) {
zk.close();
}
} catch (Exception e1) {
}
}
}
use of org.apache.zookeeper.Watcher in project lucene-solr by apache.
the class ZkIndexSchemaReader method createSchemaWatcher.
public void createSchemaWatcher() {
log.info("Creating ZooKeeper watch for the managed schema at " + managedSchemaPath);
try {
zkClient.exists(managedSchemaPath, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (ZkIndexSchemaReader.this.isRemoved) {
// the core for this reader has already been removed, don't process this event
return;
}
// session events are not change events, and do not remove the watcher
if (Event.EventType.None.equals(event.getType())) {
return;
}
log.info("A schema change: {}, has occurred - updating schema from ZooKeeper ...", event);
try {
updateSchema(this, -1);
} catch (KeeperException e) {
if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) {
log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK");
return;
}
log.error("", e);
throw new ZooKeeperException(ErrorCode.SERVER_ERROR, "", e);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
log.warn("", e);
}
}
}, true);
} catch (KeeperException e) {
final String msg = "Error creating ZooKeeper watch for the managed schema";
log.error(msg, e);
throw new ZooKeeperException(ErrorCode.SERVER_ERROR, msg, e);
} catch (InterruptedException e) {
// Restore the interrupted status
Thread.currentThread().interrupt();
log.warn("", e);
}
}
use of org.apache.zookeeper.Watcher in project lucene-solr by apache.
the class ZkSolrClientTest method testMultipleWatchesAsync.
public void testMultipleWatchesAsync() throws Exception {
try (ZkConnection conn = new ZkConnection()) {
final SolrZkClient zkClient = conn.getClient();
zkClient.makePath("/collections", true);
final int numColls = random().nextInt(100);
final CountDownLatch latch = new CountDownLatch(numColls);
for (int i = 1; i <= numColls; i++) {
String collPath = "/collections/collection" + i;
zkClient.makePath(collPath, true);
zkClient.getChildren(collPath, new Watcher() {
@Override
public void process(WatchedEvent event) {
latch.countDown();
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
}
}
}, true);
}
for (int i = 1; i <= numColls; i++) {
String shardsPath = "/collections/collection" + i + "/shards";
zkClient.makePath(shardsPath, true);
}
assertTrue(latch.await(1000, TimeUnit.MILLISECONDS));
}
}
use of org.apache.zookeeper.Watcher in project XRTB by benmfaul.
the class ZTester method connect.
// host should be 127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002
public ZooKeeper connect(String host) throws Exception {
zk = new ZooKeeper(host, 2101, new Watcher() {
public void process(WatchedEvent event) {
if (event.getState() == KeeperState.SyncConnected) {
connSignal.countDown();
}
}
});
connSignal.await();
return zk;
}
Aggregations