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

The following examples show how to use org.apache.curator.framework.recipes.cache.PathChildrenCache#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: ZookeeperRegistryCenter.java    From eagle with Apache License 2.0 6 votes vote down vote up
@Override
public PathChildrenCache addChildrenCacheData(final String cachePath, boolean cacheData) {
    if (childrenCaches.containsKey(cachePath)) {
        return childrenCaches.get(cachePath);
    }
    PathChildrenCache cache = new PathChildrenCache(client, cachePath, cacheData);
    try {
        cache.start();
        //CHECKSTYLE:OFF
    } catch (final Exception ex) {
        //CHECKSTYLE:ON
        throw new EagleFrameException(ex);
    }
    childrenCaches.put(cachePath, cache);
    return cache;
}
 
Example 2
Source File: ZkDiscoveryService.java    From iotplatform with Apache License 2.0 6 votes vote down vote up
@PostConstruct
public void init() {
  log.info("Initializing...");
  Assert.hasLength(zkUrl, MiscUtils.missingProperty("zk.url"));
  Assert.notNull(zkRetryInterval, MiscUtils.missingProperty("zk.retry_interval_ms"));
  Assert.notNull(zkConnectionTimeout, MiscUtils.missingProperty("zk.connection_timeout_ms"));
  Assert.notNull(zkSessionTimeout, MiscUtils.missingProperty("zk.session_timeout_ms"));

  log.info("Initializing discovery service using ZK connect string: {}", zkUrl);

  zkNodesDir = zkDir + "/nodes";
  try {
    client = CuratorFrameworkFactory.newClient(zkUrl, zkSessionTimeout, zkConnectionTimeout,
        new RetryForever(zkRetryInterval));
    client.start();
    client.blockUntilConnected();
    cache = new PathChildrenCache(client, zkNodesDir, true);
    cache.getListenable().addListener(this);
    cache.start();
  } catch (Exception e) {
    log.error("Failed to connect to ZK: {}", e.getMessage(), e);
    CloseableUtils.closeQuietly(client);
    throw new RuntimeException(e);
  }
}
 
