com.twitter.finagle.common.zookeeper.ZooKeeperClient.get()方法的使用及代码示例

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

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

ZooKeeperClient.get介绍

[英]Returns the current active ZK connection or establishes a new one if none has yet been established or a previous connection was disconnected or had its session time out. This method will attempt to re-use sessions when possible. Equivalent to:

get(Amount.of(0L, ...)

.
[中]返回当前活动的ZK连接,或者如果尚未建立任何连接,或者之前的连接已断开或会话超时,则建立新的ZK连接。如果可能,此方法将尝试重新使用会话。相当于:

get(Amount.of(0L, ...)

代码示例

代码示例来源:origin: com.twitter/finagle-serversets

private void watchGroup()
  throws ZooKeeperConnectionException, InterruptedException, KeeperException {
 if (stopped) {
  return;
 }
 List<String> children = zkClient.get().getChildren(path, groupWatcher);
 setMembers(children.stream()
  .filter(nodeNameFilter)
  .collect(Collectors.toList())
 );
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

/**
 * Returns the current list of group member ids by querying ZooKeeper synchronously.
 *
 * @return the ids of all the present members of this group
 * @throws ZooKeeperConnectionException if there was a problem connecting to ZooKeeper
 * @throws KeeperException if there was a problem reading this group's member ids
 * @throws InterruptedException if this thread is interrupted listing the group members
 */
public Iterable<String> getMemberIds()
  throws ZooKeeperConnectionException, KeeperException, InterruptedException {
 return zkClient.get().getChildren(path, false)
   .stream()
   .filter(nodeNameFilter)
   .collect(Collectors.toList());
}

代码示例来源:origin: com.twitter/finagle-serversets

/**
 * Returns the current list of group member ids by querying ZooKeeper synchronously.
 *
 * @return the ids of all the present members of this group
 * @throws ZooKeeperConnectionException if there was a problem connecting to ZooKeeper
 * @throws KeeperException if there was a problem reading this group's member ids
 * @throws InterruptedException if this thread is interrupted listing the group members
 */
public Iterable<String> getMemberIds()
  throws ZooKeeperConnectionException, KeeperException, InterruptedException {
 return zkClient.get().getChildren(path, false)
   .stream()
   .filter(nodeNameFilter)
   .collect(Collectors.toList());
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

private void watchGroup()
  throws ZooKeeperConnectionException, InterruptedException, KeeperException {
 if (stopped) {
  return;
 }
 List<String> children = zkClient.get().getChildren(path, groupWatcher);
 setMembers(children.stream()
  .filter(nodeNameFilter)
  .collect(Collectors.toList())
 );
}

代码示例来源:origin: com.twitter/finagle-serversets

private static void ensurePathInternal(ZooKeeperClient zkClient, List<ACL> acl, String path)
  throws ZooKeeperConnectionException, InterruptedException, KeeperException {
 if (zkClient.get().exists(path, false) == null) {
  // The current path does not exist; so back up a level and ensure the parent path exists
  // unless we're already a root-level path.
  int lastPathIndex = path.lastIndexOf('/');
  if (lastPathIndex > 0) {
   ensurePathInternal(zkClient, acl, path.substring(0, lastPathIndex));
  }
  // We've ensured our parent path (if any) exists so we can proceed to create our path.
  try {
   zkClient.get().create(path, null, acl, CreateMode.PERSISTENT);
  } catch (KeeperException.NodeExistsException e) {
   // This ensures we don't die if a race condition was met between checking existence and
   // trying to create the node.
   LOG.info("Node existed when trying to ensure path " + path + ", somebody beat us to it?");
  }
 }
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

/**
 * Gets the data for one of this groups members by querying ZooKeeper synchronously.
 *
 * @param memberId the id of the member whose data to retrieve
 * @return the data associated with the {@code memberId}
 * @throws ZooKeeperConnectionException if there was a problem connecting to ZooKeeper
 * @throws KeeperException if there was a problem reading this member's data
 * @throws InterruptedException if this thread is interrupted retrieving the member data
 */
public byte[] getMemberData(String memberId)
  throws ZooKeeperConnectionException, KeeperException, InterruptedException {
 return zkClient.get().getData(getMemberPath(memberId), false, null);
}

代码示例来源:origin: com.twitter/finagle-serversets

/**
 * Gets the data for one of this groups members by querying ZooKeeper synchronously.
 *
 * @param memberId the id of the member whose data to retrieve
 * @return the data associated with the {@code memberId}
 * @throws ZooKeeperConnectionException if there was a problem connecting to ZooKeeper
 * @throws KeeperException if there was a problem reading this member's data
 * @throws InterruptedException if this thread is interrupted retrieving the member data
 */
public byte[] getMemberData(String memberId)
  throws ZooKeeperConnectionException, KeeperException, InterruptedException {
 return zkClient.get().getData(getMemberPath(memberId), false, null);
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

private static void ensurePathInternal(ZooKeeperClient zkClient, List<ACL> acl, String path)
  throws ZooKeeperConnectionException, InterruptedException, KeeperException {
 if (zkClient.get().exists(path, false) == null) {
  // The current path does not exist; so back up a level and ensure the parent path exists
  // unless we're already a root-level path.
  int lastPathIndex = path.lastIndexOf('/');
  if (lastPathIndex > 0) {
   ensurePathInternal(zkClient, acl, path.substring(0, lastPathIndex));
  }
  // We've ensured our parent path (if any) exists so we can proceed to create our path.
  try {
   zkClient.get().create(path, null, acl, CreateMode.PERSISTENT);
  } catch (KeeperException.NodeExistsException e) {
   // This ensures we don't die if a race condition was met between checking existence and
   // trying to create the node.
   LOG.info("Node existed when trying to ensure path " + path + ", somebody beat us to it?");
  }
 }
}

代码示例来源:origin: com.twitter/finagle-serversets

/**
 * Returns the current active ZK connection or establishes a new one if none has yet been
 * established or a previous connection was disconnected or had its session time out.  This method
 * will attempt to re-use sessions when possible.  Equivalent to:
 * <pre>get(Amount.of(0L, ...)</pre>.
 *
 * @return a connected ZooKeeper client
 * @throws ZooKeeperConnectionException if there was a problem connecting to the ZK cluster
 * @throws InterruptedException if interrupted while waiting for a connection to be established
 */
public synchronized ZooKeeper get() throws ZooKeeperConnectionException, InterruptedException {
 try {
  return get(WAIT_FOREVER);
 } catch (TimeoutException e) {
  InterruptedException interruptedException =
    new InterruptedException("Got an unexpected TimeoutException for 0 wait");
  interruptedException.initCause(e);
  throw interruptedException;
 }
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

/**
 * Returns the current active ZK connection or establishes a new one if none has yet been
 * established or a previous connection was disconnected or had its session time out.  This method
 * will attempt to re-use sessions when possible.  Equivalent to:
 * <pre>get(Amount.of(0L, ...)</pre>.
 *
 * @return a connected ZooKeeper client
 * @throws ZooKeeperConnectionException if there was a problem connecting to the ZK cluster
 * @throws InterruptedException if interrupted while waiting for a connection to be established
 */
public synchronized ZooKeeper get() throws ZooKeeperConnectionException, InterruptedException {
 try {
  return get(WAIT_FOREVER);
 } catch (TimeoutException e) {
  InterruptedException interruptedException =
    new InterruptedException("Got an unexpected TimeoutException for 0 wait");
  interruptedException.initCause(e);
  throw interruptedException;
 }
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

@Override public Boolean get() throws JoinException {
  try {
   zkClient.get().delete(nodePath, ZooKeeperUtils.ANY_VERSION);
   return true;
  } catch (InterruptedException e) {
   Thread.currentThread().interrupt();
   throw new JoinException("Interrupted trying to cancel membership: " + nodePath, e);
  } catch (ZooKeeperConnectionException e) {
   LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
   return false;
  } catch (NoNodeException e) {
   LOG.info("Membership already cancelled, node at path: " + nodePath
        + " has been deleted");
   return true;
  } catch (KeeperException e) {
   if (zkClient.shouldRetry(e)) {
    LOG.log(Level.WARNING, "Temporary error cancelling membership: " + nodePath, e);
    return false;
   } else {
    throw new JoinException("Problem cancelling membership: " + nodePath, e);
   }
  }
 }
});

代码示例来源:origin: com.twitter/finagle-serversets

@Override public Boolean get() throws JoinException {
  try {
   zkClient.get().delete(nodePath, ZooKeeperUtils.ANY_VERSION);
   return true;
  } catch (InterruptedException e) {
   Thread.currentThread().interrupt();
   throw new JoinException("Interrupted trying to cancel membership: " + nodePath, e);
  } catch (ZooKeeperConnectionException e) {
   LOG.log(Level.WARNING, "Problem connecting to ZooKeeper, retrying", e);
   return false;
  } catch (NoNodeException e) {
   LOG.info("Membership already cancelled, node at path: " + nodePath
        + " has been deleted");
   return true;
  } catch (KeeperException e) {
   if (zkClient.shouldRetry(e)) {
    LOG.log(Level.WARNING, "Temporary error cancelling membership: " + nodePath, e);
    return false;
   } else {
    throw new JoinException("Problem cancelling membership: " + nodePath, e);
   }
  }
 }
});

代码示例来源:origin: com.twitter/finagle-serversets_2.11

? CreateMode.EPHEMERAL_SEQUENTIAL
  : CreateMode.EPHEMERAL;
nodePath = zkClient.get().create(
 path + "/" + nodeName, updatedMembershipData, acl, createMode);
memberId = Group.this.getMemberId(nodePath);
zkClient.get().exists(nodePath, new Watcher() {
 @Override public void process(WatchedEvent event) {
  if (event.getType() == EventType.NodeDeleted) {

代码示例来源:origin: com.twitter/finagle-serversets

? CreateMode.EPHEMERAL_SEQUENTIAL
  : CreateMode.EPHEMERAL;
nodePath = zkClient.get().create(
 path + "/" + nodeName, updatedMembershipData, acl, createMode);
memberId = Group.this.getMemberId(nodePath);
zkClient.get().exists(nodePath, new Watcher() {
 @Override public void process(WatchedEvent event) {
  if (event.getType() == EventType.NodeDeleted) {

代码示例来源:origin: com.twitter/finagle-serversets_2.11

@Override public ServiceInstance get() {
   try {
    byte[] data = zkClient.get().getData(nodePath, false, null);
    return ServerSets.deserializeServiceInstance(data, codec);
   } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new ServiceInstanceFetchException(
      "Interrupted updating service data for: " + nodePath, e);
   } catch (ZooKeeperConnectionException e) {
    LOG.log(Level.WARNING,
      "Temporary error trying to updating service data for: " + nodePath, e);
    return null;
   } catch (NoNodeException e) {
    invalidateNodePath(nodePath);
    throw new ServiceInstanceDeletedException(nodePath);
   } catch (KeeperException e) {
    if (zkClient.shouldRetry(e)) {
     LOG.log(Level.WARNING,
       "Temporary error trying to update service data for: " + nodePath, e);
     return null;
    } else {
     throw new ServiceInstanceFetchException(
       "Failed to update service data for: " + nodePath, e);
    }
   } catch (IOException e) {
    throw new ServiceInstanceFetchException(
      "Failed to deserialize the ServiceInstance data for: " + nodePath, e);
   }
  }
});

代码示例来源:origin: com.twitter/finagle-serversets

@Override public ServiceInstance get() {
   try {
    byte[] data = zkClient.get().getData(nodePath, false, null);
    return ServerSets.deserializeServiceInstance(data, codec);
   } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
    throw new ServiceInstanceFetchException(
      "Interrupted updating service data for: " + nodePath, e);
   } catch (ZooKeeperConnectionException e) {
    LOG.log(Level.WARNING,
      "Temporary error trying to updating service data for: " + nodePath, e);
    return null;
   } catch (NoNodeException e) {
    invalidateNodePath(nodePath);
    throw new ServiceInstanceDeletedException(nodePath);
   } catch (KeeperException e) {
    if (zkClient.shouldRetry(e)) {
     LOG.log(Level.WARNING,
       "Temporary error trying to update service data for: " + nodePath, e);
     return null;
    } else {
     throw new ServiceInstanceFetchException(
       "Failed to update service data for: " + nodePath, e);
    }
   } catch (IOException e) {
    throw new ServiceInstanceFetchException(
      "Failed to deserialize the ServiceInstance data for: " + nodePath, e);
   }
  }
});

代码示例来源:origin: com.twitter/finagle-serversets

@Override
public synchronized byte[] updateMemberData() throws UpdateException {
 byte[] updatedMembershipData = memberData.get();
 if (!ArrayUtils.isEquals(this.membershipData, updatedMembershipData)) {
  try {
   zkClient.get().setData(nodePath, updatedMembershipData, ZooKeeperUtils.ANY_VERSION);
   this.membershipData = updatedMembershipData;
  } catch (KeeperException e) {
   throw new UpdateException("Problem updating membership data.", e);
  } catch (InterruptedException e) {
   throw new UpdateException("Interrupted attempting to update membership data.", e);
  } catch (ZooKeeperConnectionException e) {
   throw new UpdateException(
     "Could not connect to the ZooKeeper cluster to update membership data.", e);
  }
 }
 return updatedMembershipData;
}

代码示例来源:origin: com.twitter/finagle-serversets_2.11

@Override
public synchronized byte[] updateMemberData() throws UpdateException {
 byte[] updatedMembershipData = memberData.get();
 if (!ArrayUtils.isEquals(this.membershipData, updatedMembershipData)) {
  try {
   zkClient.get().setData(nodePath, updatedMembershipData, ZooKeeperUtils.ANY_VERSION);
   this.membershipData = updatedMembershipData;
  } catch (KeeperException e) {
   throw new UpdateException("Problem updating membership data.", e);
  } catch (InterruptedException e) {
   throw new UpdateException("Interrupted attempting to update membership data.", e);
  } catch (ZooKeeperConnectionException e) {
   throw new UpdateException(
     "Could not connect to the ZooKeeper cluster to update membership data.", e);
  }
 }
 return updatedMembershipData;
}

相关文章