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

The following examples show how to use org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent#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: StandbyNode.java    From niubi-job with Apache License 2.0 6 votes vote down vote up
@Override
public synchronized void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception {
    AssertHelper.isTrue(isJoined(), "illegal state .");
    boolean hasLeadership = leaderSelector != null && leaderSelector.hasLeadership();
    if (!hasLeadership) {
        return;
    }
    if (!EventHelper.isChildModifyEvent(event)) {
        return;
    }
    StandbyJobData standbyJobData = new StandbyJobData(event.getData());
    if (StringHelper.isEmpty(standbyJobData.getData().getJobOperation())) {
        return;
    }
    StandbyJobData.Data data = standbyJobData.getData();
    if (data.isUnknownOperation()) {
        return;
    }
    StandbyNodeData.Data nodeData = standbyApiFactory.nodeApi().getNode(nodePath).getData();
    executeOperation(nodeData, data);
}
 
Example 2
Source File: BinDataPathChildrenCacheListener.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData data = event.getData();
    switch (event.getType()) {

        case CHILD_ADDED:

            add(data.getPath().substring(data.getPath().lastIndexOf("/")+1),event.getData().getData()) ;
            break;
        case CHILD_REMOVED:
            delete(data.getPath().substring(data.getPath().lastIndexOf("/")+1),event.getData().getData()); ;
            break;
        case CHILD_UPDATED:
            add(data.getPath().substring(data.getPath().lastIndexOf("/")+1),event.getData().getData()) ;
            break;
        default:
            break;
    }
}
 
Example 3
Source File: RuleDataPathChildrenCacheListener.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData data = event.getData();
    switch (event.getType()) {

        case CHILD_ADDED:

            add(data.getPath().substring(data.getPath().lastIndexOf("/") + 1), event.getData().getData());
            break;
        case CHILD_REMOVED:
            delete(data.getPath().substring(data.getPath().lastIndexOf("/") + 1), event.getData().getData());
            ;
            break;
        case CHILD_UPDATED:
            add(data.getPath().substring(data.getPath().lastIndexOf("/") + 1), event.getData().getData());
            break;
        default:
            break;
    }
}
 
Example 4
Source File: DDLChildListener.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ClusterDelayProvider.delayAfterGetDdlNotice();
    ChildData childData = event.getData();
    switch (event.getType()) {
        case CHILD_ADDED:
            try {
                lockTableByNewNode(childData);
            } catch (Exception e) {
                LOGGER.warn("CHILD_ADDED error", e);
            }
            break;
        case CHILD_UPDATED:
            updateMeta(childData);
            break;
        case CHILD_REMOVED:
            deleteNode(childData);
            break;
        default:
            break;
    }
}
 
Example 5
Source File: ViewChildListener.java    From dble with GNU General Public License v2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData childData = event.getData();
    ClusterDelayProvider.delayWhenReponseViewNotic();
    switch (event.getType()) {
        case CHILD_ADDED:
            createOrUpdateViewMeta(childData, false);
            break;
        case CHILD_UPDATED:
            createOrUpdateViewMeta(childData, true);
            break;
        case CHILD_REMOVED:
            deleteNode(childData);
            break;
        default:
            break;
    }
}
 
Example 6
Source File: NodeDiscovery.java    From curator-extensions with Apache License 2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) {
    String nodePath = null;
    T nodeData = null;
    if (event.getData() != null) {
        nodePath = event.getData().getPath();
        nodeData = parseChildData(event.getData());
    }
    switch (event.getType()) {
        case CHILD_ADDED:
            addNode(nodePath, nodeData);
            break;

        case CHILD_REMOVED:
            removeNode(nodePath, nodeData);
            break;

        case CHILD_UPDATED:
            updateNode(nodePath, nodeData);
            break;
    }
}
 
Example 7
Source File: OfflineStatusListener.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData childData = event.getData();
    switch (event.getType()) {
        case CHILD_ADDED:
            break;
        case CHILD_UPDATED:
            break;
        case CHILD_REMOVED:
            deleteNode(childData);
            break;
        default:
            break;
    }
}
 
Example 8
Source File: CuratorClientService.java    From knox with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent)
        throws Exception {
    ChildData childData = pathChildrenCacheEvent.getData();
    if (childData != null) {
        ChildEntryListener.Type eventType = adaptType(pathChildrenCacheEvent.getType());
        if (eventType != null) {
            delegate.childEvent(client, eventType, childData.getPath());
        }
    }
}
 