Example 3
Source File: TransactorCache.java    From fluo with Apache License 2.0 6 votes vote down vote up
public TransactorCache(Environment env) {
  final FluoConfiguration conf = env.getConfiguration();

  timeoutCache =
      CacheBuilder.newBuilder().maximumSize(FluoConfigurationImpl.getTransactorMaxCacheSize(conf))
          .expireAfterAccess(
              FluoConfigurationImpl.getTransactorCacheTimeout(conf, TimeUnit.MILLISECONDS),
              TimeUnit.MILLISECONDS)
          .concurrencyLevel(10).build();

  this.env = env;
  cache = new PathChildrenCache(env.getSharedResources().getCurator(),
      ZookeeperPath.TRANSACTOR_NODES, true);
  try {
    cache.start(StartMode.BUILD_INITIAL_CACHE);
    status = TcStatus.OPEN;
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 4
Source File: ZKUtils.java    From Mycat2 with GNU General Public License v3.0 6 votes vote down vote up
public static void addChildPathCache(String path, PathChildrenCacheListener listener) {
    NameableExecutor businessExecutor = MycatServer.getInstance().getBusinessExecutor();
    ExecutorService executor = businessExecutor == null ? Executors.newFixedThreadPool(5) : businessExecutor;

    try {
        /**
         * 监听子节点的变化情况
         */
        final PathChildrenCache childrenCache = new PathChildrenCache(getConnection(), path, true);
        childrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
        childrenCache.getListenable().addListener(listener, executor);
        watchMap.put(path, childrenCache);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: ZKClientImpl.java    From codes-scratch-zookeeper-netty with Apache License 2.0 5 votes vote down vote up
@Override
public List<String> listenChildrenPath(final String parent, final NodeListener listener, final boolean sync)
        throws Exception {
    PathChildrenCache cache = new PathChildrenCache(client, parent, false, false, EVENT_THREAD_POOL);
    cache.getListenable().addListener(new PathChildrenCacheListener() {
        @Override
        public void childEvent(CuratorFramework c, PathChildrenCacheEvent e)
                throws Exception {
            if (e.getData() == null) { return; }
            switch (e.getType()) {
                case CHILD_ADDED:
                    listener.nodeChanged(ZKClientImpl.this,
                                         new ChangedEvent(e.getData().getPath(), ChangedEvent.Type.CHILD_ADDED));
                    break;
                case CHILD_REMOVED:
                    listener.nodeChanged(ZKClientImpl.this,
                                         new ChangedEvent(e.getData().getPath(), ChangedEvent.Type.CHILD_REMOVED));
                    break;
                case CHILD_UPDATED:
                    listener.nodeChanged(ZKClientImpl.this,
                                         new ChangedEvent(e.getData().getPath(), ChangedEvent.Type.CHILD_UPDATED));
                    break;
            }
        }
    }, SAME_EXECUTOR);
    PathChildrenCache.StartMode mode = sync ? PathChildrenCache.StartMode.BUILD_INITIAL_CACHE : PathChildrenCache.StartMode.NORMAL;
    cache.start(mode);
    List<ChildData> children = cache.getCurrentData();
    List<String> result = new ArrayList<String>();
    for (ChildData child : children) {
        result.add(child.getPath());
    }
    return result;
}
 
Example 6
Source File: ScrollChildrenChangeListener.java    From SkyEye 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:
            PathChildrenCache pathChildrenCache = new PathChildrenCache(client, event.getData().getPath(), true);
            pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
            pathChildrenCache.getListenable().addListener(new AppChildrenChangeListener(this.rabbitmqService, this.zkClient, this.appInfoService));
            LOGGER.info("app added: " + event.getData().getPath());
            break;
        case CHILD_REMOVED:
            LOGGER.info("app removed: " + event.getData().getPath());
            break;
    }
}
 
Example 7
Source File: Zk.java    From t-io with Apache License 2.0 5 votes vote down vote up
/**
 * 
 * @param path
 * @param pathChildrenCacheListener
 * @throws Exception
 */
@SuppressWarnings("resource")
public static void addPathChildrenCacheListener(String path, PathChildrenCacheListener pathChildrenCacheListener) throws Exception {
	PathChildrenCache cache = new PathChildrenCache(zkclient, path, true);
	cache.start();

	//		System.out.println("监听开始/zk........");
	//		PathChildrenCacheListener plis = 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(pathChildrenCacheListener);

}
 
Example 8
Source File: OracleClient.java    From fluo with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {

  try {
    synchronized (this) {
      // want this code to be mutually exclusive with close() .. so if in middle of setup, close
      // method will wait till finished
      if (closed.get()) {
        return;
      }

      curatorFramework = CuratorUtil.newAppCurator(env.getConfiguration());
      CuratorCnxnListener cnxnListener = new CuratorCnxnListener();
      curatorFramework.getConnectionStateListenable().addListener(cnxnListener);
      curatorFramework.start();

      while (!cnxnListener.isConnected()) {
        UtilWaitThread.sleep(200);
      }

      leaderLatch = new LeaderLatch(curatorFramework, ZookeeperPath.ORACLE_SERVER);

      pathChildrenCache =
          new PathChildrenCache(curatorFramework, ZookeeperPath.ORACLE_SERVER, true);
      pathChildrenCache.getListenable().addListener(this);
      pathChildrenCache.start();

      connect();
    }
    doWork();
  } catch (Exception e) {
    if (!closed.get()) {
      log.error("Exception occurred in run() method", e);
    } else {
      log.debug("Exception occurred in run() method", e);
    }
  }
}
 
Example 9
Source File: ZookeeperRegistry.java    From sofa-rpc with Apache License 2.0 5 votes vote down vote up
/**
 * 订阅接口级配置
 *
 * @param config   provider/consumer config
 * @param listener config listener
 */
protected void subscribeConfig(final AbstractInterfaceConfig config, ConfigListener listener) {
    try {
        if (configObserver == null) { // 初始化
            configObserver = new ZookeeperConfigObserver();
        }
        configObserver.addConfigListener(config, listener);
        final String configPath = buildConfigPath(rootPath, config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, configPath, true);
        pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client1, PathChildrenCacheEvent event) throws Exception {
                if (LOGGER.isDebugEnabled(config.getAppName())) {
                    LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
                }
                switch (event.getType()) {
                    case CHILD_ADDED: //新增接口级配置
                        configObserver.addConfig(config, configPath, event.getData());
                        break;
                    case CHILD_REMOVED: //删除接口级配置
                        configObserver.removeConfig(config, configPath, event.getData());
                        break;
                    case CHILD_UPDATED:// 更新接口级配置
                        configObserver.updateConfig(config, configPath, event.getData());
                        break;
                    default:
                        break;
                }
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_CONFIG_CACHE.put(configPath, pathChildrenCache);
        configObserver.updateConfigAll(config, configPath, pathChildrenCache.getCurrentData());
    } catch (Exception e) {
        throw new SofaRpcRuntimeException(LogCodes.getLog(LogCodes.ERROR_SUB_PROVIDER_CONFIG, EXT_NAME), e);
    }
}
 
Example 10
Source File: PartitionManager.java    From fluo with Apache License 2.0 5 votes vote down vote up
PartitionManager(Environment env, long minSleepTime, long maxSleepTime) {
  try {
    this.curator = env.getSharedResources().getCurator();
    this.env = env;

    this.minSleepTime = minSleepTime;
    this.maxSleepTime = maxSleepTime;
    this.retrySleepTime = minSleepTime;

    groupSize = env.getConfiguration().getInt(FluoConfigurationImpl.WORKER_PARTITION_GROUP_SIZE,
        FluoConfigurationImpl.WORKER_PARTITION_GROUP_SIZE_DEFAULT);

    myESNode = new PersistentNode(curator, CreateMode.EPHEMERAL_SEQUENTIAL, false,
        ZookeeperPath.FINDERS + "/" + ZK_FINDER_PREFIX, ("" + groupSize).getBytes(UTF_8));
    myESNode.start();
    myESNode.waitForInitialCreate(1, TimeUnit.MINUTES);

    childrenCache = new PathChildrenCache(curator, ZookeeperPath.FINDERS, true);
    childrenCache.getListenable().addListener(new FindersListener());
    childrenCache.start(StartMode.BUILD_INITIAL_CACHE);

    schedExecutor = Executors.newScheduledThreadPool(1,
        new FluoThreadFactory("Fluo worker partition manager"));
    schedExecutor.scheduleWithFixedDelay(new CheckTabletsTask(), 0, maxSleepTime,
        TimeUnit.MILLISECONDS);

    scheduleUpdate();
  } catch (Exception e) {
    throw new RuntimeException(e);
  }
}
 
Example 11
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 订阅IP级配置(服务发布暂时不支持动态配置,暂时支持订阅ConsumerConfig参数设置)
 *
 * @param config   consumer config
 * @param listener config listener
 */
protected void subscribeOverride(final ConsumerConfig config, ConfigListener listener) {
    try {
        if (overrideObserver == null) {
            overrideObserver = new ZookeeperOverrideObserver();
        }
        overrideObserver.addConfigListener(config, listener);
        final String overridePath = buildOverridePath(rootPath, config);
        final AbstractInterfaceConfig registerConfig = getRegisterConfig(config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, overridePath,
            true);
        pathChildrenCache.getListenable().addListener((client1, event) -> {
            if (LOGGER.isDebugEnabled(config.getAppName())) {
                LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
            }
            switch (event.getType()) {
                case CHILD_ADDED:
                    overrideObserver.addConfig(config, overridePath, event.getData());
                    break;
                case CHILD_REMOVED:
                    overrideObserver.removeConfig(config, overridePath, event.getData(),
                        registerConfig);
                    break;
                case CHILD_UPDATED:
                    overrideObserver.updateConfig(config, overridePath, event.getData());
                    break;
                default:
                    break;
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_OVERRIDE_CACHE.put(overridePath, pathChildrenCache);
        overrideObserver.updateConfigAll(config, overridePath,
            pathChildrenCache.getCurrentData());
    } catch (Throwable t) {
        throw new SofaRpcRuntimeException(
            "Failed to subscribe provider config from zookeeperRegistry!", t);
    }
}
 
Example 12
Source File: Curator4ZookeeperRegistry.java    From spring-cloud-sofastack-samples with Apache License 2.0 5 votes vote down vote up
/**
 * 订阅接口级配置
 *
 * @param config   provider/consumer config
 * @param listener config listener
 */
protected void subscribeConfig(final AbstractInterfaceConfig config, ConfigListener listener) {
    try {
        if (configObserver == null) {
            configObserver = new ZookeeperConfigObserver();
        }
        configObserver.addConfigListener(config, listener);
        final String configPath = buildConfigPath(rootPath, config);
        // 监听配置节点下 子节点增加、子节点删除、子节点Data修改事件
        PathChildrenCache pathChildrenCache = new PathChildrenCache(zkClient, configPath, true);
        pathChildrenCache.getListenable().addListener((client1, event) -> {
            if (LOGGER.isDebugEnabled(config.getAppName())) {
                LOGGER.debug("Receive zookeeper event: " + "type=[" + event.getType() + "]");
            }
            switch (event.getType()) {
                case CHILD_ADDED:
                    configObserver.addConfig(config, configPath, event.getData());
                    break;
                case CHILD_REMOVED:
                    configObserver.removeConfig(config, configPath, event.getData());
                    break;
                case CHILD_UPDATED:
                    configObserver.updateConfig(config, configPath, event.getData());
                    break;
                default:
                    break;
            }
        });
        pathChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        INTERFACE_CONFIG_CACHE.put(configPath, pathChildrenCache);
        configObserver.updateConfigAll(config, configPath, pathChildrenCache.getCurrentData());
    } catch (Throwable t) {
        throw new SofaRpcRuntimeException(
            "Failed to subscribe provider config from zookeeperRegistry!", t);
    }
}
 
Example 13
Source File: ZookeeperDistributedLock.java    From kylin with Apache License 2.0 5 votes vote down vote up
@Override
public Closeable watchLocks(String lockPathRoot, Executor executor, final Watcher watcher) {
    PathChildrenCache cache = new PathChildrenCache(curator, lockPathRoot, true);
    try {
        cache.start();
        cache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
                switch (event.getType()) {
                case CHILD_ADDED:
                    watcher.onLock(event.getData().getPath(),
                            new String(event.getData().getData(), StandardCharsets.UTF_8));
                    break;
                case CHILD_REMOVED:
                    watcher.onUnlock(event.getData().getPath(),
                            new String(event.getData().getData(), StandardCharsets.UTF_8));
                    break;
                default:
                    break;
                }
            }
        }, executor);
    } catch (Exception ex) {
        logger.error("Error to watch lock path " + lockPathRoot, ex);
    }
    return cache;
}
 
Example 14
Source File: ZkResAware.java    From xian with Apache License 2.0 4 votes vote down vote up
@Override
public PathChildrenCache load(String pluginName) throws Exception {
    PathChildrenCache cache = new PathChildrenCache(ZkConnection.client, fullPath(pluginName), true);
    cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
    return cache;
}
 
Example 15
Source File: CuratorPCWatcher.java    From BigData-In-Practice with Apache License 2.0 4 votes vote down vote up
public static void main(String[] args) throws Exception {
    final String nodePath = "/testZK";
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(10000, 5);
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkServerIps)
            .sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
    client.start();
    try {
        // 为子节点添加watcher,PathChildrenCache: 监听数据节点的增删改,可以设置触发的事件
        final PathChildrenCache childrenCache = new PathChildrenCache(client, nodePath, true);

        /**
         * StartMode: 初始化方式
         *  - POST_INITIALIZED_EVENT:异步初始化,初始化之后会触发事件
         *  - NORMAL:异步初始化
         *  - BUILD_INITIAL_CACHE:同步初始化
         */
        childrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

        // 列出子节点数据列表,需要使用BUILD_INITIAL_CACHE同步初始化模式才能获得,异步是获取不到的
        List<ChildData> childDataList = childrenCache.getCurrentData();
        System.out.println("当前节点的子节点详细数据列表:");
        for (ChildData childData : childDataList) {
            System.out.println("\t* 子节点路径:" + new String(childData.getPath()) + ",该节点的数据为:" + new String(childData.getData()));
        }

        // 添加事件监听器
        childrenCache.getListenable().addListener(new PathChildrenCacheListener() {
            @Override
            public void childEvent(CuratorFramework curatorFramework, PathChildrenCacheEvent event) throws Exception {
                // 通过判断event type的方式来实现不同事件的触发
                if (event.getType().equals(PathChildrenCacheEvent.Type.INITIALIZED)) {  // 子节点初始化时触发
                    System.out.println("子节点初始化成功");
                } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_ADDED)) {  // 添加子节点时触发
                    System.out.print("子节点:" + event.getData().getPath() + " 添加成功,");
                    System.out.println("该子节点的数据为:" + new String(event.getData().getData()));
                } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_REMOVED)) {  // 删除子节点时触发
                    System.out.println("子节点:" + event.getData().getPath() + " 删除成功");
                } else if (event.getType().equals(PathChildrenCacheEvent.Type.CHILD_UPDATED)) {  // 修改子节点数据时触发
                    System.out.print("子节点:" + event.getData().getPath() + " 数据更新成功,");
                    System.out.println("子节点:" + event.getData().getPath() + " 新的数据为:" + new String(event.getData().getData()));
                }
            }
        });
        Thread.sleep(100000); // sleep 100秒,在 zkCli.sh 操作子节点,注意查看控制台的输出
    } finally {
        client.close();
    }
}
 
