Package org.apache.curator.framework.recipes.cache

Examples of org.apache.curator.framework.recipes.cache.PathChildrenCache


          // this exception can be ignored; logging at trace level
          logger.trace("Exception while closing deployments cache", e);
        }
      }

      deployments = new PathChildrenCache(client, moduleDeploymentPath, true,
          ThreadUtils.newThreadFactory("DeploymentsPathChildrenCache"));
      deployments.getListenable().addListener(deploymentListener);
      deployments.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);

      logger.info("Container {} joined cluster", containerAttributes);
View Full Code Here


  }

  private void startTapListener(CuratorFramework client) {
    String tapPath = Paths.build(Paths.TAPS);
    Paths.ensurePath(client, tapPath);
    taps = new PathChildrenCache(client, tapPath, true,
        ThreadUtils.newThreadFactory("TapsPathChildrenCache"));
    taps.getListenable().addListener(tapListener);
    try {
      taps.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
    }
View Full Code Here

  /**
   * Close the {@link PathChildrenCache container cache} and null out
   * the {@link #cacheRef atmoic reference}.
   */
  private void closeCache() {
    PathChildrenCache cache = cacheRef.get();
    if (cache != null) {
      try {
        cache.close();
      }
      catch (Exception e) {
        // ignore exception on close
      }
      finally {
View Full Code Here

  private PathChildrenCache ensureCache() {
    if (cacheRef.get() == null) {
      synchronized (cacheRef) {
        if (cacheRef.get() == null) {
          CuratorFramework client = zkConnection.getClient();
          PathChildrenCache cache = new PathChildrenCache(client, Paths.CONTAINERS,
              true, ThreadUtils.newThreadFactory("ContainerCache"));
          cache.getListenable().addListener(new PathChildrenCacheListener() {

            @Override
            public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) {
              // shut down the cache if ZooKeeper connection goes away
              if (event.getType() == PathChildrenCacheEvent.Type.CONNECTION_SUSPENDED ||
                  event.getType() == PathChildrenCacheEvent.Type.CONNECTION_LOST) {
                closeCache();
              }
            }
          });
          try {
            Paths.ensurePath(client, Paths.CONTAINERS);
            cacheRef.set(cache);
            cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
          }
          catch (Exception e) {
            try {
              cache.close();
            }
            catch (Exception ce) {
              // ignore exception on close
            }
            finally {
              cacheRef.compareAndSet(cache, null);
            }
            throw ZooKeeperUtils.wrapThrowable(e);
          }
        }
      }
    }

    PathChildrenCache cache = cacheRef.get();
    Assert.state(cache != null, "Container cache not initialized " +
        "(likely as a result of a ZooKeeper connection error)");
    return cache;
  }
View Full Code Here

   *
   * @param path the path whose children to listen to
   * @param listener the children listener
   */
  public void addPathListener(final String path, PathChildrenCacheListener listener) {
    PathChildrenCache cache = mapChildren.get(path);
    if (cache == null) {
      mapChildren.put(path, cache = new PathChildrenCache(zooKeeperConnection.getClient(), path, true));
      try {
        cache.getListenable().addListener(listener);
        cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
      }
      catch (Exception e) {
        throw ZooKeeperUtils.wrapThrowable(e);
      }
    }
    else {
      cache.getListenable().addListener(listener);
    }
  }
View Full Code Here

   *
   * @param path the path whose children to listen to
   * @param listener the children listener
   */
  public void removePathListener(String path, PathChildrenCacheListener listener) {
    PathChildrenCache cache = mapChildren.get(path);
    if (cache != null) {
      cache.getListenable().removeListener(listener);
      if (cache.getListenable().size() == 0) {
        try {
          cache.close();
          mapChildren.remove(path);
        }
        catch (Exception e) {
          throw ZooKeeperUtils.wrapThrowable(e);
        }
View Full Code Here

   * @param client the {@link CuratorFramework} client
   * @param path   the path for the cache
   * @return Curator path children cache
   */
  private PathChildrenCache instantiatePathChildrenCache(CuratorFramework client, String path) {
    return new PathChildrenCache(client, path, true, false, executorService);
  }
View Full Code Here

    public void takeLeadership(CuratorFramework client) throws Exception {
      logger.info("Leader Admin {} is watching for stream/job deployment requests.", getId());

      cleanupDeployments(client);

      PathChildrenCache containers = null;
      PathChildrenCache streamDeployments = null;
      PathChildrenCache jobDeployments = null;
      PathChildrenCache moduleDeploymentRequests = null;
      StreamDeploymentListener streamDeploymentListener;
      JobDeploymentListener jobDeploymentListener;
      ContainerListener containerListener;

      try {
        StreamFactory streamFactory = new StreamFactory(streamDefinitionRepository, moduleRegistry,
            moduleOptionsMetadataResolver);

        JobFactory jobFactory = new JobFactory(jobDefinitionRepository, moduleRegistry,
            moduleOptionsMetadataResolver);

        String requestedModulesPath = Paths.build(Paths.MODULE_DEPLOYMENTS, Paths.REQUESTED);
        Paths.ensurePath(client, requestedModulesPath);
        String allocatedModulesPath = Paths.build(Paths.MODULE_DEPLOYMENTS, Paths.ALLOCATED);
        Paths.ensurePath(client, allocatedModulesPath);
        moduleDeploymentRequests = instantiatePathChildrenCache(client, requestedModulesPath);
        moduleDeploymentRequests.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);

        streamDeploymentListener = new StreamDeploymentListener(zkConnection,
            moduleDeploymentRequests,
            containerRepository,
            streamFactory,
            containerMatcher,
            stateCalculator);

        streamDeployments = instantiatePathChildrenCache(client, Paths.STREAM_DEPLOYMENTS);
        streamDeployments.getListenable().addListener(streamDeploymentListener);

        // using BUILD_INITIAL_CACHE so that all known streams are populated
        // in the cache before invoking recalculateStreamStates; same for
        // jobs below
        streamDeployments.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        streamDeploymentListener.recalculateStreamStates(client, streamDeployments);

        jobDeploymentListener = new JobDeploymentListener(zkConnection,
            moduleDeploymentRequests,
            containerRepository,
            jobFactory,
            containerMatcher,
            stateCalculator);

        jobDeployments = instantiatePathChildrenCache(client, Paths.JOB_DEPLOYMENTS);
        jobDeployments.getListenable().addListener(jobDeploymentListener);
        jobDeployments.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
        jobDeploymentListener.recalculateJobStates(client, jobDeployments);

        containerListener = new ContainerListener(zkConnection,
            containerRepository,
            streamFactory,
            jobFactory,
            streamDeployments,
            jobDeployments,
            moduleDeploymentRequests,
            containerMatcher,
            stateCalculator,
            executorService,
            quietPeriod);

        containers = instantiatePathChildrenCache(client, Paths.CONTAINERS);
        containers.getListenable().addListener(containerListener);
        containers.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);

        Thread.sleep(Long.MAX_VALUE);
      }
      catch (InterruptedException e) {
        logger.info("Leadership canceled due to thread interrupt");
        Thread.currentThread().interrupt();
      }
      finally {
        if (containers != null) {
          containers.close();
        }

        if (streamDeployments != null) {
          streamDeployments.close();
        }

        if (jobDeployments != null) {
          jobDeployments.close();
        }
        if (moduleDeploymentRequests != null) {
          moduleDeploymentRequests.close();
        }
      }
    }
View Full Code Here

    EmbeddedZooKeeper zkServer = new EmbeddedZooKeeper(5555);
    zkServer.start();
    ZooKeeperConnection zkConnection = new ZooKeeperConnection("localhost:5555");
    zkConnection.start();
    CuratorFramework client = zkConnection.getClient();
    PathChildrenCache cache = new PathChildrenCache(client, Paths.build(Paths.STREAMS), true);
    cache.getListenable().addListener(new ListenerOne());
    cache.getListenable().addListener(new ListenerTwo());
    cache.start();

    Paths.ensurePath(client, Paths.STREAMS);

    for (int i = 0; i < 100; i++) {
      client.create().creatingParentsIfNeeded().forPath(
          Paths.build(Paths.STREAM_DEPLOYMENTS, "foo" + i));
    }
    cache.close();
  }
View Full Code Here

        for (int i = 0; i < 5; i++) {
            System.out.format("%nCreating consumer #%d%n", i + 1);
            ConsumerConnector connector = Consumer.createJavaConsumerConnector(config);
            connector.createMessageStreams(Collections.singletonMap("mytopic", 1));
            if (i == 0) {
                PathChildrenCache cache = new PathChildrenCache(curator, "/consumers/foo/owners/mytopic", true);
                listener = new RebalanceListener(5);
                cache.getListenable().addListener(listener);
                cache.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT);
            }

            synchronized (listener) {
                System.out.println("******** Waiting for rebalance...");
                listener.wait();
View Full Code Here

TOP

Related Classes of org.apache.curator.framework.recipes.cache.PathChildrenCache

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.