【问题标题】:zookeeper curator - not receiving child nodes creation event notifications with PathCacheListenerzookeeper curator - 未使用 PathCacheListener 接收子节点创建事件通知
【发布时间】:2014-04-27 16:57:50
【问题描述】:

我有一个工人类,它将 znode 注册为 /workers/worker-1,我想从 /assign/worker-1/task1 接收 master 为这个工人分配的任务。我已经注册了一个主监听器,并且还在分配路径 (/assign/worker-1) 上启动了一个 PathChildrenCache,但由于某种原因,我没有收到有关工作人员的任何事件。这是代码的样子

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 的通知。任何想法我可能做错了什么。

谢谢

【问题讨论】:

    标签: apache-zookeeper apache-curator


    【解决方案1】:

    不要介意上面的代码是正确的。我在没有正确的实例 ID 时遇到问题,因此它正在查看不正确的路径。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-31
      • 1970-01-01
      • 2018-03-23
      • 2017-02-05
      • 2016-06-16
      相关资源
      最近更新 更多