當前位置: 首頁>>代碼示例>>Java>>正文


Java NodeCache類代碼示例

本文整理匯總了Java中org.apache.curator.framework.recipes.cache.NodeCache的典型用法代碼示例。如果您正苦於以下問題:Java NodeCache類的具體用法?Java NodeCache怎麽用?Java NodeCache使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


NodeCache類屬於org.apache.curator.framework.recipes.cache包,在下文中一共展示了NodeCache類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: startup

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
@Override
public void startup()
{
	if( isCluster() )
	{
		CuratorFramework curator = getCurator();
		debugCache = new NodeCache(curator, CLUSTER_DEBUG_FULL_PATH);
		try
		{
			debugCache.start();
			createNode(SERVER_REL_PATH, "");
			membersCache = createPathCache(SERVER_REL_PATH, false);
			ensureDebug = curator.newNamespaceAwareEnsurePath(CLUSTER_DEBUG_FULL_PATH);
		}
		catch( Exception e )
		{
			Throwables.propagate(e);
		}
	}
	hasStarted = true;
}
 
開發者ID:equella,項目名稱:Equella,代碼行數:22,代碼來源:ZookeeperServiceImpl.java

示例2: runWatch

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
/**
 * @param cache    NodeCache
 * @param zkListen
 * @throws Exception
 * @Created 2016/9/20
 */
private static void runWatch(final NodeCache cache, final ZookeeperProcessListen zkListen)
        throws Exception {
    cache.getListenable().addListener(new NodeCacheListener() {

        @Override
        public void nodeChanged() {
            LOGGER.info("ZktoxmlMain runWatch  process path  event start ");
            String notPath = cache.getCurrentData().getPath();
            LOGGER.info("NodeCache changed, path is: " + notPath);
            // notify
            zkListen.notify(notPath);
            LOGGER.info("ZktoxmlMain runWatch  process path  event over");
        }
    });
}
 
開發者ID:actiontech,項目名稱:dble,代碼行數:22,代碼來源:ZktoXmlMain.java

示例3: listenNodes

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
private List<NodeCache> listenNodes(String profileCode, String[] appCodes) {
    ZkTemplate.NodeListener listener = new ZkTemplate.NodeListener() {
        @Override
        public void nodeChanged() throws Exception {
            try {
                refresher.refresh();
            } catch (Throwable e) {
                logger.error("觸發刷新出錯:", e);
            }
        }
    };
    List<NodeCache> nodeCaches = new ArrayList<>();
    for (String appCode : appCodes) {
        NodeCache nodeCache = zkTemplate.listenNode(ZkTemplate.buildPath(profileCode, appCode), false, listener);
        nodeCaches.add(nodeCache);
    }
    return nodeCaches;
}
 
開發者ID:zhongxunking,項目名稱:configcenter,代碼行數:19,代碼來源:RefreshTrigger.java

示例4: test_zkNode

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
@Test
public void test_zkNode() throws Exception {
    CuratorFramework zk = curator.usingNamespace("namespace-test");
    String groupPath = "/group-1/localhost:9001";
    String s = zk.create().creatingParentsIfNeeded().forPath(groupPath);
    Assert.assertEquals(s, "/group-1/localhost:9001");
    NodeCache nodeCache = new NodeCache(zk, "/group-1", true);
    nodeCache.start();
    nodeCache.getListenable().addListener(new NodeCacheListener() {

        @Override
        public void nodeChanged() throws Exception {
            logger.info("node cache change");
        }
    });
    zk.setData().forPath("/group-1", "test-0".getBytes());
    zk.setData().forPath("/group-1", "test-2".getBytes());
}
 
開發者ID:netboynb,項目名稱:coco,代碼行數:19,代碼來源:CuratorTest.java

示例5: initProvider

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
private void initProvider(CuratorFramework client, String serviceName) throws Exception {
    this.client = client;
    this.serviceName = serviceName;
    tmpFile = confFile(true);
    confFile = confFile(false);
    String configPath = ZookeeperUtils.configPath(serviceName);

    byte[] bytes = client.getData().forPath(configPath);
    saveFile(bytes);
    cache = new NodeCache(client, configPath);
    cache.getListenable().addListener(() -> {
        try {
            if (cache.getCurrentData() != null) {
                byte[] data = cache.getCurrentData().getData();
                if (data.length > 0) {
                    saveFile(data);
                }
            }
        } catch (Exception e) {
            logger.warn("Error while processing config file change event. message={}", e.getMessage());
        }
    });
    cache.start();
}
 
