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

The following examples show how to use org.apache.curator.framework.recipes.locks.Lease. 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 5 votes vote down vote up
@Override
public DistributedLease acquire(long time, TimeUnit unit) throws Exception {
  Lease lease = semaphore.acquire(time, unit);
  if(lease != null){
    return new LeaseHolder(lease);
  }else{
    return null;
  }
}
 
Example #4
Source File: ZkDistributedSemaphore.java    From dremio-oss with Apache License 2.0 5 votes vote down vote up
@Override
public DistributedLease acquire(int permits, long time, TimeUnit unit) throws Exception {
  Collection<Lease> leases = semaphore.acquire(permits, time, unit);
  if (leases != null) {
    return new LeasesHolder(leases);
  }
  return null;

}
 
Example #5
Source File: ZkDistributedSemaphore.java    From Bats with Apache License 2.0 4 votes vote down vote up
public LeaseHolder(Lease lease) {
  super();
  this.lease = lease;
}
 
Example #6
Source File: ZkDistributedSemaphore.java    From dremio-oss with Apache License 2.0 4 votes vote down vote up
LeasesHolder(Collection<Lease> leases) {
  this.leases = leases;
}