Search in sources :

Example 1 with CloseableExecutorService

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;
}
Also used : CloseableExecutorService(org.apache.curator.utils.CloseableExecutorService)

Example 2 with CloseableExecutorService

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;
}
Also used : TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) CloseableExecutorService(org.apache.curator.utils.CloseableExecutorService)

Example 3 with CloseableExecutorService

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);
            }
        }
    }
}
Also used : ShardingException(com.vip.saturn.job.sharding.exception.ShardingException) ShardingTreeCache(com.vip.saturn.job.sharding.entity.ShardingTreeCache) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) CloseableExecutorService(org.apache.curator.utils.CloseableExecutorService) ShardingException(com.vip.saturn.job.sharding.exception.ShardingException)

Example 4 with CloseableExecutorService

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();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Type(org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) CloseableExecutorService(org.apache.curator.utils.CloseableExecutorService) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) SaturnThreadFactory(com.vip.saturn.job.threads.SaturnThreadFactory) TimeDiffIntolerableException(com.vip.saturn.job.exception.TimeDiffIntolerableException)

Example 5 with CloseableExecutorService

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;
}
Also used : CloseableExecutorService(org.apache.curator.utils.CloseableExecutorService)

Aggregations

CloseableExecutorService (org.apache.curator.utils.CloseableExecutorService)5 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)2 TimeDiffIntolerableException (com.vip.saturn.job.exception.TimeDiffIntolerableException)1 ShardingTreeCache (com.vip.saturn.job.sharding.entity.ShardingTreeCache)1 ShardingException (com.vip.saturn.job.sharding.exception.ShardingException)1 SaturnThreadFactory (com.vip.saturn.job.threads.SaturnThreadFactory)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 ChildData (org.apache.curator.framework.recipes.cache.ChildData)1 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)1 Type (org.apache.curator.framework.recipes.cache.TreeCacheEvent.Type)1 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)1