開發者ID:yahoo,項目名稱:gondola,代碼行數:25,代碼來源:ZookeeperConfigProvider.java

示例6: addNodeCacheListener

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
public void addNodeCacheListener(String path) throws Exception {
    Stat existStat = curatorFramework
            .checkExists()
            .forPath(path);
    if (existStat == null)
        curatorFramework
                .create()
                .creatingParentsIfNeeded()
                .withMode(CreateMode.PERSISTENT)
                .forPath(path);
    NodeCache nodeCache = new NodeCache(curatorFramework, path, false);
    nodeCache.start();
    nodeCache.getListenable().addListener(() -> {
                ChildData currentData = nodeCache.getCurrentData();
                LOG.info("New Cache Data: {}", currentData == null ? "null" : new String(currentData.getData()));
            }
    );
}
 
開發者ID:asdf2014,項目名稱:yuzhouwan,代碼行數:19,代碼來源:CuratorNodeCache.java

示例7: main

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
public static void main(String[] args) throws Exception {
    CuratorFramework framework = CuratorFrameworkFactory.builder()
            .connectString("localhost:2181").retryPolicy(new RetryNTimes(3, 2000)).build();
    try {
        framework.start();
        final NodeCache nodeCache = new NodeCache(framework, "/test");
        nodeCache.getListenable().addListener(new NodeCacheListener() {
            @Override
            public void nodeChanged() throws Exception {
                ChildData data = nodeCache.getCurrentData();
                System.out.println(new String(data.getData()) + " / " + data);
            }
        });
        nodeCache.start();

        Thread.sleep(30000);

        nodeCache.close();
    } finally {
        framework.close();
    }
}
 
開發者ID:DDTH,項目名稱:ddth-zookeeper,代碼行數:23,代碼來源:TestCuratorNodeWatch.java

示例8: start

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
/**
 * Start.
 *
 * @throws Exception the exception
 */
public void start() throws Exception { //NOSONAR
  LOG.info("Starting node tracker");
  zkClient.getUnhandledErrorListenable().addListener(errorsListener);
  if (createZkNode()) {
    controlCache = new NodeCache(zkClient, CONTROL_SERVER_NODE_PATH);
    controlCache.getListenable().addListener(new NodeCacheListener() {

      @Override
      public void nodeChanged() throws Exception {
        ChildData currentData = controlCache.getCurrentData();
        if (currentData == null) {
          LOG.warn("Control service node died!");
          onNoMaster();
        } else {
          LOG.warn("Control service node changed!");
          onMasterChange(currentData);
        }
      }
    });
    controlCache.start();
  } else {
    LOG.warn("Failed to create ZK node!");
  }
}
 
開發者ID:kaaproject,項目名稱:kaa,代碼行數:30,代碼來源:ControlNodeTracker.java

示例9: registerListener

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
private void registerListener(final CuratorFramework curator, final String path, final ZookeeperConfigurationListener listener) throws Exception
{
    final NodeCache cache   = new NodeCache(curator, path, true);
    
    cache.start();
    cache.getListenable().addListener(new NodeCacheListener()
    {
        @Override
        public void nodeChanged() throws Exception
        {
            listener.onChange(curator, cache, path);
        }
    });

    childrens.add(cache);
    logger.info(String.format("Add listener %s for %s", listener, path));
}
 
開發者ID:interruptus,項目名稱:interruptus,代碼行數:18,代碼來源:AttachConfigurationListener.java

示例10: ZkNodeCacheWrapper

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
public ZkNodeCacheWrapper(IDataSourceConnector connector, String path, NodeCache cache, boolean dataIsCompressed, boolean useCache, boolean useCacheWhenNotConnectedToDataSource) {
    super(connector);
    this.useCache = useCache;
    this.dataIsCompressed = dataIsCompressed;
    this.useCacheWhenNotConnectedToDataSource = useCacheWhenNotConnectedToDataSource;
    this.path = path;
    this.cache = cache;
}
 
