Java Code Examples for org.apache.curator.framework.CuratorFramework#newWatcherRemoveCuratorFramework()

The following examples show how to use org.apache.curator.framework.CuratorFramework#newWatcherRemoveCuratorFramework() . These examples are extracted from open source projects. 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
@Test
public void testSameWatcherDifferentPaths() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();
        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                // NOP
            }
        };
        removerClient.checkExists().usingWatcher(watcher).forPath("/a/b/c");
        removerClient.checkExists().usingWatcher(watcher).forPath("/d/e/f");
        Assert.assertEquals(removerClient.getWatcherRemovalManager().getEntries().size(), 2);
        removerClient.removeWatchers();
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 2
@Test
public void testSameWatcherDifferentPaths1Triggered() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();
        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();
        final CountDownLatch latch = new CountDownLatch(1);
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                latch.countDown();
            }
        };
        removerClient.checkExists().usingWatcher(watcher).forPath("/a/b/c");
        removerClient.checkExists().usingWatcher(watcher).forPath("/d/e/f");
        removerClient.create().creatingParentsIfNeeded().forPath("/d/e/f");

        Timing timing = new Timing();
        Assert.assertTrue(timing.awaitLatch(latch));
        timing.sleepABit();

        removerClient.removeWatchers();
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 3
@Test
public void testSameWatcherDifferentKinds1Triggered() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();
        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();
        final CountDownLatch latch = new CountDownLatch(1);
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                latch.countDown();
            }
        };

        removerClient.create().creatingParentsIfNeeded().forPath("/a/b/c");
        removerClient.checkExists().usingWatcher(watcher).forPath("/a/b/c");
        removerClient.getData().usingWatcher(watcher).forPath("/a/b/c");
        removerClient.setData().forPath("/a/b/c", "new".getBytes());

        Timing timing = new Timing();
        Assert.assertTrue(timing.awaitLatch(latch));
        timing.sleepABit();

        removerClient.removeWatchers();
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 4
@Test
public void testSameWatcherDifferentKinds() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();
        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                // NOP
            }
        };

        removerClient.create().creatingParentsIfNeeded().forPath("/a/b/c");
        removerClient.checkExists().usingWatcher(watcher).forPath("/a/b/c");
        removerClient.getData().usingWatcher(watcher).forPath("/a/b/c");
        removerClient.removeWatchers();
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 5
@Test
public void testWithRetry() throws Exception
{
    server.stop();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();
        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();
        Watcher w = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                // NOP
            }
        };
        try
        {
            removerClient.checkExists().usingWatcher(w).forPath("/one/two/three");
            Assert.fail("Should have thrown ConnectionLossException");
        }
        catch ( KeeperException.ConnectionLossException expected )
        {
            // expected
        }
        Assert.assertEquals(removerClient.getWatcherRemovalManager().getEntries().size(), 0);
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 6
@Test
public void testMissingNode() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();
        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();
        Watcher w = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                // NOP
            }
        };
        try
        {
            removerClient.getData().usingWatcher(w).forPath("/one/two/three");
            Assert.fail("Should have thrown NoNodeException");
        }
        catch ( KeeperException.NoNodeException expected )
        {
            // expected
        }
        removerClient.removeWatchers();
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 7
@Test
public void testSameWatcher() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();
        client.create().forPath("/test");

        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();

        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                // NOP
            }
        };

        removerClient.getData().usingWatcher(watcher).forPath("/test");
        Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1);
        removerClient.getData().usingWatcher(watcher).forPath("/test");
        Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1);
        removerClient.removeWatchers();
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 8
@Test
public void testTriggered() throws Exception
{
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();

        WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();

        final CountDownLatch latch = new CountDownLatch(1);
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                if ( event.getType() == Event.EventType.NodeCreated )
                {
                    latch.countDown();
                }
            }
        };

        removerClient.checkExists().usingWatcher(watcher).forPath("/yo");
        Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1);
        removerClient.create().forPath("/yo");

        Assert.assertTrue(new Timing().awaitLatch(latch));

        Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 0);
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 9
private void internalTryBasic(CuratorFramework client) throws Exception
{
    WatcherRemoveCuratorFramework removerClient = client.newWatcherRemoveCuratorFramework();

    final CountDownLatch latch = new CountDownLatch(1);
    Watcher watcher = new Watcher()
    {
        @Override
        public void process(WatchedEvent event)
        {
            if ( event.getType() == Event.EventType.DataWatchRemoved )
            {
                latch.countDown();
            }
        }
    };
    removerClient.checkExists().usingWatcher(watcher).forPath("/hey");

    List<String> existWatches = WatchersDebug.getExistWatches(client.getZookeeperClient().getZooKeeper());
    Assert.assertEquals(existWatches.size(), 1);

    removerClient.removeWatchers();

    Assert.assertTrue(new Timing().awaitLatch(latch));

    existWatches = WatchersDebug.getExistWatches(client.getZookeeperClient().getZooKeeper());
    Assert.assertEquals(existWatches.size(), 0);
}
 
Example 10
Source Project: curator   File: SharedValue.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param client    the client
 * @param path      the shared path - i.e. where the shared value is stored
 * @param seedValue the initial value for the value if/f the path has not yet been created
 */
public SharedValue(CuratorFramework client, String path, byte[] seedValue)
{
    this.client = client.newWatcherRemoveCuratorFramework();
    this.path = PathUtils.validatePath(path);
    this.seedValue = Arrays.copyOf(seedValue, seedValue.length);
    this.watcher = new SharedValueCuratorWatcher();
    currentValue = new AtomicReference<VersionedValue<byte[]>>(new VersionedValue<byte[]>(UNINITIALIZED_VERSION, Arrays.copyOf(seedValue, seedValue.length)));
}
 