Example 9
Source File: RoutingAwareResourceConfigFactoryBean.java    From cloud-config with MIT License 5 votes vote down vote up
@Override
protected void handleChildUpdated(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    String nodeName = safeGetNodeNameFromEvent(event);
    Object configObj = configHolder.get(nodeName);
    if( configObj instanceof CloudResourceConfig) {
        CloudResourceConfig resourceConfig = (CloudResourceConfig)configObj;
        ChildData childData = event.getData();
        Object newConfig = createConfig(childData.getData(), getObject());
        BeanUtils.copyProperties(newConfig, resourceConfig);
        resourceConfig.reload();
    }
}
 
Example 10
Source File: SimpleResourceConfigFactoryBean.java    From cloud-config with MIT License 5 votes vote down vote up
@Override
protected void handleChildUpdated(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData childData = event.getData();
    String nodeName = safeGetNodeNameFromEvent(event);
    if(Arrays.asList(configProfiles).contains(nodeName) || canApplyForLocalMachine(nodeName)) {
        T newConfig = createConfig(childData.getData(), getObject());
        BeanUtils.copyProperties(newConfig, getObject());
        getObject().reload();
    }
}
 
Example 11
Source File: ZkDiscoveryService.java    From iotplatform with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent pathChildrenCacheEvent)
    throws Exception {
  ChildData data = pathChildrenCacheEvent.getData();
  if (data == null) {
    log.debug("Ignoring {} due to empty child data", pathChildrenCacheEvent);
    return;
  } else if (data.getData() == null) {
    log.debug("Ignoring {} due to empty child's data", pathChildrenCacheEvent);
    return;
  } else if (nodePath != null && nodePath.equals(data.getPath())) {
    log.debug("Ignoring event about current server {}", pathChildrenCacheEvent);
    return;
  }
  ServerInstance instance;
  try {
    instance = new ServerInstance(ServerInfo.parseFrom(data.getData()));
  } catch (IOException e) {
    log.error("Failed to decode server instance for node {}", data.getPath(), e);
    throw e;
  }
  log.info("Processing [{}] event for [{}:{}]", pathChildrenCacheEvent.getType(), instance.getHost(),
      instance.getPort());
  switch (pathChildrenCacheEvent.getType()) {
  case CHILD_ADDED:
    listeners.forEach(listener -> listener.onServerAdded(instance));
    break;
  case CHILD_UPDATED:
    listeners.forEach(listener -> listener.onServerUpdated(instance));
    break;
  case CHILD_REMOVED:
    listeners.forEach(listener -> listener.onServerRemoved(instance));
    break;
  }
}
 
Example 12
Source File: DataHostStatusListener.java    From dble with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData childData = event.getData();
    switch (event.getType()) {
        case CHILD_ADDED:
            break;
        case CHILD_UPDATED:
            updateStatus(childData);
            break;
        case CHILD_REMOVED:
            break;
        default:
            break;
    }
}
 
Example 13
Source File: RuleFunctionCacheListener.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData data = event.getData();
    switch (event.getType()) {

        case CHILD_ADDED:
            addOrUpdate();
            break;
        case CHILD_UPDATED:
            addOrUpdate();
            break;
        default:
            break;
    }
}
 
Example 14
Source File: AbstractChildrenDataListener.java    From eagle with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    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));
}
 
Example 15
Source File: ZkServiceDiscover.java    From redant with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    ChildData data = event.getData();
    if(data==null || data.getData()==null){
        return;
    }
    Node node = Node.parse(JSON.parseObject(data.getData(),JSONObject.class));
    if(node ==null){
        LOGGER.error("get a null slave with eventType={},path={},data={}",event.getType(),data.getPath(),data.getData());
    }else {
        switch (event.getType()) {
            case CHILD_ADDED:
                nodeMap.put(node.getId(), node);
                LOGGER.info("CHILD_ADDED with path={},data={},current slave size={}", data.getPath(), new String(data.getData(),CharsetUtil.UTF_8), nodeMap.size());
                break;
            case CHILD_REMOVED:
                nodeMap.remove(node.getId());
                LOGGER.info("CHILD_REMOVED with path={},data={},current slave size={}", data.getPath(), new String(data.getData(),CharsetUtil.UTF_8), nodeMap.size());
                break;
            case CHILD_UPDATED:
                nodeMap.replace(node.getId(), node);
                LOGGER.info("CHILD_UPDATED with path={},data={},current slave size={}", data.getPath(), new String(data.getData(),CharsetUtil.UTF_8), nodeMap.size());
                break;
            default:
                break;
        }
    }
}
 