Example 16
Source File: CuratorChildrenCache.java    From yuzhouwan with Apache License 2.0 4 votes vote down vote up
public void addChildrenListener(String path) throws Exception {

        Stat existStat = curatorFramework.checkExists().forPath(path);
        if (existStat == null)
            curatorFramework
                    .create()
                    .creatingParentsIfNeeded()
                    .withMode(CreateMode.PERSISTENT)
                    .forPath(path);
        final PathChildrenCache pathChildrenCache = new PathChildrenCache(curatorFramework, path, false);
        pathChildrenCache.start(PathChildrenCache.StartMode.NORMAL);
        pathChildrenCache
                .getListenable()
                .addListener(
                        (CuratorFramework client, PathChildrenCacheEvent event) -> {
                            PathChildrenCacheEvent.Type type = event.getType();
                            LOG.info("Event type: {}", type);
                            switch (type) {
                                case CONNECTION_RECONNECTED:
                                    LOG.info("Reconnected...");
                                    break;
                                case CONNECTION_LOST:
                                    LOG.info("Connection lost...");
                                    pathChildrenCache.rebuild();
                                    LOG.info("Rebuild pathChildrenCache...");
                                    break;
                                case CONNECTION_SUSPENDED:
                                    LOG.info("Connection suspended...");
                                    break;
                                case CHILD_ADDED:
                                    LOG.info("Add new child: {}", event.getData().getPath());
                                    break;
                                case CHILD_UPDATED:
                                    LOG.info("Updated child: {}", event.getData().getPath());
                                    break;
                                case CHILD_REMOVED:
                                    LOG.info("Removed child: {}", event.getData().getPath());
                                    break;
                                default:
                                    LOG.error(String.format("Something was not excepted: %s", type));
                                    break;
                            }
                        }
                );
    }
 