Example 11
Source Project: curator   File: PathChildrenCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param client           the client
 * @param path             path to watch
 * @param cacheData        if true, node contents are cached in addition to the stat
 * @param dataIsCompressed if true, data in the path is compressed
 * @param executorService  Closeable ExecutorService to use for the PathChildrenCache's background thread. This service should be single threaded, otherwise the cache may see inconsistent results.
 */
public PathChildrenCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, final CloseableExecutorService executorService)
{
    this.client = client.newWatcherRemoveCuratorFramework();
    this.path = PathUtils.validatePath(path);
    this.cacheData = cacheData;
    this.dataIsCompressed = dataIsCompressed;
    this.executorService = executorService;
    ensureContainers = new EnsureContainers(client, path);
}
 
Example 12
Source Project: curator   File: TreeCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param client           the client
 * @param path             path to watch
 * @param cacheData        if true, node contents are cached in addition to the stat
 * @param dataIsCompressed if true, data in the path is compressed
 * @param executorService  Closeable ExecutorService to use for the TreeCache's background thread
 * @param createParentNodes true to create parent nodes as containers
 * @param disableZkWatches true to disable Zookeeper watches
 * @param selector         the selector to use
 */
TreeCache(CuratorFramework client, String path, boolean cacheData, boolean dataIsCompressed, int maxDepth, final ExecutorService executorService, boolean createParentNodes, boolean disableZkWatches, TreeCacheSelector selector)
{
    this.createParentNodes = createParentNodes;
    this.selector = Preconditions.checkNotNull(selector, "selector cannot be null");
    this.root = new TreeNode(validatePath(path), null);
    Preconditions.checkNotNull(client, "client cannot be null");
    this.client = client.newWatcherRemoveCuratorFramework();
    this.cacheData = cacheData;
    this.dataIsCompressed = dataIsCompressed;
    this.maxDepth = maxDepth;
    this.disableZkWatches = disableZkWatches;
    this.executorService = Preconditions.checkNotNull(executorService, "executorService cannot be null");
}
 
Example 13
Source Project: curator   File: NodeCache.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * @param client curator client
 * @param path the full path to the node to cache
 * @param dataIsCompressed if true, data in the path is compressed
 */
public NodeCache(CuratorFramework client, String path, boolean dataIsCompressed)
{
    this.client = client.newWatcherRemoveCuratorFramework();
    this.path = PathUtils.validatePath(path);
    this.dataIsCompressed = dataIsCompressed;
}
 
Example 14
Source Project: curator   File: LockInternals.java    License: Apache License 2.0 5 votes vote down vote up
LockInternals(CuratorFramework client, LockInternalsDriver driver, String path, String lockName, int maxLeases)
{
    this.driver = driver;
    this.lockName = lockName;
    this.maxLeases = maxLeases;

    this.client = client.newWatcherRemoveCuratorFramework();
    this.basePath = PathUtils.validatePath(path);
    this.path = ZKPaths.makePath(path, lockName);
}
 
Example 15
Source Project: curator   File: EnsembleTracker.java    License: Apache License 2.0 4 votes vote down vote up
EnsembleTracker(CuratorFramework client, EnsembleProvider ensembleProvider)
{
    this.client = client.newWatcherRemoveCuratorFramework();
    this.ensembleProvider = ensembleProvider;
}
 
Example 16
@Test
public void testResetFromWatcher() throws Exception
{
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1));
    try
    {
        client.start();

        final WatcherRemovalFacade removerClient = (WatcherRemovalFacade)client.newWatcherRemoveCuratorFramework();

        final CountDownLatch createdLatch = new CountDownLatch(1);
        final CountDownLatch deletedLatch = new CountDownLatch(1);
        Watcher watcher = new Watcher()
        {
            @Override
            public void process(WatchedEvent event)
            {
                if ( event.getType() == Event.EventType.NodeCreated )
                {
                    try
                    {
                        removerClient.checkExists().usingWatcher(this).forPath("/yo");
                    }
                    catch ( Exception e )
                    {
                        e.printStackTrace();
                    }
                    createdLatch.countDown();
                }
                else if ( event.getType() == Event.EventType.NodeDeleted )
                {
                    deletedLatch.countDown();
                }
            }
        };

        removerClient.checkExists().usingWatcher(watcher).forPath("/yo");
        Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1);
        removerClient.create().forPath("/yo");

        Assert.assertTrue(timing.awaitLatch(createdLatch));
        Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 1);

        removerClient.delete().forPath("/yo");

        Assert.assertTrue(timing.awaitLatch(deletedLatch));

        Assert.assertEquals(removerClient.getRemovalManager().getEntries().size(), 0);
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
Example 17
Source Project: curator   File: ChildrenCache.java    License: Apache License 2.0 4 votes vote down vote up
ChildrenCache(CuratorFramework client, String path)
{
    this.client = client.newWatcherRemoveCuratorFramework();
    this.path = PathUtils.validatePath(path);
}
 
Example 18
/**
 * @param client the client
 * @param path path for the lock
 */
public InterProcessSemaphoreMutex(CuratorFramework client, String path)
{
    watcherRemoveClient = client.newWatcherRemoveCuratorFramework();
    this.semaphore = new InterProcessSemaphoreV2(watcherRemoveClient, path, 1);
}