Example 16
Source File: AgentServiceImpl.java    From flow-platform-x with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) {
    ChildData data = event.getData();

    if (ChildOperations.contains(event.getType())) {
        handleAgentStatusChange(event);
    }
}
 
Example 17
Source File: ProviderNodeChangeListener.java    From sofa-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {

    // 解决自动重连情况下出现的空指针问题
    ChildData data = event.getData();
    if (data == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("event type : {}", event.getType());
        }
        return;
    }

    final String path = data.getPath();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("provider : {}", path);
    }
    switch (event.getType()) {

        case CHILD_ADDED:
            String providerData = StringUtil.substringAfterLast(path, "/");
            String serviceName = StringUtil.substringBetween(path, "/sofa-rpc/", "/providers/");
            List<RpcProvider> providerDataList = new ArrayList<>();
            providerDataList.add(convert2Provider(serviceName, providerData));
            registryDataCache.addProviders(serviceName, providerDataList);
            break;
        case CHILD_REMOVED:

            List<RpcProvider> removeProviders = new ArrayList<>();
            String removeProviderData = StringUtil.substringAfterLast(path, "/");
            String removeServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/providers/");
            removeProviders.add(convert2Provider(removeServiceName, removeProviderData));
            registryDataCache.removeProviders(removeServiceName, removeProviders);
            break;
        case CHILD_UPDATED:

            List<RpcProvider> updateProviders = new ArrayList<>();
            String updateProviderData = StringUtil.substringAfterLast(path, "/");
            String updateServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/providers/");
            updateProviders.add(convert2Provider(updateServiceName, updateProviderData));
            registryDataCache.updateProviders(updateServiceName, updateProviders);
            break;

        default:
            break;
    }

}
 
Example 18
Source File: AbstractResourceConfigFactoryBean.java    From cloud-config with MIT License 4 votes vote down vote up
protected String safeGetNodeNameFromEvent(PathChildrenCacheEvent event) {
    return event.getData() != null ?
            ZKPaths.getNodeFromPath(event.getData().getPath()) : "";
}
 
Example 19
Source File: RoutingAwareResourceConfigFactoryBean.java    From cloud-config with MIT License 4 votes vote down vote up
@Override
protected void handleChildAdded(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    String nodeName = event.getData()!=null ?
            ZKPaths.getNodeFromPath(event.getData().getPath()) : "";
    buildResourceConfig(nodeName);
}
 
Example 20
Source File: ConsumerNodeChangeListener.java    From sofa-dashboard with Apache License 2.0 4 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) {

    // 解决自动重连情况下出现的空指针问题
    ChildData data = event.getData();
    if (data == null) {
        if (LOGGER.isDebugEnabled()) {
            LOGGER.debug("event type : {}", event.getType());
        }
        return;
    }

    final String path = data.getPath();

    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("consumer : {}", path);
    }

    switch (event.getType()) {

        case CHILD_ADDED:

            String addConsumerData = StringUtil.substringAfterLast(path, "/");
            String addServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/consumers/");

            List<RpcConsumer> addConsumers = new ArrayList<>();
            addConsumers.add(convert2Consumer(addServiceName, addConsumerData));
            registryDataCache.addConsumers(addServiceName, addConsumers);
            break;
        case CHILD_REMOVED:

            String removeConsumerData = StringUtil.substringAfterLast(path, "/");
            String removeServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/consumers/");

            List<RpcConsumer> removeConsumers = new ArrayList<>();
            removeConsumers.add(convert2Consumer(removeServiceName, removeConsumerData));
            registryDataCache.removeConsumers(removeServiceName, removeConsumers);

            break;
        case CHILD_UPDATED:

            String updateConsumerData = StringUtil.substringAfterLast(path, "/");
            String updateServiceName = StringUtil.substringBetween(path, "/sofa-rpc/",
                "/consumers/");

            List<RpcConsumer> updateConsumers = new ArrayList<>();
            updateConsumers.add(convert2Consumer(updateServiceName, updateConsumerData));
            registryDataCache.updateConsumers(updateServiceName, updateConsumers);
            break;

        default:
            break;
    }

}