use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project xian by happyyangyuan.
the class PathCacheExample method main.
public static void main(String[] args) throws Exception {
TestingServer server = new TestingServer();
CuratorFramework client = null;
PathChildrenCache cache = null;
try {
client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
client.start();
// in this example we will cache data. Notice that this is optional.
cache = new PathChildrenCache(client, PATH, true);
cache.start();
processCommands(client, cache);
} finally {
CloseableUtils.closeQuietly(cache);
CloseableUtils.closeQuietly(client);
CloseableUtils.closeQuietly(server);
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project x-pipe by ctripcorp.
the class AbstractClusterServers method doStart.
@Override
protected void doStart() throws Exception {
CuratorFramework client = zkClient.get();
serversCache = new PathChildrenCache(client, MetaZkConfig.getMetaServerRegisterPath(), true, XpipeThreadFactory.create(String.format("PathChildrenCache(%d)", currentServer.getServerId())));
serversCache.getListenable().addListener(new ChildrenChanged());
serversCache.start();
future = scheduled.scheduleWithFixedDelay(new AbstractExceptionLogTask() {
@Override
public void doRun() {
try {
childrenChanged();
} catch (Throwable th) {
logger.error("[doStart]", th);
}
}
}, 1000, metaServerConfig.getClusterServersRefreshMilli(), TimeUnit.MILLISECONDS);
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project turbo-rpc by hank-whu.
the class ZooKeeperDiscover method close.
@Override
public void close() throws IOException {
for (int i = 0; i < watchers.size(); i++) {
PathChildrenCache watcher = watchers.get(i);
try {
watcher.close();
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("watcher关闭失败 ", e);
}
}
}
watchers = null;
client.close();
client = null;
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project turbo-rpc by hank-whu.
the class ZooKeeperDiscover method addListener.
@Override
public void addListener(String group, String app, Protocol protocol, final DiscoverListener listener) {
Objects.requireNonNull(listener, "listener is null");
Objects.requireNonNull(client, "call init first");
final String path = "/turbo/" + group + "/" + app + "/" + protocol;
final PathChildrenCache watcher = new PathChildrenCache(client, path, true);
PathChildrenCacheListener pathChildrenCacheListener = new PathChildrenCacheListener() {
private final ConcurrentMap<HostPort, Integer> serverWithWeight = new ConcurrentHashMap<>();
private volatile boolean waitForInitializedEvent = true;
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
if (logger.isInfoEnabled()) {
logger.info("zk监控列表发生变化, " + path + ", " + event.getType());
}
boolean isChanged = true;
switch(event.getType()) {
case INITIALIZED:
waitForInitializedEvent = false;
if (logger.isInfoEnabled()) {
logger.info("完成初始化: " + path);
}
break;
case CHILD_ADDED:
{
AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
serverWithWeight.put(kv.address, kv.weight);
if (logger.isInfoEnabled()) {
logger.info("新增节点: " + kv);
}
break;
}
case CHILD_REMOVED:
{
AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
serverWithWeight.remove(kv.address);
if (logger.isInfoEnabled()) {
logger.info("删除节点: " + kv);
}
break;
}
case CHILD_UPDATED:
{
AddressWithWeight kv = new AddressWithWeight(event.getData().getData());
serverWithWeight.put(kv.address, kv.weight);
if (logger.isInfoEnabled()) {
logger.info("更新节点: " + kv);
}
break;
}
default:
isChanged = false;
if (logger.isInfoEnabled()) {
logger.info("忽略, " + path + ", " + event.getType());
}
}
if (!waitForInitializedEvent && isChanged) {
try {
listener.onChange(serverWithWeight);
} catch (Throwable t) {
if (logger.isWarnEnabled()) {
logger.warn("Discover监听处理失败", t);
}
}
}
}
};
watcher.getListenable().addListener(pathChildrenCacheListener);
try {
watcher.start(StartMode.POST_INITIALIZED_EVENT);
watchers.add(watcher);
} catch (Exception e) {
if (logger.isErrorEnabled()) {
logger.error("zk监听失败, " + path, e);
}
}
}
use of org.apache.curator.framework.recipes.cache.PathChildrenCache in project twister2 by DSC-SPIDAL.
the class ZKController method initialize.
/**
* connect to the server
* get a workerID for this worker
* create an ephemeral znode for this client
* @return
*/
public boolean initialize() {
String zkServerAddress = ZKContext.zooKeeperServerIP(config);
String zkServerPort = ZKContext.zooKeeperServerPort(config);
zkAddress = zkServerAddress + ":" + zkServerPort;
try {
client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
client.start();
dai = new DistributedAtomicInteger(client, daiPath, new ExponentialBackoffRetry(1000, 3));
// if the job node does not exists, it means that this is not rejoining
if (client.checkExists().forPath(jobPath) == null) {
int workerID = createWorkerID();
workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
createWorkerZnode();
appendWorkerInfo();
// if the parent exists, check whether this worker joined the job before
// whether it is coming from a failure
} else {
byte[] parentData = client.getData().forPath(jobPath);
String parentStr = new String(parentData);
if (parentStr.indexOf(hostAndPort) < 0) {
int workerID = createWorkerID();
workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
createWorkerZnode();
appendWorkerInfo();
// if this worker is coming from a failure, get the ID from the parent content
} else {
int workerID = getWorkerIDFromParentData(parentStr);
workerNetworkInfo = new WorkerNetworkInfo(hostAndPort, workerID);
createWorkerZnode();
}
}
// We childrenCache children data for parent path.
// So we will listen for all workers in the job
childrenCache = new PathChildrenCache(client, jobPath, true);
childrenCache.start();
LOG.log(Level.INFO, "This worker: " + workerNetworkInfo + " initialized successfully.");
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
Aggregations