com.indeed.util.zookeeper.ZooKeeperConnection类的使用及代码示例

x33g5p2x  于2022-02-05 转载在 其他  
字(6.1k)|赞(0)|评价(0)|浏览(82)

本文整理了Java中com.indeed.util.zookeeper.ZooKeeperConnection类的一些代码示例,展示了ZooKeeperConnection类的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ZooKeeperConnection类的具体详情如下:
包路径:com.indeed.util.zookeeper.ZooKeeperConnection
类名称:ZooKeeperConnection

ZooKeeperConnection介绍

暂无

代码示例

代码示例来源:origin: indeedeng/imhotep

@Override
  public void process(WatchedEvent watchedEvent) {
    if (!closed) {
      if (watchedEvent.getType() == Event.EventType.None && watchedEvent.getState() != Event.KeeperState.SyncConnected) {
        try {
          zkConnection.close();
        } catch (InterruptedException e) {
          log.error(e);
        }
        createNode();
      }
      zkConnection.register(this);
    }
  }
}

代码示例来源:origin: indeedeng/util

public static void updateOrCreate(ZooKeeperConnection zooKeeper, String path, byte[] value, CreateMode createMode)
    throws InterruptedException, KeeperException {
  boolean success = false;
  if (zooKeeper.exists(path, false) == null) {
    success = createFullPath(zooKeeper, path, value, createMode, true);
  }
  if (!success) zooKeeper.setData(path, value, -1);
}

代码示例来源:origin: indeedeng/util

public boolean createIfNotExists(String path, byte[] value, CreateMode createMode) throws KeeperException, InterruptedException {
  return createIfNotExists(this, path, value, createMode);
}

代码示例来源:origin: indeedeng/imhotep

