zookeeper curator - 未使用PathCacheListener接收子节点创建事件通知

时间:2014-04-27 16:57:51

标签: apache-zookeeper apache-curator

我有一个worker类,它将znode注册为/ workers / worker-1,我希望从/ assign / worker-1 / task1接收来自master的master的赋值。我已经注册了一个主侦听器,并且在赋值路径(/ assign / worker-1)上启动了一个PathChildrenCache但由于某种原因,我没有在worker上收到任何事件。这是代码的样子

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);
        }
    }
};

}

所以在此之后如果我要么在/ assign / worker-1下创建一个节点,我就不会得到关于worker的任何通知。任何想法我可能做错了。

由于

1 个答案:

答案 0 :(得分:1)

不要紧,上面的代码是正确的。我有一个问题是没有正确的实例ID,所以它正在观察错误的路径。