Java Code Examples for org.apache.curator.framework.recipes.cache.TreeCache#start()

The following examples show how to use org.apache.curator.framework.recipes.cache.TreeCache#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CuratorZookeeperClient.java    From FATE-Serving with Apache License 2.0 6 votes vote down vote up
@Override
protected void addTargetDataListener(String path, CuratorZookeeperClient.CuratorWatcherImpl treeCacheListener, Executor executor) {
    try {
        TreeCache treeCache = TreeCache.newBuilder(client, path).setCacheData(false).build();
        treeCacheMap.putIfAbsent(path, treeCache);

        if (executor == null) {
            treeCache.getListenable().addListener(treeCacheListener);
        } else {
            treeCache.getListenable().addListener(treeCacheListener, executor);
        }

        treeCache.start();
    } catch (Exception e) {
        throw new IllegalStateException("Add treeCache listener for path:" + path, e);
    }
}
 
Example 2
Source File: ZookeeperRegistryCenter.java    From shardingsphere-elasticjob-cloud with Apache License 2.0 5 votes vote down vote up
@Override
public void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
    //CHECKSTYLE:OFF
    } catch (final Exception ex) {
    //CHECKSTYLE:ON
        RegExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
 
Example 3
Source File: TreeCacheExample.java    From curator with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception
{
    CuratorFramework client = CreateClientExamples.createSimple("127.0.0.1:2181");
    client.getUnhandledErrorListenable().addListener((message, e) -> {
        System.err.println("error=" + message);
        e.printStackTrace();
    });
    client.getConnectionStateListenable().addListener((c, newState) -> {
        System.out.println("state=" + newState);
    });
    client.start();

    TreeCache cache = TreeCache.newBuilder(client, "/").setCacheData(false).build();
    cache.getListenable().addListener((c, event) -> {
        if ( event.getData() != null )
        {
            System.out.println("type=" + event.getType() + " path=" + event.getData().getPath());
        }
        else
        {
            System.out.println("type=" + event.getType());
        }
    });
    cache.start();

    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    in.readLine();
}
 
Example 4
Source File: ZkClient.java    From xio with Apache License 2.0 5 votes vote down vote up
public void startTreeCache(TreeCache cache) {
  try {
    cache.start();
  } catch (Exception e) {
    log.error("Error starting treeCache {}", cache, e);
    throw new RuntimeException(e);
  }
}
 
Example 5
Source File: ZookeeperTreeCacheListener.java    From Thunder with Apache License 2.0 5 votes vote down vote up
public ZookeeperTreeCacheListener(CuratorFramework client, String path) throws Exception {
    super(client, path);

    treeCache = new TreeCache(client, path);
    treeCache.start();
    treeCache.getListenable().addListener(this);

    addListener();
}
 
Example 6
Source File: ZookeeperRegistryCenter.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
public void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
    //CHECKSTYLE:OFF
    } catch (final Exception ex) {
    //CHECKSTYLE:ON
        RegExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
 
Example 7
Source File: CuratorZookeeperCenterRepository.java    From shardingsphere with Apache License 2.0 5 votes vote down vote up
private void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
        // CHECKSTYLE:OFF
    } catch (final Exception ex) {
        // CHECKSTYLE:ON
        CuratorZookeeperExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
 
Example 8
Source File: ZookeeperRegistryCenter.java    From idworker with Apache License 2.0 5 votes vote down vote up
@Override
public void addCacheData(final String cachePath) {
    TreeCache cache = new TreeCache(client, cachePath);
    try {
        cache.start();
    } catch (final Exception ex) {
        RegExceptionHandler.handleException(ex);
    }
    caches.put(cachePath + "/", cache);
}
 
Example 9
Source File: ZookeeperRegistryCenter.java    From paascloud-master with Apache License 2.0 5 votes vote down vote up
/**
 * Add cache data.
 *
 * @param cachePath the cache path
 */
@Override
public void addCacheData(final String cachePath) {
	TreeCache cache = new TreeCache(client, cachePath);
	try {
		cache.start();
		//CHECKSTYLE:OFF
	} catch (final Exception ex) {
		//CHECKSTYLE:ON
		RegExceptionHandler.handleException(ex);
	}
	caches.put(cachePath + "/", cache);
}
 
Example 10
Source File: LogSearchConfigServerZK.java    From ambari-logsearch with Apache License 2.0 5 votes vote down vote up
@Override
public void init(Map<String, String> properties) throws Exception {
  super.init(properties);

  if (client.checkExists().forPath("/") == null) {
    client.create().creatingParentContainersIfNeeded().forPath("/");
  }
  serverCache = new TreeCache(client, "/");
  serverCache.start();
  LogLevelFilterManager logLevelFilterManager = new LogLevelFilterManagerZK(client, serverCache, LogSearchConfigZKHelper.getAcls(properties), gson);
  setLogLevelFilterManager(logLevelFilterManager);
}
 
Example 11
Source File: ZookeeperAppSubscriber.java    From sofa-dashboard-client with Apache License 2.0 5 votes vote down vote up
@Override
public void beforeStart(CuratorFramework client) {
    // Add listeners to manage local cache
    TreeCache cache = new TreeCache(client,
        ZookeeperConstants.SOFA_BOOT_CLIENT_INSTANCE);
    TreeCacheListener listener = (cli, event) -> {
        String dataPath = event.getData() == null ? null : event.getData().getPath();
        LOGGER
            .info("Dashboard client event type = {}, path= {}", event.getType(),
                dataPath);
        switch (event.getType()) {
            case NODE_ADDED:
            case NODE_UPDATED:
                runInSafe(() -> doCreateOrUpdateApplications(event));
                break;
            case NODE_REMOVED:
            case CONNECTION_LOST:
                runInSafe(() -> doRemoveApplications(event));
                break;
            case CONNECTION_RECONNECTED: // Try to recover data while reconnected
                doRebuildCache();
                break;
            default:
                break;
        }
    };
    cache.getListenable().addListener(listener);
    try {
        cache.start();
    } catch (Exception e) {
        LOGGER.error("Start cache error.", e);
    }
}
 
Example 12
Source File: ZkCuratorServer.java    From schedule-spring-boot-starter with Apache License 2.0 4 votes vote down vote up
public static void addTreeCacheListener(final ApplicationContext applicationContext, final CuratorFramework client, String path) throws Exception {
    TreeCache treeCache = new TreeCache(client, path);
    treeCache.start();
    treeCache.getListenable().addListener((curatorFramework, event) -> {
        if (null == event.getData()) return;
        byte[] eventData = event.getData().getData();
        if (null == eventData || eventData.length < 1) return;
        String json = new String(eventData, Constants.Global.CHARSET_NAME);
        if ("".equals(json) || json.indexOf("{") != 0 || json.lastIndexOf("}") + 1 != json.length()) return;
        Instruct instruct = JSON.parseObject(new String(event.getData().getData(), Constants.Global.CHARSET_NAME), Instruct.class);
        switch (event.getType()) {
            case NODE_ADDED:
            case NODE_UPDATED:
                if (Constants.Global.ip.equals(instruct.getIp()) && Constants.Global.schedulerServerId.equals(instruct.getSchedulerServerId())) {
                    //获取对象
                    CronTaskRegister cronTaskRegistrar = applicationContext.getBean("itstack-middlware-schedule-cronTaskRegister", CronTaskRegister.class);
                    boolean isExist = applicationContext.containsBean(instruct.getBeanName());
                    if (!isExist) return;
                    Object scheduleBean = applicationContext.getBean(instruct.getBeanName());
                    String path_root_server_ip_clazz_method_status = StrUtil.joinStr(path_root, Constants.Global.LINE, "server", Constants.Global.LINE, instruct.getSchedulerServerId(), Constants.Global.LINE, "ip", LINE, instruct.getIp(), LINE, "clazz", LINE, instruct.getBeanName(), LINE, "method", LINE, instruct.getMethodName(), "/status");
                    //执行命令
                    Integer status = instruct.getStatus();
                    switch (status) {
                        case 0:
                            cronTaskRegistrar.removeCronTask(instruct.getBeanName() + "_" + instruct.getMethodName());
                            setData(client, path_root_server_ip_clazz_method_status, "0");
                            logger.info("itstack middleware schedule task stop {} {}", instruct.getBeanName(), instruct.getMethodName());
                            break;
                        case 1:
                            cronTaskRegistrar.addCronTask(new SchedulingRunnable(scheduleBean, instruct.getBeanName(), instruct.getMethodName()), instruct.getCron());
                            setData(client, path_root_server_ip_clazz_method_status, "1");
                            logger.info("itstack middleware schedule task start {} {}", instruct.getBeanName(), instruct.getMethodName());
                            break;
                        case 2:
                            cronTaskRegistrar.removeCronTask(instruct.getBeanName() + "_" + instruct.getMethodName());
                            cronTaskRegistrar.addCronTask(new SchedulingRunnable(scheduleBean, instruct.getBeanName(), instruct.getMethodName()), instruct.getCron());
                            setData(client, path_root_server_ip_clazz_method_status, "1");
                            logger.info("itstack middleware schedule task refresh {} {}", instruct.getBeanName(), instruct.getMethodName());
                            break;
                    }
                }
                break;
            case NODE_REMOVED:
                break;
            default:
                break;
        }
    });
}
 
Example 13
Source File: MqBeanInitRunner.java    From paascloud-master with Apache License 2.0 4 votes vote down vote up
private void initConsumerListener(CuratorFramework cf) throws Exception {
	TreeCache treeCache = new TreeCache(cf, GlobalConstant.ZK_REGISTRY_CONSUMER_ROOT_PATH);
	treeCache.getListenable().addListener(consumerChangeListener);
	treeCache.start();
}
 
Example 14
Source File: ZKClient.java    From mpush with Apache License 2.0 4 votes vote down vote up
private void initLocalCache(String watchRootPath) throws Exception {
    cache = new TreeCache(client, watchRootPath);
    cache.start();
}
 
Example 15
Source File: MqBeanInitRunner.java    From paascloud-master with Apache License 2.0 4 votes vote down vote up
private void initProducerListener(CuratorFramework cf) throws Exception {
	TreeCache treeCache = new TreeCache(cf, GlobalConstant.ZK_REGISTRY_PRODUCER_ROOT_PATH);
	treeCache.getListenable().addListener(producerChangeListener);
	treeCache.start();
}
 
Example 16
Source File: TenantDebugger.java    From vespa with Apache License 2.0 4 votes vote down vote up
public TenantDebugger(Curator curator) throws Exception {
    cache = new TreeCache(curator.framework(), "/config/v2/tenants");
    cache.getListenable().addListener(this);
    cache.start();
}
 
Example 17
Source File: LogSearchConfigZKHelper.java    From ambari-logsearch with Apache License 2.0 2 votes vote down vote up
/**
 * Assign listener to cluster cache and start to use that listener
 * @param clusterCache zookeeper znode cache (cluster)
 * @param listener znode cache listener - trigger on events
 * @throws Exception error during assinging the listener to the cache
 */
public static void addAndStartListenersOnCluster(TreeCache clusterCache, TreeCacheListener listener) throws Exception {
  clusterCache.getListenable().addListener(listener);
  clusterCache.start();
}