Search in sources :

Example 56 with RetryNTimes

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();
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) CountDownLatch(java.util.concurrent.CountDownLatch) InterProcessMutex(org.apache.curator.framework.recipes.locks.InterProcessMutex)

Example 57 with RetryNTimes

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);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) IOException(java.io.IOException)

Example 58 with RetryNTimes

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);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CoordinatorRegistryCenter(com.paascloud.core.registry.base.CoordinatorRegistryCenter)

Example 59 with RetryNTimes

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);
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) RetryPolicy(org.apache.curator.RetryPolicy)

Example 60 with RetryNTimes

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();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) AsyncCuratorFramework(org.apache.curator.x.async.AsyncCuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.junit.Test)

Aggregations

RetryNTimes (org.apache.curator.retry.RetryNTimes)68 CuratorFramework (org.apache.curator.framework.CuratorFramework)51 Test (org.junit.Test)20 RetryPolicy (org.apache.curator.RetryPolicy)12 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)12 Test (org.testng.annotations.Test)11 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)10 CountDownLatch (java.util.concurrent.CountDownLatch)9 ZooKeeperGroup (io.fabric8.groups.internal.ZooKeeperGroup)8 IOException (java.io.IOException)6 ZooKeeperGroup (org.apache.camel.component.zookeepermaster.group.internal.ZooKeeperGroup)6 Before (org.junit.Before)6 ArrayList (java.util.ArrayList)5 TestingServer (org.apache.curator.test.TestingServer)5 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 ConnectionState (org.apache.curator.framework.state.ConnectionState)4 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)3 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)3 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)3