org.apache.curator.retry.RetryNTimes Java Examples

The following examples show how to use org.apache.curator.retry.RetryNTimes. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: DefaultZkConfig.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
@Override
public CuratorFramework create(String address) throws InterruptedException {

	Builder builder = CuratorFrameworkFactory.builder();
	builder.connectionTimeoutMs(getZkConnectionTimeoutMillis());
	builder.connectString(address);
	builder.maxCloseWaitMs(getZkCloseWaitMillis());
	builder.namespace(getZkNamespace());
	builder.retryPolicy(new RetryNTimes(getZkRetries(), getSleepMsBetweenRetries()));
	builder.sessionTimeoutMs(getZkSessionTimeoutMillis());
	builder.threadFactory(XpipeThreadFactory.create("Xpipe-ZK-" + address, true));

	logger.info("[create]{}, {}", Codec.DEFAULT.encode(this), address);
	CuratorFramework curatorFramework = builder.build();
	curatorFramework.start();
	curatorFramework.blockUntilConnected(waitForZkConnectedMillis(), TimeUnit.MILLISECONDS);
	
	return curatorFramework;
}
 
Example #2
Source File: PartitionedLeaderServiceTest.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Test
public void testSingleServer() throws Exception {
    try (CuratorFramework curator = CuratorFrameworkFactory.newClient(_zookeeper.getConnectString(), new RetryNTimes(1, 10))) {
        curator.start();
        
        PartitionedLeaderService service = new PartitionedLeaderService(curator, "/leader/single", "instance0",
                "test-service", 3, 1, 5000, TimeUnit.MILLISECONDS, TestLeaderService::new, null);

        service.start();

        List<LeaderService> leaderServices = service.getPartitionLeaderServices();
        assertEquals(leaderServices.size(), 3, "Wrong number of services for the provided number of partitions");

        // Give it 5 seconds to attain leadership on all 3 partitions
        Stopwatch stopwatch = Stopwatch.createStarted();
        Set<Integer> leadPartitions = getLeadPartitions(service);

        while (leadPartitions.size() != 3 && stopwatch.elapsed(TimeUnit.SECONDS) < 5) {
            Thread.sleep(10);
            leadPartitions = getLeadPartitions(service);
        }

        assertEquals(leadPartitions, ImmutableSet.of(0, 1, 2));
        service.stop();
    }
}
 
Example #3
Source File: PurgeTest.java    From emodb with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    LifeCycleRegistry lifeCycleRegistry = mock(LifeCycleRegistry.class);
    _queueService = mock(QueueService.class);
    _jobHandlerRegistry = new DefaultJobHandlerRegistry();
    _jobStatusDAO = new InMemoryJobStatusDAO();
    _testingServer = new TestingServer();
    _curator = CuratorFrameworkFactory.builder()
            .connectString(_testingServer.getConnectString())
            .retryPolicy(new RetryNTimes(3, 100))
            .build();

    _curator.start();

    _service = new DefaultJobService(
            lifeCycleRegistry, _queueService, "testqueue", _jobHandlerRegistry, _jobStatusDAO, _curator,
            1, Duration.ZERO, 100, Duration.ofHours(1));

    _store = new InMemoryDataStore(new MetricRegistry());
    _dataStoreResource = new DataStoreResource1(_store, new DefaultDataStoreAsync(_store, _service, _jobHandlerRegistry),
            mock(CompactionControlSource.class), new UnlimitedDataStoreUpdateThrottler());

}
 
Example #4
Source File: CuratorZookeeperClient.java    From dubbo3 with Apache License 2.0 6 votes vote down vote up
public CuratorZookeeperClient(URL url) {
    super(url);
    try {
        Builder builder = CuratorFrameworkFactory.builder()
                .connectString(url.getBackupAddress())
                .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000))
                .connectionTimeoutMs(5000);
        String authority = url.getAuthority();
        if (authority != null && authority.length() > 0) {
            builder = builder.authorization("digest", authority.getBytes());
        }
        client = builder.build();
        client.getConnectionStateListenable().addListener((client, state) -> {
            if (state == ConnectionState.LOST) {
                CuratorZookeeperClient.this.stateChanged(StateListener.DISCONNECTED);
            } else if (state == ConnectionState.CONNECTED) {
                CuratorZookeeperClient.this.stateChanged(StateListener.CONNECTED);
            } else if (state == ConnectionState.RECONNECTED) {
                CuratorZookeeperClient.this.stateChanged(StateListener.RECONNECTED);
            }
        });
        client.start();
    } catch (Exception e) {
        throw new IllegalStateException(e.getMessage(), e);
    }
}
 
