下面列出了org.apache.zookeeper.KeeperException#OperationTimeoutException ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
private IZookeeperClient initClient(final ClientFactory clientFactory, final RegistryCenterConfiguration config) {
IZookeeperClient result = null;
try {
// TODO There is a bug when the start time is very short, and I haven't found the reason yet
// result = clientFactory.start(config.getRetryIntervalMilliseconds() * config.getMaxRetries(), TimeUnit.MILLISECONDS);
result = clientFactory.start();
if (!result.blockUntilConnected(config.getRetryIntervalMilliseconds() * config.getMaxRetries(), TimeUnit.MILLISECONDS)) {
result.close();
throw new KeeperException.OperationTimeoutException();
}
result.useExecStrategy(StrategyType.SYNC_RETRY);
} catch (final KeeperException.OperationTimeoutException | IOException | InterruptedException ex) {
NativeZookeeperExceptionHandler.handleException(ex);
}
return result;
}
/**
* Try to create a ZooKeeper connection. Turns any exception encountered into a
* KeeperException.OperationTimeoutException so it can retried.
* @return The created ZooKeeper connection object
* @throws KeeperException if a ZooKeeper operation fails
*/
protected synchronized ZooKeeper checkZk() throws KeeperException {
if (this.zk == null) {
try {
this.zk = new ZooKeeper(quorumServers, sessionTimeout, watcher);
} catch (IOException ex) {
LOG.warn("Unable to create ZooKeeper Connection", ex);
throw new KeeperException.OperationTimeoutException();
}
}
return zk;
}
/**
* Init.
*/
@Override
public void init() {
log.debug("Elastic job: zookeeper registry center init, server lists is: {}.", zkConfig.getZkAddressList());
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
.connectString(zkConfig.getZkAddressList())
.retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds()));
if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
builder.sessionTimeoutMs(zkConfig.getSessionTimeoutMilliseconds());
}
if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
builder.connectionTimeoutMs(zkConfig.getConnectionTimeoutMilliseconds());
}
if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
builder.authorization("digest", zkConfig.getDigest().getBytes(Charsets.UTF_8))
.aclProvider(new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
@Override
public List<ACL> getAclForPath(final String path) {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
});
}
client = builder.build();
client.start();
try {
if (!client.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS)) {
client.close();
throw new KeeperException.OperationTimeoutException();
}
//CHECKSTYLE:OFF
} catch (final Exception ex) {
//CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
}
}
@Override
public void init() {
log.debug("Elastic job: zookeeper registry center init, server lists is: {}.", zkConfig.getServerLists());
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
.connectString(zkConfig.getServerLists())
.retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds()))
.namespace(zkConfig.getNamespace());
if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
builder.sessionTimeoutMs(zkConfig.getSessionTimeoutMilliseconds());
}
if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
builder.connectionTimeoutMs(zkConfig.getConnectionTimeoutMilliseconds());
}
if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
builder.authorization("digest", zkConfig.getDigest().getBytes(Charsets.UTF_8))
.aclProvider(new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
@Override
public List<ACL> getAclForPath(final String path) {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
});
}
client = builder.build();
client.start();
try {
if (!client.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS)) {
client.close();
throw new KeeperException.OperationTimeoutException();
}
//CHECKSTYLE:OFF
} catch (final Exception ex) {
//CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
}
}
@Override
public void init() {
if (!stat.compareAndSet(false, true)) {
return;
}
String address = registryConfig.getExt(ConfigEnum.address.getName(), ConfigEnum.address.getValue());
logger.info("zookeeper registry center init, server lists is: {}.", address);
String namespace = registryConfig.getExt(ConfigEnum.namespace.getName(), ConfigEnum.namespace.getValue());
int baseSleepTimeMilliseconds = registryConfig.getExtInt(ConfigEnum.baseSleepTimeMilliseconds.getName(), ConfigEnum.baseSleepTimeMilliseconds.getIntValue());
int maxSleepTimeMilliseconds = registryConfig.getExtInt(ConfigEnum.maxSleepTimeMilliseconds.getName(), ConfigEnum.maxSleepTimeMilliseconds.getIntValue());
int maxRetries = registryConfig.getExtInt(ConfigEnum.maxRetries.getName(), ConfigEnum.maxRetries.getIntValue());
int sessionTimeoutMilliseconds = registryConfig.getExtInt(ConfigEnum.sessionTimeoutMilliseconds.getName(), ConfigEnum.sessionTimeoutMilliseconds.getIntValue());
int connectionTimeoutMilliseconds = registryConfig.getExtInt(ConfigEnum.connectionTimeoutMilliseconds.getName(), ConfigEnum.connectionTimeoutMilliseconds.getIntValue());
String digest = registryConfig.getExt(ConfigEnum.digest.getName(), ConfigEnum.digest.getValue());
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
.connectString(address)
.retryPolicy(new ExponentialBackoffRetry(baseSleepTimeMilliseconds, maxRetries, maxSleepTimeMilliseconds))
.namespace(namespace);
if (0 != sessionTimeoutMilliseconds) {
builder.sessionTimeoutMs(sessionTimeoutMilliseconds);
}
if (0 != connectionTimeoutMilliseconds) {
builder.connectionTimeoutMs(connectionTimeoutMilliseconds);
}
if (!Strings.isNullOrEmpty(digest)) {
builder.authorization("digest", digest.getBytes(Charsets.UTF_8))
.aclProvider(new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
@Override
public List<ACL> getAclForPath(final String path) {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
});
}
client = builder.build();
client.start();
try {
client.blockUntilConnected(maxSleepTimeMilliseconds * maxRetries, TimeUnit.MILLISECONDS);
if (!client.getZookeeperClient().isConnected()) {
client.close();
throw new KeeperException.OperationTimeoutException();
}
//CHECKSTYLE:OFF
} catch (final Exception ex) {
//CHECKSTYLE:ON
throw new EagleFrameException(ex);
}
}
@Override
public synchronized void init() {
if (client != null) {
// client已经初始化,直接重置返回
return;
}
logger.debug("init zookeeper registry, connect to servers : {}", zkConfig.getServerLists());
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
.connectString(zkConfig.getServerLists())
.retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(),
zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds()))
.namespace(zkConfig.getNamespace());
if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
builder.sessionTimeoutMs(zkConfig.getSessionTimeoutMilliseconds());
}
if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
builder.connectionTimeoutMs(zkConfig.getConnectionTimeoutMilliseconds());
}
if (zkConfig.getDigest() != null && !zkConfig.getDigest().isEmpty()) {
builder.authorization("digest", zkConfig.getDigest().getBytes(StandardCharsets.UTF_8))
.aclProvider(new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
@Override
public List<ACL> getAclForPath(final String path) {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
});
}
client = builder.build();
client.start();
try {
if (!client.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(),
TimeUnit.MILLISECONDS)) {
client.close();
throw new KeeperException.OperationTimeoutException();
}
} catch (final Exception ex) {
RegExceptionHandler.handleException(ex);
}
}
@Override
public void init() {
log.debug("Elastic job: zookeeper registry center init, server lists is: {}.", zkConfig.getServerLists());
CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
.connectString(zkConfig.getServerLists())
.retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds()))
.namespace(zkConfig.getNamespace());
if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
builder.sessionTimeoutMs(zkConfig.getSessionTimeoutMilliseconds());
}
if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
builder.connectionTimeoutMs(zkConfig.getConnectionTimeoutMilliseconds());
}
if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
builder.authorization("digest", zkConfig.getDigest().getBytes(Charsets.UTF_8))
.aclProvider(new ACLProvider() {
@Override
public List<ACL> getDefaultAcl() {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
@Override
public List<ACL> getAclForPath(final String path) {
return ZooDefs.Ids.CREATOR_ALL_ACL;
}
});
}
client = builder.build();
client.start();
try {
if (!client.blockUntilConnected(zkConfig.getMaxSleepTimeMilliseconds() * zkConfig.getMaxRetries(), TimeUnit.MILLISECONDS)) {
client.close();
throw new KeeperException.OperationTimeoutException();
}
//CHECKSTYLE:OFF
} catch (final Exception ex) {
//CHECKSTYLE:ON
RegExceptionHandler.handleException(ex);
}
}
/**
* Start until Timeout.
*
* @param waitingTime waiting time
* @param timeUnit time unit
* @return connected or not
* @throws IOException IO Exception
* @throws InterruptedException interrupted exception
* @throws KeeperException operation timeout exception
*/
public IZookeeperClient start(final int waitingTime, final TimeUnit timeUnit) throws IOException, InterruptedException, KeeperException {
prepareClient();
if (!client.start(waitingTime, timeUnit)) {
client.close();
throw new KeeperException.OperationTimeoutException();
}
return client;
}