use of org.apache.curator.retry.RetryNTimes in project yyl_example by Relucent.
the class CuratorDistributedLockTest method main.
/**
* 下面的程序会启动几个线程去争夺锁,拿到锁的线程会占用5秒
*/
public static void main(String[] args) throws InterruptedException {
// 1.Connect to zk
CuratorFramework client = CuratorFrameworkFactory.newClient(ZK_ADDRESS, new RetryNTimes(10, 5000));
client.start();
System.out.println(client.getState());
System.out.println("zk client start successfully!");
InterProcessMutex lock = new InterProcessMutex(client, ZK_LOCK_PATH);
int threadCount = 3;
CountDownLatch latch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
doWithLock(client, lock);
// 线程结束时计数器减1
latch.countDown();
}, "Thread-" + i).start();
}
try {
// 等待所有子线程执行完
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 关闭客户端
client.close();
}
use of org.apache.curator.retry.RetryNTimes in project apex-malhar by apache.
the class ZKAssistedDiscovery method setup.
@Override
public void setup(com.datatorrent.api.Context context) {
ObjectMapper om = new ObjectMapper();
instanceSerializerFactory = new InstanceSerializerFactory(om.reader(), om.writer());
curatorFramework = CuratorFrameworkFactory.builder().connectionTimeoutMs(connectionTimeoutMillis).retryPolicy(new RetryNTimes(connectionRetryCount, conntectionRetrySleepMillis)).connectString(connectionString).build();
curatorFramework.start();
discovery = getDiscovery(curatorFramework);
try {
discovery.start();
} catch (Exception ex) {
Throwables.propagate(ex);
}
}
use of org.apache.curator.retry.RetryNTimes in project paascloud-master by paascloud.
the class IncrementIdGenerator method nextId.
/**
* Next id long.
*
* @return the long
*/
@Override
public Long nextId() {
String app = this.registerDto.getApp();
String host = this.registerDto.getHost();
CoordinatorRegistryCenter regCenter = this.registerDto.getCoordinatorRegistryCenter();
String path = GlobalConstant.ZK_REGISTRY_ID_ROOT_PATH + GlobalConstant.Symbol.SLASH + app + GlobalConstant.Symbol.SLASH + host;
if (regCenter.isExisted(path)) {
// 如果已经有该节点,表示已经为当前的host上部署的该app分配的编号(应对某个服务重启之后编号不变的问题),直接获取该id,而无需生成
return Long.valueOf(regCenter.getDirectly(GlobalConstant.ZK_REGISTRY_ID_ROOT_PATH + GlobalConstant.Symbol.SLASH + app + GlobalConstant.Symbol.SLASH + host));
} else {
// 节点不存在,那么需要生成id,利用zk节点的版本号每写一次就自增的机制来实现
regCenter.increment(GlobalConstant.ZK_REGISTRY_SEQ, new RetryNTimes(2000, 3));
// 生成id
Integer id = regCenter.getAtomicValue(GlobalConstant.ZK_REGISTRY_SEQ, new RetryNTimes(2000, 3)).postValue();
// 将数据写入节点
regCenter.persist(path);
regCenter.persist(path, String.valueOf(id));
return Long.valueOf(id);
}
}
use of org.apache.curator.retry.RetryNTimes in project tutorials by eugenp.
the class BaseTest method newClient.
protected CuratorFramework newClient() {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
return CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
}
use of org.apache.curator.retry.RetryNTimes in project tutorials by eugenp.
the class ConnectionManagementManualTest method givenRunningZookeeper_whenOpenConnection_thenClientIsOpened.
@Test
public void givenRunningZookeeper_whenOpenConnection_thenClientIsOpened() throws Exception {
int sleepMsBetweenRetries = 100;
int maxRetries = 3;
RetryPolicy retryPolicy = new RetryNTimes(maxRetries, sleepMsBetweenRetries);
try (CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy)) {
client.start();
assertThat(client.checkExists().forPath("/")).isNotNull();
}
}
Aggregations