開發者ID:Comcast,項目名稱:redirector,代碼行數:9,代碼來源:ZkNodeCacheWrapper.java

示例11: loadZkWatch

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
private static void loadZkWatch(Set<String> setPaths, final CuratorFramework zkConn,
                                final ZookeeperProcessListen zkListen) throws Exception {
    if (null != setPaths && !setPaths.isEmpty()) {
        for (String path : setPaths) {
            final NodeCache node = new NodeCache(zkConn, path);
            node.start(true);
            runWatch(node, zkListen);
            LOGGER.info("ZktoxmlMain loadZkWatch path:" + path + " regist success");
        }
    }
}
 
開發者ID:actiontech,項目名稱:dble,代碼行數:12,代碼來源:ZktoXmlMain.java

示例12: close

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
/**
 * 關閉(釋放相關資源)
 */
public void close() {
    for (NodeCache nodeCache : nodeCaches) {
        try {
            nodeCache.close();
        } catch (IOException e) {
            logger.error("關閉節點監聽器出錯:", e);
        }
    }
    zkTemplate.close();
}
 
開發者ID:zhongxunking,項目名稱:configcenter,代碼行數:14,代碼來源:RefreshTrigger.java

示例13: buildNodeCache

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
private Observable<NodeCache> buildNodeCache(final ConfigDescriptor desc)
{
    return Observable.fromCallable(() -> {
        final String configPath = makePath(ROOT_ZK_PATH, desc.getConfigName());
        final NodeCache nc = new NodeCache(curator, configPath);
        nc.getListenable().addListener(() -> onNodeChanged(nc, desc));
        try {
            nc.start(true);

            // Note that we have to force calling onNodeChanged() here since `nc.start(true)` will not emit an initial event.
            onNodeChanged(nc, desc);

            // Create the ephemeral node last, just in case something goes wrong with setting up the node cache
            // NOTE: This process is what actually creates the configuration node if it was missing.
            PersistentEphemeralNode en = new PersistentEphemeralNode(curator, EPHEMERAL, makePath(configPath, localNodeName), new byte[0]);
            en.start();
            if (!en.waitForInitialCreate(getDefaultNodeCreationTimeout().toMillis(), TimeUnit.MILLISECONDS)) {
                throw new TimeoutException("Timeout on creation of ephemeral node for " + makePath(configPath, localNodeName));
            }
            ephemeralNodes.put(desc, en);

            return nc;
        }
        catch (Exception ex) {
            log.warn("Failed to initialize for configPath {}", configPath, ex);
            throw ex;
        }
    });
}
 
開發者ID:kikinteractive,項目名稱:ice,代碼行數:30,代碼來源:ZooKeeperDynamicConfigSource.java

示例14: onNodeChanged

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
public void onNodeChanged(final NodeCache cache, final ConfigDescriptor desc)
{
    ChildData childData = cache.getCurrentData();
    try {
        Optional<String> valueOpt = Optional.empty();
        if (childData != null && childData.getData() != null && childData.getData().length > 0) {
            valueOpt = Optional.of(new String(childData.getData(), Charsets.UTF_8));
        }
        emitEvent(desc.getConfigName(), valueOpt);
    }
    catch (Exception ex) {
        log.warn("Failed to handle onNodeChanged w/ new data for config key {}, data {}", desc.getConfigName(), childData, ex);
    }
}
 
開發者ID:kikinteractive,項目名稱:ice,代碼行數:15,代碼來源:ZooKeeperDynamicConfigSource.java

示例15: serviceStart

import org.apache.curator.framework.recipes.cache.NodeCache; //導入依賴的package包/類
@Override
protected void serviceStart() throws Exception {
    framework.start();
    nodeCache = new NodeCache(framework, zkMountTablePath, false);
    nodeCache.getListenable().addListener(new NodeCacheListener() {
        @Override
        public void nodeChanged() throws Exception {
            handleMountTableChange(nodeCache.getCurrentData().getData());
        }
    });
    nodeCache.start(false);
}
 
開發者ID:bytedance,項目名稱:nnproxy,代碼行數:13,代碼來源:MountsManager.java


注:本文中的org.apache.curator.framework.recipes.cache.NodeCache類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。