package com.colobu.zkrecipe.lock;

import java.util.Collection;
import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2;
import org.apache.curator.framework.recipes.locks.Lease;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;

public class InterProcessSemaphoreExample {
	private static final int MAX_LEASE = 10;
	private static final String PATH = "/examples/locks";

	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);
		}
	}

}

@@ -447,16 +447,47 @@
 erver;%0A%0A
+/**%0A * %E4%BF%A1%E5%8F%B7%E9%87%8FShared Semaphore%0A */%0A
 public c
@@ -522,17 +522,20 @@
 ample %7B%0A
-%09
+    
 private 
@@ -567,17 +567,20 @@
 E = 10;%0A
-%09
+    
 private 
@@ -626,17 +626,20 @@
 ocks%22;%0A%0A
-%09
+    
 public s
@@ -688,18 +688,24 @@
 ption %7B%0A
-%09%09
+        
 FakeLimi
@@ -754,18 +754,24 @@
 urce();%0A
-%09%09
+        
 try (Tes
@@ -814,19 +814,28 @@
 r()) %7B%0A%0A
-%09%09%09
+            
 CuratorF
@@ -952,19 +952,28 @@
 0, 3));%0A
-%09%09%09
+            
 client.s
@@ -981,19 +981,28 @@
 art();%0A%0A
-%09%09%09
+            
 InterPro
@@ -1083,19 +1083,28 @@
 LEASE);%0A
-%09%09%09
+            
 Collecti
@@ -1144,19 +1144,28 @@
 ire(5);%0A
-%09%09%09
+            
 System.o
@@ -1212,19 +1212,28 @@
 ases%22);%0A
-%09%09%09
+            
 Lease le
@@ -1259,19 +1259,28 @@
 uire();%0A
-%09%09%09
+            
 System.o
@@ -1313,19 +1313,28 @@
 ase%22);%0A%0A
-%09%09%09
+            
 resource
@@ -1342,19 +1342,28 @@
 use();%0A%0A
-%09%09%09
+            
 Collecti
@@ -1426,19 +1426,28 @@
 CONDS);%0A
-%09%09%09
+            
 System.o
@@ -1507,19 +1507,28 @@
 ses2);%0A%0A
-%09%09%09
+            
 System.o
@@ -1555,27 +1555,36 @@
 ne lease%22);%0A
-%09%09%09
+            
 semaphore.re
@@ -1601,19 +1601,28 @@
 lease);%0A
-%09%09%09
+            
 System.o
@@ -1664,11 +1664,20 @@
 %22);%0A
-%09%09%09
+            
 sema
@@ -1705,13 +1705,22 @@
 s);%0A
-%09%09%7D%0A%09
+        %7D%0A    
 %7D%0A%0A%7D

test Semaphore