Example 17
Source File: AlarmEPLManager.java    From nightwatch with GNU Lesser General Public License v3.0 4 votes vote down vote up
public AlarmEPLManager(Map conf, CountDownLatch latch) {

        this.conf = conf;
        this.latch = latch;
        this.configuration = new Configuration();
        this.addMethods(configuration);
        this.addDbs();


        this.epService = EPServiceProviderManager.getDefaultProvider(configuration);
        this.admin = epService.getEPAdministrator();

        this.addSchemas();
        this.addEpls();
        this.start();
        curator = CuratorFrameworkFactory
                .newClient(com.jzsec.rtc.config.Configuration.getConfig().getString("rtc.zk.hosts"),
                        Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_SESSION_TIMEOUT)),
                        Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_CONNECTION_TIMEOUT)),
                        new RetryNTimes(
                                Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_TIMES)),
                                Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL))
                        ));
        curator.start();
        createPath();
        ExecutorService pool = Executors.newFixedThreadPool(2);
        final PathChildrenCache pathChildrenCache = new PathChildrenCache(curator, "/alarm", true);
        try {
            pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
            pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
                public void childEvent(CuratorFramework framework,
                                       PathChildrenCacheEvent event) throws Exception {
                    List<ChildData> childDataList = pathChildrenCache.getCurrentData();
                    if(event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED || event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
                        if (childDataList != null && childDataList.size() > 0) {
                            // update and reload single rule  by businessScope in future
                            List<Map<Object, Object>> zkDataList = new ArrayList<Map<Object, Object>>();
                            for (ChildData childData : childDataList) {
                                LOG.info("==" + childData.getPath() + " changed," + new String(childData.getData(), "UTF-8"));
                                String data = new String(childData.getData(), "UTF-8");
                                if(!StringUtils.isEmpty(data)) {
                                    System.out.println("==" + childData.getPath() + " changed," + new String(childData.getData(), "UTF-8"));
                                    Map<Object, Object> zkData = (Map<Object, Object>) JSONValue.parse(data);
                                    zkDataList.add(zkData);
                                }

                            }
                            if(zkDataList.size() > 0) refresh(zkDataList);
                        }
                    }
                }
            }, pool);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 18
