zookeeper curator - not receiving child nodes creation event notifications with PathCacheListener

StackOverflow https://stackoverflow.com/questions/23326263

  •  10-07-2023
  •  | 
  •  

Question

I have a worker class which registers the znode as /workers/worker-1 and I want to receive assignments by master for this worker from /assign/worker-1/task1. I have registered a main listener and also started a PathChildrenCache on the assignments path (/assign/worker-1) but for some reason I receive no events on the worker. here is how the code looks like

public class Worker {

@Autowired
public Worker(CuratorFramework curatorFramework) {
    client = curatorFramework;

    //initialize assignments cache
    assignments = new PathChildrenCache(client, Constants.ZK_ASSIGNMENTS_PATH + "/" + instanceId, false);
}

/**
 * Starts the worker
 * @throws Exception 
 */
public void start() throws Exception{
    log.debug("starting worker " + instanceId);

    //create worker nodes
    client.create()
            .withMode(CreateMode.EPHEMERAL)
            .inBackground()
            .forPath(Constants.ZK_WORKERS_PATH + "/" + instanceId, new byte[0]);
    //create worker assignment nodes
    client.create()
            .withMode(CreateMode.PERSISTENT)
            .inBackground()
            .forPath(Constants.ZK_ASSIGNMENTS_PATH + "/" + instanceId, new byte[0]);

    //register listerner for assignments cache
    assignments.getListenable().addListener(assignmentsListener);
    assignments.start();

    //main worker listener
    client.getCuratorListenable().addListener(new CuratorListener(){
        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            log.debug("worker event => " + event);              
        }
    });
}

/**
 * Assignments cache listener
 */
PathChildrenCacheListener assignmentsListener = new PathChildrenCacheListener(){
    @Override
    public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
        switch(event.getType()){
            case CHILD_ADDED:
                log.debug("worker acknowleding assignment of " + event.getData().getPath());
                break;
            default:
                log.debug("worker received event " + event);
        }
    }
};

}

so after this if i either create a node under /assign/worker-1 I dont get any notification on worker. Any ideas what I could be doing wrong.

Thanks

Was it helpful?

Solution

never mind the code above is correct. I had a problem in not having the correct instance id so it was watching the incorrect path.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top