Java Code Examples for org.apache.curator.framework.recipes.cache.TreeCacheEvent#getData()

The following examples show how to use org.apache.curator.framework.recipes.cache.TreeCacheEvent#getData() . 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: DubboServiceDiscoveryAutoConfiguration.java    From spring-cloud-alibaba with Apache License 2.0 6 votes vote down vote up
/**
 * Resolve the name of service.
 * @param event {@link TreeCacheEvent}
 * @return If the Zookeeper's {@link ChildData#getPath() node path} that was
 * notified comes from {@link ServiceInstance the service instance}, return it's
 * parent path as the service name, or return <code>null</code>
 */
private String resolveServiceName(TreeCacheEvent event) {
	ChildData childData = event.getData();
	String path = childData.getPath();
	if (logger.isDebugEnabled()) {
		logger.debug("ZK node[path : {}] event type : {}", path, event.getType());
	}

	String serviceName = null;

	if (pathMatcher.match(serviceInstancePathPattern, path)) {
		Map<String, String> variables = pathMatcher
				.extractUriTemplateVariables(serviceInstancePathPattern, path);
		serviceName = variables.get(SERVICE_NAME_PATH_VARIABLE_NAME);
	}

	return serviceName;
}
 
Example 2
Source File: ZKCacheListener.java    From mpush with Apache License 2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework curator, TreeCacheEvent event) throws Exception {
    ChildData data = event.getData();
    if (data == null) return;
    String dataPath = data.getPath();
    if (Strings.isNullOrEmpty(dataPath)) return;
    if (dataPath.startsWith(watchPath)) {
        switch (event.getType()) {
            case NODE_ADDED:
                listener.onServiceAdded(dataPath, Jsons.fromJson(data.getData(), CommonServiceNode.class));
                break;
            case NODE_REMOVED:
                listener.onServiceRemoved(dataPath, Jsons.fromJson(data.getData(), CommonServiceNode.class));
                break;
            case NODE_UPDATED:
                listener.onServiceUpdated(dataPath, Jsons.fromJson(data.getData(), CommonServiceNode.class));
                break;
        }
        Logs.RSD.info("ZK node data change={}, nodePath={}, watchPath={}, ns={}");
    }
}
 
Example 3
Source File: AccumuloGraph.java    From vertexium with Apache License 2.0 6 votes vote down vote up
private void invalidatePropertyDefinitions(TreeCacheEvent event) {
    if (event == null || event.getData() == null) {
        return;
    }

    String path = event.getData().getPath();
    byte[] bytes = event.getData().getData();
    if (path == null || bytes == null) {
        return;
    }

    if (!path.startsWith(zkPath + "/" + ZK_DEFINE_PROPERTY)) {
        return;
    }

    String key = new String(bytes, StandardCharsets.UTF_8);
    if (key == null) {
        return;
    }

    String propertyName = key.substring(METADATA_DEFINE_PROPERTY_PREFIX.length());
    LOGGER.debug("invalidating property definition: %s", propertyName);
    invalidatePropertyDefinition(propertyName);
}
 
Example 4
Source File: SimpleEventListener.java    From metron with Apache License 2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
  String path = null;
  byte[] data = null;
  if(event != null && event.getData() != null) {
    path = event.getData().getPath();
    data = event.getData().getData();
  }
  LOG.debug("Type: {}, Path: {}, Data: {}", event.getType(), (path == null?"":path) , (data == null?"":new String(data,
      StandardCharsets.UTF_8)));
  List<Callback> callback = callbacks.get(event.getType());
  if(callback != null) {
    for(Callback cb : callback) {
      cb.apply(client, path, data);
    }
  }
}
 
Example 5
Source File: ZookeeperAppSubscriber.java    From sofa-dashboard-client with Apache License 2.0 5 votes vote down vote up
/**
 * Update cached application instance according to zookeeper node event.
 *
 * @param event zookeeper node changed event
 */
private void doCreateOrUpdateApplications(TreeCacheEvent event) {
    ChildData chileData = event.getData();
    Application app = ZookeeperRegistryUtils.parseSessionNode(chileData.getPath());
    if (app != null) {
        applications.compute(app.getAppName(), (key, value) -> {
            Set<Application> group = value == null ? new ConcurrentSkipListSet<>() : value;
            group.remove(app); // remove if exists
            group.add(app);
            return group;
        });
    }
}
 
Example 6
Source File: ZookeeperAppSubscriber.java    From sofa-dashboard-client with Apache License 2.0 5 votes vote down vote up
/**
 * Remove cached application instance according to zookeeper node event.
 *
 * @param event zookeeper node changed event
 */
private void doRemoveApplications(TreeCacheEvent event) {
    ChildData chileData = event.getData();
    if (chileData == null) {
        return; // Maybe null if session is timeout
    }

    Application app = ZookeeperRegistryUtils.parseSessionNode(chileData.getPath());
    if (app != null) {
        applications.computeIfPresent(app.getAppName(), (key, value) -> {
            value.remove(app); // Always remove whatever if it's exists
            return value;
        });
    }
}
 
Example 7
Source File: TopicListener.java    From kafka-monitor with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework curator, TreeCacheEvent event) throws Exception {
    ChildData data = event.getData();
    if (data != null) {
        if (event.getType() == NODE_ADDED) {

        }
        String path = data.getPath();
        //判断是否为topics节点
        if (path.contains(String.format("%s/",ZkUtils.BrokerTopicsPath())) && (!path.contains("partitions"))) {
            Topic topic = JSONObject.parseObject(data.getData(), Topic.class);
            String name = path.substring(path.lastIndexOf("/") + 1, path.length());
            topic.setName(name);

            int[] tPartiyions = topic.getPartitions().keySet().stream().mapToInt((t) -> Integer.valueOf(t)).sorted().toArray();
            for (Object key : tPartiyions
                    ) {
                String partitionPath = String.format("%s/partitions/%s/state", path, key);
                String state = new String(curator.getData().forPath(partitionPath));
                Partition partition = JSONObject.parseObject(state, Partition.class);
                JSONArray replicas = topic.getPartitions().getJSONArray(String.valueOf(key));
                int[] replicasArray = new int[replicas.size()];
                for (int i = 0; i <
                        replicas.size(); i++) {
                    replicasArray[i] = replicas.getInteger(i);
                }
                partition.setReplicasArray(replicasArray);

                topic.getPartitionMap().put((Integer) key, partition);
            }
            topicList.add(topic);
        }
    }
}
 
Example 8
Source File: AbstractJobListener.java    From shardingsphere-elasticjob-lite with Apache License 2.0 5 votes vote down vote up
@Override
public final void childEvent(final CuratorFramework client, final TreeCacheEvent event) {
    ChildData childData = event.getData();
    if (null == childData) {
        return;
    }
    String path = childData.getPath();
    if (path.isEmpty()) {
        return;
    }
    dataChanged(path, event.getType(), null == childData.getData() ? "" : new String(childData.getData(), Charsets.UTF_8));
}