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

The following examples show how to use org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent#getType() . 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: 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 2
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 3
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 4
Source File: ZKWorkerController.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * listen for additions to the job events directory in zk server
 * @param cache
 */
private void addEventsChildrenCacheListener(PathChildrenCache cache) {
  PathChildrenCacheListener listener = new PathChildrenCacheListener() {

    public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) {

      switch (event.getType()) {
        case CHILD_ADDED:
          eventPublished(event);
          break;

        default:
          // nothing to do
      }
    }
  };
  cache.getListenable().addListener(listener);
}
 
Example 5
Source File: ZKMasterController.java    From twister2 with Apache License 2.0 6 votes vote down vote up
/**
 * create the listener for persistent worker znodes to determine worker status changes
 */
private void addPersChildrenCacheListener(PathChildrenCache cache) {
  PathChildrenCacheListener listener = new PathChildrenCacheListener() {

    public void childEvent(CuratorFramework clientOfEvent, PathChildrenCacheEvent event) {

      switch (event.getType()) {

        case CHILD_UPDATED:
          childZnodeUpdated(event);
          break;

        default:
          // nothing to do
      }
    }
  };
  cache.getListenable().addListener(listener);
}
 
Example 6
Source File: ZooKeeperCommandExecutor.java    From centraldogma with Apache License 2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework unused, PathChildrenCacheEvent event) throws Exception {
    if (event.getType() != PathChildrenCacheEvent.Type.CHILD_ADDED) {
        return;
    }

    final long lastKnownRevision = revisionFromPath(event.getData().getPath());
    try {
        replayLogs(lastKnownRevision);
    } catch (ReplicationException ignored) {
        // replayLogs() logs and handles the exception already, so we just bail out here.
        return;
    }

    oldLogRemover.touch();
}
 
Example 7
Source File: BasePathChildrenCacheListener.java    From hermes with Apache License 2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
	if (event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED
	      || event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED
	      || event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {

		new VersionGuardedTask(m_version) {

			@Override
			public String name() {
				return getName();
			}

			@Override
			protected void doRun() {
				childChanged();
			}

			@Override
			protected void onGuardNotPass() {
				removeListener();
			}
		}.run();

	}
}
 
Example 8
Source File: BrokerListener.java    From kafka-monitor with Apache License 2.0 6 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {

    logger.debug("BrokerListener {} {}", event.getType(), event.getData());

    switch (event.getType()) {
        case CHILD_REMOVED:
            removeBroker(parseBrokerID(event.getData()));
            break;
        case CHILD_ADDED:
        case CHILD_UPDATED:
            addBroker(parseBroker(event.getData()));
            break;
        case INITIALIZED:
            brokerPathCache.getCurrentData().stream()
                    .map(BrokerListener.this::parseBroker)
                    .forEach(broker -> addBroker(broker));
            break;
        default:
            break;

    }

}
 
Example 9
Source File: ZookeeperSyncToNacosServiceImpl.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
private void processEvent(TaskDO taskDO, NamingService destNamingService, PathChildrenCacheEvent event, String path,
    Map<String, String> queryParam) throws NacosException {
    Map<String, String> ipAndPortParam = parseIpAndPortString(path);
    Instance instance = buildSyncInstance(queryParam, ipAndPortParam, taskDO);
    switch (event.getType()) {
        case CHILD_ADDED:
        case CHILD_UPDATED:

            destNamingService.registerInstance(
                getServiceNameFromCache(taskDO.getTaskId(), queryParam), instance);
            break;
        case CHILD_REMOVED:

            destNamingService.deregisterInstance(
                getServiceNameFromCache(taskDO.getTaskId(), queryParam),
                ipAndPortParam.get(INSTANCE_IP_KEY),
                Integer.parseInt(ipAndPortParam.get(INSTANCE_PORT_KEY)));
            nacosServiceNameMap.remove(taskDO.getTaskId());
            break;
        default:
            break;
    }
}
 
Example 10
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 11
Source File: ServiceCacheImpl.java    From xian with Apache License 2.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
    boolean notifyListeners = false;
    ServiceInstance<T> serviceInstance = null;
    switch (event.getType()) {
        case CHILD_ADDED:
        case CHILD_UPDATED: {
            serviceInstance = addInstance(event.getData(), false);
            notifyListeners = true;
            break;
        }

        case CHILD_REMOVED: {
            serviceInstance = instances.remove(instanceIdFromData(event.getData()));
            notifyListeners = true;
            break;
        }
    }

    if (notifyListeners) {
        final ServiceInstance<T> fServiceInstance = serviceInstance;
        listenerContainer.forEach(
                new Function<ServiceCacheListener<T>, Void>() {
                    @Override
                    public Void apply(ServiceCacheListener<T> listener) {
                        listener.cacheChanged();
                        listener.cacheChanged(event, fServiceInstance);
                        return null;
                    }
                }
        );
    }
}
 