Example #5
Source File: ZookeeperServiceDiscovery.java    From Microservices-Deployment-Cookbook with MIT License 6 votes vote down vote up
private static ServiceProvider<Object> getGeolocationServiceProvider() throws Exception {
	if(geolocationServiceProvider == null) {
		CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient("192.168.99.100:2181", new RetryNTimes(5, 1000));
		curatorFramework.start();

		ServiceDiscovery<Object> serviceDiscovery = ServiceDiscoveryBuilder.builder(Object.class)
				.basePath("com.packt.microservices")
				.client(curatorFramework)
				.build();
		serviceDiscovery.start();

		geolocationServiceProvider = serviceDiscovery.serviceProviderBuilder()
				.serviceName("geolocation")
				.build();
		geolocationServiceProvider.start();
	}
	return geolocationServiceProvider;
}
 
Example #6
Source File: DistAtomicIntTest.java    From BigData-In-Practice with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    for (int i = 0; i < clientNums; i++) {
        String name = "client#" + i;
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    CuratorFramework client = ZKUtils.getClient();
                    client.start();
                    DistributedAtomicInteger atomicInteger = new DistributedAtomicInteger(client, distAtomicPath, new RetryNTimes(3, 1000));
                    for (int j = 0; j < 10; j++) {
                        AtomicValue<Integer> rc = atomicInteger.add(1);
                        System.out.println(name + " Result: " + rc.succeeded() + ", postValue: " + rc.postValue());
                        Thread.sleep(100);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    countDownLatch.countDown();
                }
            }
        }).start();
    }
    countDownLatch.await();
}
 
Example #7
Source File: CuratorDistributedBarrier.java    From yuzhouwan with Apache License 2.0 6 votes vote down vote up
private void init() {
        CuratorFramework curatorFramework = CuratorFrameworkFactory
                .builder()
                .connectString("localhost:2181")
                .connectionTimeoutMs(3000)
                .sessionTimeoutMs(5000)
                .retryPolicy(new RetryNTimes(3, 2000))
                .namespace("distBarrier")
                .build();
        curatorFramework.start();
        distributedBarrier = new DistributedBarrier(curatorFramework, "/barrier");

//        try {
//            Stat stat = curatorFramework.checkExists().forPath("/double");
//            if (stat != null)
//                curatorFramework.delete().deletingChildrenIfNeeded().forPath("/double");
//            else
//                curatorFramework.create().creatingParentsIfNeeded()
//                      .withMode(CreateMode.PERSISTENT).forPath("/double");
//        } catch (Exception e) {
//            throw new RuntimeException("Cannot create path '/double' !!", e);
//        }
        distributedDoubleBarrier = new DistributedDoubleBarrier(curatorFramework, "/double", 3);
    }
 
Example #8
Source File: ZookeeperServerHolder.java    From nacos-sync with Apache License 2.0 6 votes vote down vote up
@Override
CuratorFramework createServer(String clusterId, Supplier<String> serverAddressSupplier, String namespace) {
    List<String> allClusterConnectKey = skyWalkerCacheServices
            .getAllClusterConnectKey(clusterId);
    String serverList = Joiner.on(",").join(allClusterConnectKey);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder()
            .connectString(serverList)
            .retryPolicy(new RetryNTimes(1, 3000))
            .connectionTimeoutMs(5000);

    CuratorFramework client = builder.build();
    client.getConnectionStateListenable().addListener((clientInstance, state) -> {
        if (state == ConnectionState.LOST) {
            log.error("zk address: {} client state LOST",serverList);
        } else if (state == ConnectionState.CONNECTED) {
            log.info("zk address: {} client state CONNECTED",serverList);
        } else if (state == ConnectionState.RECONNECTED) {
            log.info("zk address: {} client state RECONNECTED",serverList);
        }
    });
    client.start();
    return client;
}
 
Example #9
Source File: ZookeeperIpFinderTest.java    From ignite with Apache License 2.0 6 votes vote down vote up
/**
 * Before test.
 *
 * @throws Exception
 */
@Override public void beforeTest() throws Exception {
    super.beforeTest();

    // remove stale system properties
    System.getProperties().remove(TcpDiscoveryZookeeperIpFinder.PROP_ZK_CONNECTION_STRING);

    // start the ZK cluster
    zkCluster = new TestingCluster(ZK_CLUSTER_SIZE);

    zkCluster.start();

    // start the Curator client so we can perform assertions on the ZK state later
    zkCurator = CuratorFrameworkFactory.newClient(zkCluster.getConnectString(), new RetryNTimes(10, 1000));
    zkCurator.start();
}
 
