Added doc and completed the example
authorJordan Zimmerman <jordan@jordanzimmerman.com>
Thu, 11 Oct 2012 20:24:59 +0000 (13:24 -0700)
committerJordan Zimmerman <jordan@jordanzimmerman.com>
Thu, 11 Oct 2012 20:24:59 +0000 (13:24 -0700)
curator-examples/src/main/java/locking/ExampleClientThatLocks.java
curator-examples/src/main/java/locking/FakeLimitedResource.java
curator-examples/src/main/java/locking/LockingExample.java

index 5f99834..cf95627 100644 (file)
@@ -18,6 +18,7 @@ package locking;
 
 import com.netflix.curator.framework.CuratorFramework;
 import com.netflix.curator.framework.recipes.locks.InterProcessMutex;
+import java.util.concurrent.TimeUnit;
 
 public class ExampleClientThatLocks
 {
@@ -32,9 +33,12 @@ public class ExampleClientThatLocks
         lock = new InterProcessMutex(client, lockPath);
     }
 
-    public void     doWork() throws Exception
+    public void     doWork(long time, TimeUnit unit) throws Exception
     {
-        lock.acquire();
+        if ( !lock.acquire(time, unit) )
+        {
+            throw new IllegalStateException(clientName + " could not acquire the lock");
+        }
         try
         {
             System.out.println(clientName + " has the lock");
@@ -43,7 +47,7 @@ public class ExampleClientThatLocks
         finally
         {
             System.out.println(clientName + " releasing the lock");
-            lock.release();
+            lock.release(); // always release the lock in a finally block
         }
     }
 }
index acc2f4d..8acde55 100644 (file)
@@ -18,12 +18,17 @@ package locking;
 
 import java.util.concurrent.atomic.AtomicBoolean;
 
+/**
+ * Simulates some external resource that can only be access by one process at a time
+ */
 public class FakeLimitedResource
 {
     private final AtomicBoolean     inUse = new AtomicBoolean(false);
 
     public void     use() throws InterruptedException
     {
+        // in a real application this would be accessing/manipulating a shared resource
+
         if ( !inUse.compareAndSet(false, true) )
         {
             throw new IllegalStateException("Needs to be used by one client at a time");
index 99fed83..378051c 100644 (file)
@@ -35,7 +35,11 @@ public class LockingExample
 
     public static void main(String[] args) throws Exception
     {
+        // all of the useful sample code is in ExampleClientThatLocks.java
+
+        // FakeLimitedResource simulates some external resource that can only be access by one process at a time
         final FakeLimitedResource   resource = new FakeLimitedResource();
+
         ExecutorService             service = Executors.newFixedThreadPool(QTY);
         final TestingServer         server = new TestingServer();
         try
@@ -56,9 +60,13 @@ public class LockingExample
                             ExampleClientThatLocks      example = new ExampleClientThatLocks(client, PATH, resource, "Client " + index);
                             for ( int j = 0; j < REPETITIONS; ++j )
                             {
-                                example.doWork();
+                                example.doWork(10, TimeUnit.SECONDS);
                             }
                         }
+                        catch ( Throwable e )
+                        {
+                            e.printStackTrace();
+                        }
                         finally
                         {
                             Closeables.closeQuietly(client);