org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2 Java Examples

The following examples show how to use org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2. 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: SharedSemaphoreTest.java    From BigData-In-Practice with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    CuratorFramework client = ZKUtils.getClient();
    client.start();
    InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, MAX_LEASE);
    Collection<Lease> leases = semaphore.acquire(5);
    System.out.println("获取租约数量:" + leases.size());
    Lease lease = semaphore.acquire();
    System.out.println("获取单个租约");
    resource.use(); // 使用资源
    // 再次申请获取5个leases,此时leases数量只剩4个,不够,将超时
    Collection<Lease> leases2 = semaphore.acquire(5, 10, TimeUnit.SECONDS);
    System.out.println("获取租约,如果超时将为null: " + leases2);
    System.out.println("释放租约");
    semaphore.returnLease(lease);
    // 再次申请获取5个,这次刚好够
    leases2 = semaphore.acquire(5, 10, TimeUnit.SECONDS);
    System.out.println("获取租约,如果超时将为null: " + leases2);
    System.out.println("释放集合中的所有租约");
    semaphore.returnAll(leases);
    semaphore.returnAll(leases2);
    client.close();
    System.out.println("结束!");
}
 
Example #2
Source File: InterProcessSemaphoreExample.java    From ZKRecipesByExample with Apache License 2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
	FakeLimitedResource resource = new FakeLimitedResource();
	try (TestingServer server = new TestingServer()) {

		CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3));
		client.start();

		InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, MAX_LEASE);
		Collection<Lease> leases = semaphore.acquire(5);
		System.out.println("get " + leases.size() + " leases");
		Lease lease = semaphore.acquire();
		System.out.println("get another lease");

		resource.use();

		Collection<Lease> leases2 = semaphore.acquire(5, 10, TimeUnit.SECONDS);
		System.out.println("Should timeout and acquire return " + leases2);

		System.out.println("return one lease");
		semaphore.returnLease(lease);
		System.out.println("return another 5 leases");
		semaphore.returnAll(leases);
	}
}
 
Example #3
Source File: ZkDistributedSemaphore.java    From Bats with Apache License 2.0 4 votes vote down vote up
public ZkDistributedSemaphore(CuratorFramework client, String path, int numberOfLeases) {
  this.semaphore = new InterProcessSemaphoreV2(client, path, numberOfLeases);
}
 
Example #4
Source File: ZkDistributedSemaphore.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
ZkDistributedSemaphore(CuratorFramework client, String path, int numberOfLeases) throws Exception {
  this.semaphore = new InterProcessSemaphoreV2(client, path, numberOfLeases);
  this.path = ZKPaths.makePath(path, "leases");
  this.client = client;
}