Example #10
Source File: ZkService.java    From DBus with Apache License 2.0 6 votes vote down vote up
/**
 * 创建ZK连接
 *
 * @param connectString  ZK服务器地址列表
 * @param sessionTimeout Session超时时间
 */
public ZkService(String connectString, int sessionTimeout) throws Exception {
    CuratorFrameworkFactory.Builder builder;
    builder = CuratorFrameworkFactory.builder()
            .connectString(connectString)
            .namespace("")
            .authorization("digest", auth.getBytes())
            .retryPolicy(new RetryNTimes(Integer.MAX_VALUE, 1000))
            .connectionTimeoutMs(sessionTimeout);

    client = builder.build();
    client.start();
    if (!client.blockUntilConnected(20, TimeUnit.SECONDS)) {
        throw new Exception("zookeeper connected failed!");
    }

    tableVersions = new HashMap<>();
    cache = new HashMap<>();
}
 
Example #11
Source File: MetaServerPrepareResourcesAndStart.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
private CuratorFramework connectToZk(String connectString) throws InterruptedException {
	Builder builder = CuratorFrameworkFactory.builder();

	builder.connectionTimeoutMs(3000);
	builder.connectString(connectString);
	builder.maxCloseWaitMs(3000);
	builder.namespace("xpipe");
	builder.retryPolicy(new RetryNTimes(3, 1000));
	builder.sessionTimeoutMs(5000);

	CuratorFramework client = builder.build();
	client.start();
	client.blockUntilConnected();

	return client;
}
 
Example #12
Source File: ZkClient.java    From binlake with Apache License 2.0 6 votes vote down vote up
public ZkClient(ZKConfig zkConfig,
                ServerConfig sc,
                HttpConfig hc,
                IWorkInitializer initI,
                ConcurrentHashMap<String, ILeaderSelector> lsm) {
    this.sc = sc;
    this.hc = hc;
    this.lsm = lsm;
    this.workInit = initI;
    this.zkConfig = zkConfig;
    this.host = sc.getHost();
    this.client = CuratorFrameworkFactory.newClient(
            zkConfig.getServers(),
            new RetryNTimes(zkConfig.getRetryTimes(), zkConfig.getSleepMsBetweenRetries())
    );
}
 
Example #13
Source File: Test.java    From hermes with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {

		Builder builder = CuratorFrameworkFactory.builder();

		builder.connectionTimeoutMs(1000);
		builder.connectString("127.0.0.1:2181,127.0.0.1:2182,127.0.0.1:2183");
		builder.retryPolicy(new RetryNTimes(1, 1000));
		builder.sessionTimeoutMs(5000);

		CuratorFramework framework = builder.build();
		framework.start();
		try {
			framework.blockUntilConnected();
		} catch (InterruptedException e) {
			throw new InitializationException(e.getMessage(), e);
		}

		System.in.read();
	}
 
Example #14
Source File: TestDefaultJobService.java    From emodb with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setUp() throws Exception {
    LifeCycleRegistry lifeCycleRegistry = mock(LifeCycleRegistry.class);
    _queueService = mock(QueueService.class);
    _jobHandlerRegistry = new DefaultJobHandlerRegistry();
    _jobStatusDAO = new InMemoryJobStatusDAO();
    _testingServer = new TestingServer();
    _curator = CuratorFrameworkFactory.builder()
            .connectString(_testingServer.getConnectString())
            .retryPolicy(new RetryNTimes(3, 100))
            .build();

    _curator.start();

    _service = new DefaultJobService(
            lifeCycleRegistry, _queueService, "testqueue", _jobHandlerRegistry, _jobStatusDAO, _curator,
            1, Duration.ZERO, 100, Duration.ofHours(1));
}
 
Example #15
Source File: IncrementIdGenerator.java    From paascloud-master with Apache License 2.0 6 votes vote down vote up
/**
 * 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);
	}
}
 
Example #16
Source File: KafkaSource.java    From cep with GNU Affero General Public License v3.0 6 votes vote down vote up
/**
 * This method prepares the properties that will be used to
 * consume messages from Kafka, and will start the Zookeeper connection.
 */