Example 12
Source File: CuratorMaster.java    From zookeeper-book-example with Apache License 2.0 5 votes vote down vote up
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) {
    if(event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED) {
        /*
         * Obtain just the worker's name
         */
        try{
            getAbsentWorkerTasks(event.getData().getPath().replaceFirst("/workers/", ""));
        } catch (Exception e) {
            LOG.error("Exception while trying to re-assign tasks", e);
        }
    } 
}
 
Example 13
Source File: CommandPathListener.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 {
    switch (event.getType()) {
    case CHILD_ADDED:
        // 在发生节点添加的时候,则执行接收命令并执行
        // 1,首先检查
        String path = event.getData().getPath();
        String basePath = ZKUtils.getZKBasePath() + ZKHandler.ZK_NODE_PATH + "/";

        // 检查节点与当前的节点是否一致
        String node = path.substring(basePath.length());

        if (node.equals(ZkConfig.getInstance().getValue(ZkParamCfg.ZK_CFG_MYID))) {
            // 检查命令内容是否为
            if (ZKHandler.RELOAD_FROM_ZK.equals(new String(client.getData().forPath(path)))) {
                // 从服务器上下载最新的配制文件信息
                ZktoXmlMain.ZKLISTENER.notifly(ZkNofiflyCfg.ZK_NOTIFLY_LOAD_ALL.getKey());
                // 重新加载配制信息
                reload(path);
                // 完成之后,删除命令信息, 以供下次读取
                client.delete().forPath(event.getData().getPath());
                LOGGER.info("CommandPathListener path:" + path + " reload success");
            }
        }

        break;
    case CHILD_UPDATED:
        break;
    case CHILD_REMOVED:
        break;
    default:
        break;
    }

}
 
Example 14
Source File: MigrateTaskWatch.java    From Mycat2 with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void childEvent(CuratorFramework curatorFramework,
                       PathChildrenCacheEvent event) throws Exception {
    switch (event.getType()) {
        case CHILD_ADDED:
            if (isTaskErrorOrSucess(event)) break;
            addOrUpdate(event);
            String path = event.getData().getPath() + "/_prepare";
            if (curatorFramework.checkExists().forPath(path) == null) {
                curatorFramework.create().creatingParentsIfNeeded().forPath(path);
            }
            ZKUtils.addChildPathCache(path, new SwitchPrepareListener());

            String commitPath = event.getData().getPath() + "/_commit";
            if (curatorFramework.checkExists().forPath(commitPath) == null) {
                curatorFramework.create().creatingParentsIfNeeded().forPath(commitPath);
            }
            ZKUtils.addChildPathCache(commitPath, new SwitchCommitListener());


            String cleanPath = event.getData().getPath() + "/_clean";
            if (curatorFramework.checkExists().forPath(cleanPath) == null) {
                curatorFramework.create().creatingParentsIfNeeded().forPath(cleanPath);
            }
            ZKUtils.addChildPathCache(cleanPath, new SwitchCleanListener());
            LOGGER.info("table CHILD_ADDED: " + event.getData().getPath());
            break;
        case CHILD_UPDATED:
            if (isTaskErrorOrSucess(event)) break;
            addOrUpdate(event);
            LOGGER.info("CHILD_UPDATED: " + event.getData().getPath());
            break;
        default:
            break;
    }
}
 
Example 15
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 16
Source File: TenantRepository.java    From vespa with Apache License 2.0 5 votes vote down vote up
private void childEvent(CuratorFramework framework, PathChildrenCacheEvent event) {
    switch (event.getType()) {
        case CHILD_ADDED:
        case CHILD_REMOVED:
            updateTenants();
            break;
    }
}
 
Example 17
Source File: PathCacheExample.java    From curator with Apache License 2.0 5 votes vote down vote up
private static void addListener(PathChildrenCache cache)
{
    // a PathChildrenCacheListener is optional. Here, it's used just to log changes
    PathChildrenCacheListener listener = new PathChildrenCacheListener()
    {
        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception
        {
            switch ( event.getType() )
            {
                case CHILD_ADDED:
                {
                    System.out.println("Node added: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                }

                case CHILD_UPDATED:
                {
                    System.out.println("Node changed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                }

                case CHILD_REMOVED:
                {
                    System.out.println("Node removed: " + ZKPaths.getNodeFromPath(event.getData().getPath()));
                    break;
                }
            }
        }
    };
    cache.getListenable().addListener(listener);
}
 
Example 18
Source File: EventHelper.java    From niubi-job with Apache License 2.0 4 votes vote down vote up
static boolean isChildUpdateEvent(PathChildrenCacheEvent event) {
    return event != null && event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED;
}
 
Example 19
Source File: EventHelper.java    From niubi-job with Apache License 2.0 4 votes vote down vote up
static boolean isChildRemoveEvent(PathChildrenCacheEvent event) {
    return event != null && event.getType() == PathChildrenCacheEvent.Type.CHILD_REMOVED;
}
 
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;
    }

}