Thanks Scott, yeah namespacing was the issue.

From: Scott Blum [mailto:[email protected]]
Sent: Friday, August 26, 2016 12:49 AM
To: [email protected]
Subject: Re: Not able to get TreeCache working

Ankit, can you double-check your namespacing?  It looks like your 
curatorFramework is itself namespaced, and then you're opening your TreeCache 
at a deeper path within that namespace.  I believe you're not getting events 
because you're not creating nodes in the subtree that TreeCache is actually 
watching.

On Wed, Aug 24, 2016 at 6:45 AM, Ankit Singhai 
<[email protected]<mailto:[email protected]>> wrote:
Hi All,
I am trying to get the notifications (events) by using TreeCache but apart from 
Initialized none other are coming on adding / deleting of node. Can someone 
please point where I am going wrong.

package com.gvc.caches;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.zookeeper.CreateMode;
import java.util.concurrent.Executors;

/**
* Created by Ankit on 8/24/2016.
*
* Not working .... events are not coming on add / delete / update of node.
*/
public class TreeNodeCacheMain {

    public static void main(String[] args) throws Exception {
        String path = "treeCacheTesting1";
        String pathPass = "/treeCacheTesting1";
        String connectionString = "127.0.0.1:2181<http://127.0.0.1:2181>";
        CuratorFramework curatorFramework = 
CuratorFrameworkFactory.builder().connectString(connectionString).namespace(path).retryPolicy(new
 ExponentialBackoffRetry(1000, 10)).build();
        curatorFramework.start();
        curatorFramework.blockUntilConnected();
        TreeCache treeCache = 
TreeCache.newBuilder(curatorFramework,pathPass).setCacheData(true).setExecutor(Executors.newSingleThreadExecutor()).build();
        treeCache.getListenable().addListener(new TreeCacheListenerImpl(), 
Executors.newSingleThreadExecutor());
        treeCache.start();
        System.out.println("Creating data");
        
curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath("/node1", new 
byte[1024]);
        
curatorFramework.create().withMode(CreateMode.EPHEMERAL).forPath("/node2", new 
byte[1024]);
        System.out.println("Changing data");
        curatorFramework.setData().forPath("/node1", new byte[512]);
        curatorFramework.setData().forPath("/node2", new byte[512]);
        System.out.println("Deleting data");
        curatorFramework.delete().forPath("/node1");
        curatorFramework.delete().forPath("/node2");
    }
}

class TreeCacheListenerImpl implements TreeCacheListener {
    @Override
    public void childEvent(CuratorFramework curatorFramework, TreeCacheEvent 
treeCacheEvent) throws Exception {
        System.out.println("Inside Listener " + treeCacheEvent);
    }
}

Thanks,
Ankit Singhai

Reply via email to