@Override
public void prepare() {
    String zkConnect = (String) getProperty("zk_connect");

    curator = CuratorFrameworkFactory.newClient(zkConnect, new RetryNTimes(10, 30000));
    curator.start();

    props = new Properties();
    props.put("auto.commit.enable", "true");
    props.put("zookeeper.connect", zkConnect);
    props.put("group.id", "rb-cep-engine");
    props.put("zookeeper.session.timeout.ms", "400");
    props.put("zookeeper.sync.time.ms", "200");
    props.put("auto.commit.interval.ms", "60000");
    props.put("auto.offset.reset", "largest");
}
 
Example #17
Source File: ZKUtil.java    From codes-scratch-zookeeper-netty with Apache License 2.0 6 votes vote down vote up
public static CuratorFramework create() {
    RetryNTimes retryPolicy = new RetryNTimes(5, 5000);
    String authString = Constants.ZK_USER_NAME + ":" + Constants.ZK_PASSWORD;
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString(Constants.ZK_CONNECT_STRING)
                                                     .retryPolicy(retryPolicy)
                                                     .connectionTimeoutMs(Constants.ZOO_KEEPER_TIMEOUT)
                                                     .sessionTimeoutMs(Constants.ZOO_KEEPER_TIMEOUT * 3)
                                                     .authorization("digest", authString.getBytes()).build();
    try {
        acl.clear();
        acl.add(new ACL(ZooDefs.Perms.ALL,
                        new Id("digest", DigestAuthenticationProvider.generateDigest(authString))));
        acl.add(new ACL(ZooDefs.Perms.READ, ZooDefs.Ids.ANYONE_ID_UNSAFE));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
        LOGGER.error("ZKUtil-->>create() error,", e);
    }
    return client;
}
 
Example #18
Source File: CuratorTest2.java    From liteFlow with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
        // 1.Connect to zk
        CuratorFramework client = CuratorFrameworkFactory.newClient(
                ZK_ADDRESS,
                new RetryNTimes(10, 5000)
        );
        client.start();

//        removeNodeData(client);

//        createNode(client);

//        nodeListen(client);
//
        modifyNodeData(client);

    }
 
Example #19
Source File: ZKAssistedDiscovery.java    From attic-apex-malhar with Apache License 2.0 6 votes vote down vote up
@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);
  }
}
 
Example #20
Source File: AdHocThrottleTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startZookeeper() throws Exception {
    _testingServer = new TestingServer();
    _rootCurator = CuratorFrameworkFactory.builder()
            .connectString(_testingServer.getConnectString())
            .retryPolicy(new RetryNTimes(3, 100))
            .threadFactory(new ThreadFactoryBuilder().setNameFormat("test-%d").setDaemon(true).build())
            .build();
    _rootCurator.start();
}
 
Example #21
Source File: CuratorConfiguration.java    From Kafdrop with Apache License 2.0 5 votes vote down vote up
@Primary
@Bean(initMethod = "start", destroyMethod = "close")
public CuratorFramework curatorFramework(ZookeeperProperties props)
{
   return CuratorFrameworkFactory.builder()
      .connectString(props.getConnect())
      .connectionTimeoutMs(props.getConnectTimeoutMillis())
      .sessionTimeoutMs(props.getSessionTimeoutMillis())
      .retryPolicy(new RetryNTimes(props.getMaxRetries(), props.getRetryMillis()))
      .build();
}
 
Example #22
Source File: CuratorInBackground.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
private void init() {
    RetryPolicy retryPolicy = new RetryNTimes(3, 2000);
    curatorFramework = CuratorFrameworkFactory.builder()
            .connectString("127.0.0.1:2181")
            .sessionTimeoutMs(5000)
            .connectionTimeoutMs(4000)
            .retryPolicy(retryPolicy)
            .namespace("background")
            .build();
    curatorFramework.start();

    countDownLatch = new CountDownLatch(2);
    executorService = Executors.newFixedThreadPool(2);
}
 
Example #23
Source File: ZookeeperBrokers.java    From message-queue-client-framework with Apache License 2.0 5 votes vote down vote up
public ZookeeperBrokers(ZookeeperHosts zkHosts) {
    _zkPath = zkHosts.getBrokerZkPath();
    _topic = zkHosts.getTopic();
    _curator = CuratorFrameworkFactory.newClient(zkHosts.getBrokerZkStr(), new RetryNTimes(
            Integer.MAX_VALUE, KafkaConstants.INTERVAL_IN_MS));
    _curator.start();
}
 
