use of org.apache.curator.utils.CloseableExecutorService in project xian by happyyangyuan.
the class ServiceCacheBuilderImpl method executorService.
/**
* Optional executor getGroup to use for the cache's background thread
*
* @param executorService executor getGroup
* @return this
*/
@Override
public ServiceCacheBuilder<T> executorService(ExecutorService executorService) {
this.executorService = new CloseableExecutorService(executorService);
this.threadFactory = null;
return this;
}
use of org.apache.curator.utils.CloseableExecutorService in project Saturn by vipshop.
the class ZkCacheManager method buildAndStartTreeCache.
private TreeCache buildAndStartTreeCache(String path, int depth) {
try {
String key = buildMapKey(path, depth);
TreeCache tc = treeCacheMap.get(key);
if (tc == null) {
tc = TreeCache.newBuilder(client, path).setMaxDepth(depth).setExecutor(new CloseableExecutorService(executorService, false)).build();
treeCacheMap.put(key, tc);
tc.start();
LogUtils.info(log, jobName, "{} - {} builds treeCache for path = {}, depth = {}", executorName, jobName, path, depth);
}
return tc;
} catch (Exception e) {
LogUtils.error(log, jobName, "{} - {} fails in building treeCache for path = {}, depth = {}, saturn will not work correctly.", executorName, jobName, path, depth);
LogUtils.error(log, jobName, e.getMessage(), e);
}
return null;
}
use of org.apache.curator.utils.CloseableExecutorService in project Saturn by vipshop.
the class ShardingTreeCacheService method addTreeCacheIfAbsent.
public void addTreeCacheIfAbsent(String path, int depth) throws Exception {
synchronized (isShutdownFlag) {
if (isShutdownFlag.get()) {
throw new ShardingException("ShardingTreeCacheService has been shutdown");
}
String fullPath = namespace + path;
if (!shardingTreeCache.containsTreeCache(path, depth)) {
TreeCache treeCache = TreeCache.newBuilder(curatorFramework, path).setExecutor(new CloseableExecutorService(executorService, false)).setMaxDepth(depth).build();
try {
treeCache.start();
} catch (Exception e) {
treeCache.close();
throw e;
}
TreeCache treeCacheOld = shardingTreeCache.putTreeCacheIfAbsent(path, depth, treeCache);
if (treeCacheOld != null) {
treeCache.close();
} else {
LOGGER.info("create TreeCache, full path is {}, depth is {}", fullPath, depth);
}
}
}
}
use of org.apache.curator.utils.CloseableExecutorService in project Saturn by vipshop.
the class SaturnExecutorService method registerCallbackAndStartExistingJob.
/**
* Register NewJobCallback, and async start existing jobs. The TreeCache will publish the create events for already
* existing jobs.
*/
public void registerCallbackAndStartExistingJob(final ScheduleNewJobCallback callback) throws Exception {
jobsTreeCache = TreeCache.newBuilder((CuratorFramework) coordinatorRegistryCenter.getRawClient(), JobNodePath.ROOT).setExecutor(new CloseableExecutorService(Executors.newSingleThreadExecutor(new SaturnThreadFactory(executorName + "-$Jobs-watcher", false)), true)).setMaxDepth(1).build();
jobsTreeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
if (event == null) {
return;
}
ChildData data = event.getData();
if (data == null) {
return;
}
String path = data.getPath();
if (path == null || path.equals(JobNodePath.ROOT)) {
return;
}
Type type = event.getType();
if (type == null || !type.equals(Type.NODE_ADDED)) {
return;
}
String jobName = StringUtils.substringAfterLast(path, "/");
String jobClassPath = JobNodePath.getNodeFullPath(jobName, ConfigurationNode.JOB_CLASS);
// wait 5 seconds at most until jobClass created.
for (int i = 0; i < WAIT_JOBCLASS_ADDED_COUNT; i++) {
if (client.checkExists().forPath(jobClassPath) == null) {
Thread.sleep(200);
continue;
}
log.info("new job: {} 's jobClass created event received", jobName);
if (!jobNames.contains(jobName)) {
if (callback.call(jobName)) {
jobNames.add(jobName);
log.info("the job {} initialize successfully", jobName);
} else {
log.warn("the job {} initialize fail", jobName);
}
} else {
log.warn("the job {} is unnecessary to initialize, because it's already existing", jobName);
}
break;
}
}
});
jobsTreeCache.start();
}
use of org.apache.curator.utils.CloseableExecutorService in project BRFS by zhangnianli.
the class ServiceCacheBuilderImpl method executorService.
/**
* Optional executor service to use for the cache's background thread
*
* @param executorService executor service
* @return this
*/
@Override
public ServiceCacheBuilder<T> executorService(ExecutorService executorService) {
this.executorService = new CloseableExecutorService(executorService);
this.threadFactory = null;
return this;
}
Aggregations