private void createNode() {
  boolean created = false;
  while (!created) {
    try {
      try {
        zkConnection.connect();
        ZooKeeperConnection.createFullPath(zkConnection, rootPath, new byte[0], CreateMode.PERSISTENT, true);
        zkConnection.create(rootPath + "/"+nodeName, data, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        created = true;
      } catch (IOException e) {
        log.error(e);
        Thread.sleep(TIMEOUT);
      } catch (KeeperException e) {
        log.error(e);
        zkConnection.close();
        Thread.sleep(TIMEOUT);
      }
    } catch (InterruptedException e) {
      log.error(e);
    }
  }
}

代码示例来源:origin: indeedeng/imhotep

public ZkHostsReloader(final String zkNodes, final String zkPath, final boolean readHostsBeforeReturning) {
  super("ZkHostsReloader");
  zkConnection = new ZooKeeperConnection(zkNodes, TIMEOUT);
  try {
    zkConnection.connect();
  } catch (Exception e) {
    throw Throwables.propagate(e);
  }
  this.zkPath = trimEndingSlash(zkPath);
  closed = false;
  hosts = new ArrayList<Host>();
  if (readHostsBeforeReturning) {
    int retries = 0;
    while (true) {
      updateLastLoadCheck();
      if (load()) {
        loadComplete();
        break;
      }
      try {
        Thread.sleep(TIMEOUT);
      } catch (InterruptedException e) {
        log.error("interrupted", e);
      }
      if (++retries == 5) {
        throw new RuntimeException("unable to connect to zookeeper");
      }
    }
  }
}

代码示例来源:origin: indeedeng/imhotep

@Override
public boolean load() {
  if (!closed) {
    try {
      try {
        try {
          final List<Host> newHosts = readHostsFromZK();
          if (!newHosts.equals(hosts)) {
            hosts = newHosts;
          }
          return true;
        } catch (KeeperException e) {
          log.error("zookeeper exception", e);
          loadFailed();
          zkConnection.close();
          zkConnection.connect();
        }
      } catch (IOException e) {
        loadFailed();
        log.error("io exception", e);
      }
    } catch (InterruptedException e) {
      log.error("interrupted", e);
      loadFailed();
    }
  }
  return false;
}

代码示例来源:origin: indeedeng/util

public static boolean createIfNotExists(ZooKeeperConnection zooKeeper, String path, byte[] value, CreateMode createMode)
    throws InterruptedException, KeeperException {
  if (zooKeeper.exists(path, false) == null) {
    try {
      zooKeeper.create(path, value, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
    } catch (KeeperException e) {
      if (e.code() != KeeperException.Code.NODEEXISTS) throw e;
      return false;
    }
    return true;
  }
  return false;
}

代码示例来源:origin: indeedeng/util

public static boolean createFullPath(ZooKeeperConnection zooKeeperConnection, String path, byte[] value, CreateMode createMode, boolean ignoreIfExists)
    throws InterruptedException, KeeperException {
  final byte[] empty = new byte[0];
  final String[] nodes = path.split("/");
  final StringBuilder pathBuilder = new StringBuilder();
  for (int i = 1; i < nodes.length-1; i++) {
    pathBuilder.append("/").append(nodes[i]);
    createIfNotExists(zooKeeperConnection, pathBuilder.toString(), empty, CreateMode.PERSISTENT);
  }
  pathBuilder.append("/").append(nodes[nodes.length-1]);
  if (ignoreIfExists) {
    return createIfNotExists(zooKeeperConnection, path, value, createMode);
  }
  zooKeeperConnection.create(pathBuilder.toString(), value, ZooDefs.Ids.OPEN_ACL_UNSAFE, createMode);
  return true;
}

代码示例来源:origin: indeedeng/imhotep

public ServiceZooKeeperWrapper(final String zkNodes, final String hostname, final int port,
                final String rootPath) {
  zkConnection = new ZooKeeperConnection(zkNodes, TIMEOUT);
  this.rootPath = rootPath;
  nodeName = UUID.randomUUID().toString();
  data = (hostname+":"+port).getBytes(Charsets.UTF_8);
  closed = false;
  createNode();
  zkConnection.register(new MyWatcher());
}

代码示例来源:origin: indeedeng/util

public static void createFullPath(ZooKeeperConnection zooKeeperConnection, String path, byte[] value, CreateMode createMode)
    throws InterruptedException, KeeperException {
  createFullPath(zooKeeperConnection, path, value, createMode, false);
}

代码示例来源:origin: indeedeng/imhotep

@Override
public void close() throws IOException {
  closed = true;
  try {
    zkConnection.close();
  } catch (InterruptedException e) {
    log.error(e);
  }
}

代码示例来源:origin: indeedeng/imhotep

private List<Host> readHostsFromZK() throws KeeperException, InterruptedException {
  final List<String> childNodes = zkConnection.getChildren(zkPath, false);
  final List<Host> hosts = new ArrayList<Host>(childNodes.size());
  for (final String childNode : childNodes) {
    final byte[] data = zkConnection.getData(zkPath + "/" + childNode, false, new Stat());
    final String hostString = new String(data, Charsets.UTF_8);
    final String[] splitHostString = hostString.split(":");
    hosts.add(new Host(splitHostString[0], Integer.parseInt(splitHostString[1])));
  }
  Collections.sort(hosts);
  return hosts;
}

代码示例来源:origin: indeedeng/imhotep

public boolean isAlive() {
  return zkConnection.getState().isAlive();
}

代码示例来源:origin: indeedeng/util

public void createFullPath(String path, byte[] value, CreateMode createMode) throws KeeperException, InterruptedException {
  createFullPath(this, path, value, createMode);
}

代码示例来源:origin: indeedeng/imhotep

@Override
  public void shutdown() {
    closed = true;
    try {
      zkConnection.close();
    } catch (InterruptedException e) {
      log.error(e);
    }
  }
}

代码示例来源:origin: indeedeng/util

public boolean createFullPath(String path, byte[] value, CreateMode createMode, boolean ignoreIfExists) throws KeeperException, InterruptedException {
  return createFullPath(this, path, value, createMode, ignoreIfExists);
}

相关文章