Source File: EPLManager.java    From nightwatch with GNU Lesser General Public License v3.0 4 votes vote down vote up
public EPLManager(Map conf, OutputCollector collector, CountDownLatch latch) {

        if(collector != null) this.collector = collector;
        this.conf = conf;
        this.latch = latch;
        this.configuration = new com.espertech.esper.client.Configuration();
        this.configuration.getEngineDefaults().getThreading().setInsertIntoDispatchPreserveOrder(false);
        this.addMethods(configuration);
        this.addDbs();


        this.epService = EPServiceProviderManager.getDefaultProvider(configuration);
        this.admin = epService.getEPAdministrator();
        this.addSchemas(null);
        this.addEpls();
        this.start();
        curator = CuratorFrameworkFactory
                .newClient(com.jzsec.rtc.config.Configuration.getConfig().getString("rtc.zk.hosts"),
                        Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_SESSION_TIMEOUT)),
                        Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_CONNECTION_TIMEOUT)),
                        new RetryNTimes(
                                Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_TIMES)),
                                Utils.getInt(conf.get(Config.STORM_ZOOKEEPER_RETRY_INTERVAL))
                        ));
        curator.start();
        createPath();
        ExecutorService pool = Executors.newFixedThreadPool(2);
        final PathChildrenCache pathChildrenCache = new PathChildrenCache(curator, "/risk", true);
        try {
            pathChildrenCache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
            pathChildrenCache.getListenable().addListener(new PathChildrenCacheListener() {
                public void childEvent(CuratorFramework framework,
                                       PathChildrenCacheEvent event) throws Exception {
                    List<ChildData> childDataList = pathChildrenCache.getCurrentData();
                    if(event.getType() == PathChildrenCacheEvent.Type.CHILD_ADDED || event.getType() == PathChildrenCacheEvent.Type.CHILD_UPDATED) {
                        if (childDataList != null && childDataList.size() > 0) {
                            // update and reload single rule  by businessScope in future
                            List<Map<Object, Object>> zkDataList = new ArrayList<Map<Object, Object>>();
                            for (ChildData childData : childDataList) {

                                LOG.info("==" + childData.getPath() + " changed," + new String(childData.getData(), "UTF-8"));

                                String data = new String(childData.getData(), "UTF-8");
                                if(!StringUtils.isEmpty(data)) {

                                    System.out.println("==" + childData.getPath() + " changed," + new String(childData.getData(), "UTF-8"));

                                    Map<Object, Object> zkData = (Map<Object, Object>) JSONValue.parse(data);
                                    if(!zkData.containsKey("type")) {
                                        String childPath = childData.getPath();
                                        zkData.put("type", childPath.substring(childPath.lastIndexOf("/") + 1));
                                    }
                                    zkDataList.add(zkData);
                                }

                            }
                            if(zkDataList.size() > 0) refresh(zkDataList);
                        }
                    }
                }
            }, pool);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
 
Example 19
Source File: CuratorClientService.java    From knox with Apache License 2.0 4 votes vote down vote up
@Override
public void addChildEntryListener(String path, ChildEntryListener listener) throws Exception {
    PathChildrenCache childCache = new PathChildrenCache(delegate, path, false);
    childCache.getListenable().addListener(new ChildEntryListenerAdapter(this, listener));
    childCache.start();
}
 
Example 20
Source File: ZKMasterController.java    From twister2 with Apache License 2.0 4 votes vote down vote up
/**
 * initialize JM when it is coming from failure
 * @throws Exception
 */
private void initRestarting() throws Exception {
  LOG.info("Job Master restarting .... ");

  // build the cache
  // we will not get events for the past worker joins/fails
  ephemChildrenCache = new PathChildrenCache(client, ephemDir, true);
  addEphemChildrenCacheListener(ephemChildrenCache);
  ephemChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

  List<ChildData> joinedWorkerZnodes = ephemChildrenCache.getCurrentData();
  LOG.info("Initially existing workers: " + joinedWorkerZnodes.size());

  // We listen for status updates for persistent path
  persChildrenCache = new PathChildrenCache(client, persDir, true);
  addPersChildrenCacheListener(persChildrenCache);
  persChildrenCache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

  // get all joined workers and provide them to workerMonitor
  List<WorkerWithState> joinedWorkers = new LinkedList<>();
  for (ChildData child : joinedWorkerZnodes) {
    String fullPath = child.getPath();
    int workerID = ZKUtils.getWorkerIDFromEphemPath(fullPath);

    WorkerWithState workerWithState = getWorkerWithState(workerID);
    if (workerWithState != null) {
      joinedWorkers.add(workerWithState);
    } else {
      LOG.severe("worker[" + fullPath + "] added, but its data can not be retrieved.");
    }
  }

  // publish jm restarted event
  jmRestarted();

  // if all workers joined and allJoined event has not been published, publish it
  boolean allJoined = workerMonitor.addJoinedWorkers(joinedWorkers);
  if (allJoined && !allJoinedPublished()) {
    LOG.info("Publishing AllJoined event when restarting, since it is not previously published.");
    allJoined();
  }
}