Example #24
Source File: ZookeeperBrokers.java    From message-queue-client-framework with Apache License 2.0 5 votes vote down vote up
public ZookeeperBrokers(String zkStr, String zkPath, String topic) {
    _zkPath = zkPath;
    _topic = topic;
    _curator = CuratorFrameworkFactory.newClient(zkStr, new RetryNTimes(
            Integer.MAX_VALUE, KafkaConstants.INTERVAL_IN_MS));
    _curator.start();
}
 
Example #25
Source File: CuratorNodeCache.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
private void init() {
    curatorFramework = CuratorFrameworkFactory.
            builder().
            connectString("10.27.129.60:2181,10.27.129.60:2182,10.27.129.60:2183").
            sessionTimeoutMs(5000).
            connectionTimeoutMs(10000).
            retryPolicy(new RetryNTimes(2, 2000)).
            namespace("watchNodeCache").
            build();
    curatorFramework.start();
    LOG.info("Curator's Framework start...");
}
 
Example #26
Source File: DataStoreUpdateThrottleTest.java    From emodb with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public static void startZookeeper() throws Exception {
    _testingServer = new TestingServer();
    _rootCurator = CuratorFrameworkFactory.builder()
            .connectString(_testingServer.getConnectString())
            .retryPolicy(new RetryNTimes(3, 100))
            .threadFactory(new ThreadFactoryBuilder().setNameFormat("test-%d").setDaemon(true).build())
            .build();
    _rootCurator.start();
}
 
Example #27
Source File: ZKTimestampStorage.java    From phoenix-omid with Apache License 2.0 5 votes vote down vote up
@Inject
public ZKTimestampStorage(CuratorFramework zkClient) throws Exception {
    LOG.info("ZK Client state {}", zkClient.getState());
    timestamp = new DistributedAtomicLong(zkClient, TIMESTAMP_ZNODE, new RetryNTimes(3, 1000)); // TODO Configure
    // this?
    if (timestamp.initialize(INITIAL_MAX_TS_VALUE)) {
        LOG.info("Timestamp value in ZNode initialized to {}", INITIAL_MAX_TS_VALUE);
    }
}
 
Example #28
Source File: ZkUtils.java    From GoPush with GNU General Public License v2.0 5 votes vote down vote up
/**
 * 初始化zk链接
 *
 * @param zookeeperServer
 * @param connectionTimeout
 * @param sessionTimeout
 * @param maxRetries
 * @param retriesSleepTime
 * @param namespace
 * @param listener
 */
public void init(String zookeeperServer,
                 int connectionTimeout,
                 int sessionTimeout,
                 int maxRetries, int retriesSleepTime,
                 String namespace,
                 ZkStateListener listener) {
    if (zkClient == null) {
        zkClient = CuratorFrameworkFactory.builder()
                .connectString(zookeeperServer)
                .connectionTimeoutMs(connectionTimeout)
                .sessionTimeoutMs(sessionTimeout)
                .namespace(namespace)
                .retryPolicy(new RetryNTimes(maxRetries, retriesSleepTime))
                .build();
    }
    zkClient.getConnectionStateListenable().addListener((curatorFramework, connectionState) -> {
        pathChildrenCaches.clear();
        nodeCaches.clear();
        treeCaches.clear();
        if (connectionState == ConnectionState.CONNECTED) {
            listener.connectedEvent(curatorFramework, connectionState);
        } else if (connectionState == ConnectionState.RECONNECTED) {
            listener.reconnectedEvent(curatorFramework, connectionState);
        } else if (connectionState == ConnectionState.LOST) {
            listener.lostEvent(curatorFramework, connectionState);
        }
    });
    zkClient.start();

}
 
Example #29
Source File: CuratorZKPaths.java    From yuzhouwan with Apache License 2.0 5 votes vote down vote up
private void init() {
    curatorFramework = CuratorFrameworkFactory
            .builder()
            .connectString("localhost:2181")
            .connectionTimeoutMs(3000)
            .sessionTimeoutMs(5000)
            .retryPolicy(new RetryNTimes(3, 2000))
            .namespace("zkPaths")
            .build();
    curatorFramework.start();
}
 
Example #30
Source File: ZkService.java    From DBus with Apache License 2.0 5 votes vote down vote up
/**
 * 获得ZK中的当前值,如果不存在,抛出异常
 *
 * @param path
 * @return
 * @throws Exception
 */
private long currentValueFromZk(String path) throws Exception {
    if (isExists(path)) {
        DistributedAtomicLong count = new DistributedAtomicLong(client, path, new RetryNTimes(10, 1000));
        AtomicValue<Long> val = count.get();
        return val.postValue();
    } else {
        throw new RuntimeException("Path is not existed! Call nextValue firstly!");
    }
}