Search in sources :

Example 1 with MonitorCommond

use of com.jeesuite.scheduler.monitor.MonitorCommond in project jeesuite-libs by vakinge.

the class ZkJobRegistry method regAndSubscribeNodeEvent.

/**
 * 订阅节点事件
 * @return
 */
private synchronized void regAndSubscribeNodeEvent() {
    if (nodeEventSubscribed)
        return;
    // 创建node节点
    zkClient.createEphemeral(nodeStateParentPath + "/" + JobContext.getContext().getNodeId());
    String path;
    // 订阅节点信息变化
    zkClient.subscribeChildChanges(nodeStateParentPath, new IZkChildListener() {

        @Override
        public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
            // 
            if (currentChilds == null || !currentChilds.contains(JobContext.getContext().getNodeId())) {
                zkClient.createEphemeral(nodeStateParentPath + "/" + JobContext.getContext().getNodeId());
                logger.info("Nodelist is empty~ node[{}] re-join task clusters", JobContext.getContext().getNodeId());
                return;
            }
            logger.info(">>nodes changed ,nodes:{}", currentChilds);
            // 分配节点
            rebalanceJobNode(currentChilds);
            // 刷新当前可用节点
            JobContext.getContext().refreshNodes(currentChilds);
        }
    });
    logger.info("subscribe nodes change event at path:{}", nodeStateParentPath);
    // 注册手动执行事件监听(来自于监控平台的)
    path = nodeStateParentPath + "/" + JobContext.getContext().getNodeId();
    zkClient.subscribeDataChanges(path, new IZkDataListener() {

        @Override
        public void handleDataDeleted(String dataPath) throws Exception {
        }

        @Override
        public void handleDataChange(String dataPath, Object data) throws Exception {
            MonitorCommond cmd = (MonitorCommond) data;
            if (cmd != null) {
                logger.info("收到commond:" + cmd.toString());
                execCommond(cmd);
            }
        }
    });
    logger.info("subscribe command event at path:{}", path);
    // 刷新节点列表
    List<String> activeNodes = zkClient.getChildren(nodeStateParentPath);
    JobContext.getContext().refreshNodes(activeNodes);
    logger.info("current activeNodes:{}", activeNodes);
    nodeEventSubscribed = true;
}
Also used : MonitorCommond(com.jeesuite.scheduler.monitor.MonitorCommond) IZkChildListener(org.I0Itec.zkclient.IZkChildListener) IZkDataListener(org.I0Itec.zkclient.IZkDataListener)

Aggregations

MonitorCommond (com.jeesuite.scheduler.monitor.MonitorCommond)1 IZkChildListener (org.I0Itec.zkclient.IZkChildListener)1 IZkDataListener (org.I0Itec.zkclient.IZkDataListener)1