use of org.apache.curator.retry.RetryForever in project thingsboard by thingsboard.
the class ZkDiscoveryService method init.
@PostConstruct
public void init() {
log.info("Initializing...");
Assert.hasLength(zkUrl, MiscUtils.missingProperty("zk.url"));
Assert.notNull(zkRetryInterval, MiscUtils.missingProperty("zk.retry_interval_ms"));
Assert.notNull(zkConnectionTimeout, MiscUtils.missingProperty("zk.connection_timeout_ms"));
Assert.notNull(zkSessionTimeout, MiscUtils.missingProperty("zk.session_timeout_ms"));
log.info("Initializing discovery service using ZK connect string: {}", zkUrl);
zkNodesDir = zkDir + "/nodes";
try {
client = CuratorFrameworkFactory.newClient(zkUrl, zkSessionTimeout, zkConnectionTimeout, new RetryForever(zkRetryInterval));
client.start();
client.blockUntilConnected();
cache = new PathChildrenCache(client, zkNodesDir, true);
cache.getListenable().addListener(this);
cache.start();
} catch (Exception e) {
log.error("Failed to connect to ZK: {}", e.getMessage(), e);
CloseableUtils.closeQuietly(client);
throw new RuntimeException(e);
}
}
use of org.apache.curator.retry.RetryForever in project druid by alibaba.
the class ZookeeperNodeListener method init.
/**
* Init a PathChildrenCache to watch the given path.
*/
@Override
public void init() {
checkParameters();
super.init();
if (client == null) {
client = CuratorFrameworkFactory.builder().canBeReadOnly(true).connectionTimeoutMs(5000).connectString(zkConnectString).retryPolicy(new RetryForever(10000)).sessionTimeoutMs(30000).build();
client.start();
privateZkClient = true;
}
cache = new PathChildrenCache(client, path, true);
cache.getListenable().addListener(new PathChildrenCacheListener() {
@Override
public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
try {
LOG.info("Receive an event: " + event.getType());
lock.lock();
PathChildrenCacheEvent.Type eventType = event.getType();
switch(eventType) {
case CHILD_REMOVED:
updateSingleNode(event, NodeEventTypeEnum.DELETE);
break;
case CHILD_ADDED:
updateSingleNode(event, NodeEventTypeEnum.ADD);
break;
case CONNECTION_RECONNECTED:
refreshAllNodes();
break;
default:
// CHILD_UPDATED
// INITIALIZED
// CONNECTION_LOST
// CONNECTION_SUSPENDED
LOG.info("Received a PathChildrenCacheEvent, IGNORE it: " + event);
}
} finally {
lock.unlock();
LOG.info("Finish the processing of event: " + event.getType());
}
}
});
try {
// Use BUILD_INITIAL_CACHE to force build cache in the current Thread.
// We don't use POST_INITIALIZED_EVENT, so there's no INITIALIZED event.
cache.start(PathChildrenCache.StartMode.BUILD_INITIAL_CACHE);
} catch (Exception e) {
LOG.error("Can't start PathChildrenCache", e);
}
}
use of org.apache.curator.retry.RetryForever in project xian by happyyangyuan.
the class TestFrameworkEdges method testCreateContainersForBadConnect.
@Test
public void testCreateContainersForBadConnect() throws Exception {
final int serverPort = server.getPort();
server.close();
CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), 1000, 1000, new RetryForever(100));
try {
new Thread() {
@Override
public void run() {
try {
Thread.sleep(3000);
server = new TestingServer(serverPort, true);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
client.start();
client.createContainers("/this/does/not/exist");
Assert.assertNotNull(client.checkExists().forPath("/this/does/not/exist"));
} finally {
CloseableUtils.closeQuietly(client);
}
}
use of org.apache.curator.retry.RetryForever in project xian by happyyangyuan.
the class TestRetryLoop method testRetryForever.
@Test
public void testRetryForever() throws Exception {
int retryIntervalMs = 1;
RetrySleeper sleeper = Mockito.mock(RetrySleeper.class);
RetryForever retryForever = new RetryForever(retryIntervalMs);
for (int i = 0; i < 10; i++) {
boolean allowed = retryForever.allowRetry(i, 0, sleeper);
Assert.assertTrue(allowed);
Mockito.verify(sleeper, times(i + 1)).sleepFor(retryIntervalMs, TimeUnit.MILLISECONDS);
}
}
Aggregations