Package com.alibaba.dubbo.remoting.zookeeper.zkclient

Source Code of com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient

package com.alibaba.dubbo.remoting.zookeeper.zkclient;

import java.util.List;

import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkStateListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.exception.ZkNoNodeException;
import org.I0Itec.zkclient.exception.ZkNodeExistsException;
import org.apache.zookeeper.Watcher.Event.KeeperState;

import com.alibaba.dubbo.common.URL;
import com.alibaba.dubbo.remoting.zookeeper.ChildListener;
import com.alibaba.dubbo.remoting.zookeeper.StateListener;
import com.alibaba.dubbo.remoting.zookeeper.support.AbstractZookeeperClient;

public class ZkclientZookeeperClient extends AbstractZookeeperClient<IZkChildListener> {

  private final ZkClient client;

  private volatile KeeperState state = KeeperState.SyncConnected;

  public ZkclientZookeeperClient(URL url) {
    super(url);
    client = new ZkClient(url.getBackupAddress());
    client.subscribeStateChanges(new IZkStateListener() {
      public void handleStateChanged(KeeperState state) throws Exception {
        ZkclientZookeeperClient.this.state = state;
        if (state == KeeperState.Disconnected) {
          stateChanged(StateListener.DISCONNECTED);
        } else if (state == KeeperState.SyncConnected) {
          stateChanged(StateListener.CONNECTED);
        }
      }
      public void handleNewSession() throws Exception {
        stateChanged(StateListener.RECONNECTED);
      }
    });
  }

  public void createPersistent(String path) {
    client.createPersistent(path, true);
  }

  public void createEphemeral(String path) {
    try {
      client.createEphemeral(path);
    } catch (ZkNodeExistsException e) {
    }
  }

  public void delete(String path) {
    client.delete(path);
  }

  public List<String> getChildren(String path) {
    try {
      return client.getChildren(path);
        } catch (ZkNoNodeException e) {
            return null;
        }
  }

  public boolean isConnected() {
    return state == KeeperState.SyncConnected;
  }

  public void close() {
    client.close();
  }

  public IZkChildListener createTargetChildListener(String path, final ChildListener listener) {
    return new IZkChildListener() {
      public void handleChildChange(String parentPath, List<String> currentChilds)
          throws Exception {
        listener.childChanged(parentPath, currentChilds);
      }
    };
  }

  public List<String> addTargetChildListener(String path, final IZkChildListener listener) {
    return client.subscribeChildChanges(path, listener);
  }

  public void removeTargetChildListener(String path, IZkChildListener listener) {
    client.unsubscribeChildChanges(path,  listener);
  }

}
TOP

Related Classes of com.alibaba.dubbo.remoting.zookeeper.zkclient.ZkclientZookeeperClient

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.