下面列出了怎么用org.apache.zookeeper.KeeperException.ConnectionLossException的API类实例代码及写法,或者点击链接到github查看源代码。
/**
* A private method used to re-establish a zookeeper session with a peer cluster.
* @param ke
*/
protected void reconnect(KeeperException ke) {
if (ke instanceof ConnectionLossException || ke instanceof SessionExpiredException
|| ke instanceof AuthFailedException) {
String clusterKey = ctx.getPeerConfig().getClusterKey();
LOG.warn("Lost the ZooKeeper connection for peer " + clusterKey, ke);
try {
reloadZkWatcher();
} catch (IOException io) {
LOG.warn("Creation of ZookeeperWatcher failed for peer " + clusterKey, io);
}
}
}
@Override
public void run(final boolean timeout) throws InterruptedException {
final RetryScheduler retryScheduler = retryIntervalPolicy.newScheduler();
while (isAlive()) {
final long sleep = retryScheduler.nextMillis();
boolean successfullyRegistered = false;
try {
successfullyRegistered = zooKeeperRegistrar.tryToRegister(client);
} catch (Exception e) {
if (e instanceof ConnectionLossException) {
log.warn("ZooKeeper connection lost, retrying registration in {} ms", sleep);
} else if (e instanceof HostNotFoundException) {
log.error("ZooKeeper deregistration of old hostname failed, retrying in {} ms: {}",
sleep, e);
} else {
log.error("ZooKeeper registration failed, retrying in {} ms", sleep, e);
}
}
// only exit the loop when zookeeper registration is successful. if registration does not
// succeed because another host is already registered, this will cause another registration
// attempt after sleeping. if zookeeper is cannot be connected to, this will also cause
// another attempt after sleeping.
if (successfullyRegistered) {
log.info("Successfully registered host in zookeeper");
zkRegistrationSignal.ifPresent(CountDownLatch::countDown);
return;
} else {
log.warn("registration not successful, sleeping for {} seconds",
TimeUnit.MILLISECONDS.toSeconds(sleep));
sleeper.sleep(sleep);
}
}
}
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof ConnectionLossException || cause instanceof NoNodeException || cause instanceof NodeExistsException;
}
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof ConnectionLossException || cause instanceof NoNodeException || cause instanceof NodeExistsException;
}
@Test
public void assertHandleExceptionWithConnectionLossException() {
RegExceptionHandler.handleException(new ConnectionLossException());
}
@Test
public void assertHandleExceptionWithCausedConnectionLossException() {
RegExceptionHandler.handleException(new RuntimeException(new ConnectionLossException()));
}
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof ConnectionLossException || cause instanceof NoNodeException
|| cause instanceof NodeExistsException;
}
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof ConnectionLossException || cause instanceof NoNodeException || cause instanceof NodeExistsException;
}
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof ConnectionLossException || cause instanceof NoNodeException || cause instanceof NodeExistsException;
}
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof ConnectionLossException || cause instanceof NoNodeException || cause instanceof NodeExistsException;
}
private static boolean isIgnoredException(final Throwable cause) {
return cause instanceof ConnectionLossException || cause instanceof NoNodeException || cause instanceof NodeExistsException;
}
@Test
public void assertHandleExceptionWithConnectionLossException() {
RegExceptionHandler.handleException(new ConnectionLossException());
}
@Test
public void assertHandleExceptionWithCausedConnectionLossException() {
RegExceptionHandler.handleException(new RuntimeException(new ConnectionLossException()));
}