当前位置: 首页>>代码示例>>Java>>正文


Java SharedCount.start方法代码示例

本文整理汇总了Java中org.apache.curator.framework.recipes.shared.SharedCount.start方法的典型用法代码示例。如果您正苦于以下问题:Java SharedCount.start方法的具体用法?Java SharedCount.start怎么用?Java SharedCount.start使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在org.apache.curator.framework.recipes.shared.SharedCount的用法示例。


在下文中一共展示了SharedCount.start方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: printAll

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
public static void printAll(CuratorFramework client, String path,
		String prefix) {
	client.start();
	List<String> children;
	try {
		children = client.getChildren().forPath(path);
		for (String child : children) {
			if (child.startsWith(prefix)) {
				SharedCount c = new SharedCount(client, path + child, 0);
				c.start();
				System.out.println(path + child + "\t" + c.getCount());
				c.close();
			}
		}
		client.close();
	} catch (Exception e) {
		e.printStackTrace();

	}
}
 
开发者ID:mitdbg,项目名称:AdaptDB,代码行数:21,代码来源:CuratorUtils.java

示例2: getIncrementForPrefix

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
@SuppressWarnings("resource")
private int getIncrementForPrefix(String prefix) {
    String path = String.format(COUNTER_PATH, prefix);
    SharedCount sharedCount = new SharedCount(curator.get(), path, 1);
    try {
        sharedCount.start();
        for (int count = sharedCount.getCount(); true; count = sharedCount.getCount()) {
            if (sharedCount.trySetCount(count + 1)) {
                return count;
            }
        }
    } catch (Exception e) {
        throw FabricException.launderThrowable(e);
    } finally {
        IOUtils.safeClose(sharedCount);
    }
}
 
开发者ID:tdiesler,项目名称:fabric8poc,代码行数:18,代码来源:ClusterDataStoreImpl.java

示例3: createAndStartCounter

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
public static SharedCount createAndStartCounter(String zkHosts,
		String counterPath) {
	SharedCount c = createCounter(zkHosts, counterPath);
	try {
		c.start();
		return c;
	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException("Failed to start the counter: "
				+ counterPath + "\n" + e.getMessage());
	}
}
 
开发者ID:mitdbg,项目名称:AdaptDB,代码行数:13,代码来源:CuratorUtils.java

示例4: getCounter

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
public static int getCounter(CuratorFramework client, String counterPath) {
	SharedCount c = new SharedCount(client, counterPath, 0);
	try {
		c.start();
		int val = c.getCount();
		c.close();
		return val;

	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException("Failed to add the counter: "
				+ counterPath + "\n" + e.getMessage());
	}
}
 
开发者ID:mitdbg,项目名称:AdaptDB,代码行数:15,代码来源:CuratorUtils.java

示例5: addCounter

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
public static void addCounter(CuratorFramework client, String counterPath,
		int increment) {
	SharedCount c = new SharedCount(client, counterPath, 0);
	try {
		c.start();
		int oldVal = c.getCount();
		c.setCount(oldVal + increment);
		c.close();
	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException("Failed to add the counter: "
				+ counterPath + "\n" + e.getMessage());
	}
}
 
开发者ID:mitdbg,项目名称:AdaptDB,代码行数:15,代码来源:CuratorUtils.java

示例6: setCounter

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
public static void setCounter(CuratorFramework client, String counterPath,
		int value) {
	SharedCount c = new SharedCount(client, counterPath, 0);
	try {
		c.start();
		c.setCount(value);
		c.close();
	} catch (Exception e) {
		e.printStackTrace();
		throw new RuntimeException("Failed to add the counter: "
				+ counterPath + "\n" + e.getMessage());
	}
}
 
开发者ID:mitdbg,项目名称:AdaptDB,代码行数:14,代码来源:CuratorUtils.java

示例7: testThreadedLeaseIncrease

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
@Test
public void testThreadedLeaseIncrease() throws Exception
{
    final Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
    try
    {
        client.start();

        final SharedCount count = new SharedCount(client, "/foo/count", 1);
        count.start();

        final InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", count);

        ExecutorService service = Executors.newCachedThreadPool();

        final CountDownLatch latch1 = new CountDownLatch(1);
        final CountDownLatch latch2 = new CountDownLatch(1);
        Future<Object> future1 = service.submit
            (
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        Lease lease = semaphore.acquire(timing.seconds(), TimeUnit.SECONDS);
                        Assert.assertNotNull(lease);
                        latch1.countDown();
                        lease = semaphore.acquire(timing.forWaiting().seconds(), TimeUnit.SECONDS);
                        Assert.assertNotNull(lease);
                        latch2.countDown();
                        return null;
                    }
                }
            );
        Future<Object> future2 = service.submit
            (
                new Callable<Object>()
                {
                    @Override
                    public Object call() throws Exception
                    {
                        Assert.assertTrue(latch1.await(timing.forWaiting().seconds(), TimeUnit.SECONDS));
                        timing.sleepABit(); // make sure second acquire is waiting
                        Assert.assertTrue(count.trySetCount(2));
                        //Make sure second acquire takes less than full waiting time:
                        timing.sleepABit();
                        Assert.assertTrue(latch2.await(0, TimeUnit.SECONDS));
                        return null;
                    }
                }
            );

        future1.get();
        future2.get();

        count.close();
    }
    finally
    {
        TestCleanState.closeAndTestClean(client);
    }
}
 
开发者ID:apache,项目名称:curator,代码行数:64,代码来源:TestInterProcessSemaphore.java

示例8: main

import org.apache.curator.framework.recipes.shared.SharedCount; //导入方法依赖的package包/类
public static void main(String[] args) throws IOException, Exception {
	final Random rand = new Random();
	SharedCounterExample example = new SharedCounterExample();
	try (TestingServer server = new TestingServer()) {
		CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
		client.start();
		
		SharedCount baseCount = new SharedCount(client, PATH, 0);
		baseCount.addListener(example);
		baseCount.start();
		
		List<SharedCount> examples = Lists.newArrayList();
		ExecutorService service = Executors.newFixedThreadPool(QTY);
		for (int i = 0; i < QTY; ++i) {
			final SharedCount count = new SharedCount(client, PATH, 0);
			examples.add(count);
			Callable<Void> task = new Callable<Void>() {
				@Override
				public Void call() throws Exception {
					count.start();
					Thread.sleep(rand.nextInt(10000));
					System.out.println("Increment:" + count.trySetCount(count.getVersionedValue(), count.getCount() + rand.nextInt(10)));
					return null;
				}
			};
			service.submit(task);
		}
		
		
		
		service.shutdown();
		service.awaitTermination(10, TimeUnit.MINUTES);
		
		for (int i = 0; i < QTY; ++i) {
			examples.get(i).close();
		}
		baseCount.close();
	}


}
 
开发者ID:smallnest,项目名称:ZKRecipesByExample,代码行数:42,代码来源:SharedCounterExample.java


注:本文中的org.apache.curator.framework.recipes.shared.SharedCount.start方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。