Java Code Examples for org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier#enter()

The following examples show how to use org.apache.curator.framework.recipes.barriers.DistributedDoubleBarrier#enter() . 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: DoubleDistributedBarrierTest.java    From BigData-In-Practice with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws InterruptedException {
    CuratorFramework client = ZKUtils.getClient();
    client.start();
    ExecutorService service = Executors.newFixedThreadPool(clientNums);
    for (int i = 0; i < (clientNums + 2); ++i) {
        final DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, barrierPath, clientNums);
        final int index = i;
        Callable<Void> task = new Callable<Void>() {
            @Override
            public Void call() throws Exception {
                Thread.sleep((long) (3 * Math.random()));
                System.out.println("Client #" + index + " 等待");
                if (false == barrier.enter(5, TimeUnit.SECONDS)) {
                    System.out.println("Client #" + index + " 等待超时!");
                    return null;
                }
                System.out.println("Client #" + index + " 进入");
                Thread.sleep((long) (3000 * Math.random()));
                barrier.leave();
                System.out.println("Client #" + index + " 结束");
                return null;
            }
        };
        service.submit(task);
    }
    service.shutdown();
    service.awaitTermination(10, TimeUnit.MINUTES);
    client.close();
    System.out.println("OK!");

}
 
Example 2
Source File: DistributedBarrierExample.java    From ZKRecipesByExample with Apache License 2.0 5 votes vote down vote up
public static void main(String[] args) throws Exception {
	try (TestingServer server = new TestingServer()) {
		CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
		client.start();

		ExecutorService service = Executors.newFixedThreadPool(QTY);
		for (int i = 0; i < QTY; ++i) {
			final DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, PATH, QTY);
			final int index = i;
			Callable<Void> task = new Callable<Void>() {
				@Override
				public Void call() throws Exception {
					
					Thread.sleep((long) (3 * Math.random()));
					System.out.println("Client #" + index + " enters");
					barrier.enter();
					System.out.println("Client #" + index + " begins");
					Thread.sleep((long) (3000 * Math.random()));
					barrier.leave();
					System.out.println("Client #" + index + " left");
					return null;
				}
			};
			service.submit(task);
		}
		
		
		service.shutdown();
		service.awaitTermination(10, TimeUnit.MINUTES);

	}

}
 
Example 3
Source File: ClusterBarrierManager.java    From Decision with Apache License 2.0 2 votes vote down vote up
public Boolean manageAckBarrier(String barrierPath, Integer nodesExpected) throws Exception {

        DistributedDoubleBarrier barrier =  getDistributedDoubleBarrier(barrierPath, nodesExpected);
        return barrier.enter(ackTimeout, TimeUnit.MILLISECONDS);

    }