You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@curator.apache.org by ra...@apache.org on 2014/06/18 01:03:15 UTC

[11/18] git commit: 1. Added license. 2. ExecuteAfterConnectionEstablished should use a Callable and allow the exception to bubble up

1. Added license. 2. ExecuteAfterConnectionEstablished should use a Callable and allow the exception to bubble up


Project: http://git-wip-us.apache.org/repos/asf/curator/repo
Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/59076777
Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/59076777
Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/59076777

Branch: refs/heads/master
Commit: 59076777adfe42d96b0a92f775c8920033e1d975
Parents: 59bab73
Author: randgalt <ra...@apache.org>
Authored: Mon Jun 16 12:40:34 2014 -0500
Committer: randgalt <ra...@apache.org>
Committed: Mon Jun 16 12:40:34 2014 -0500

----------------------------------------------------------------------
 .../framework/imps/TestBlockUntilConnected.java | 18 ++++
 .../ExecuteAfterConnectionEstablished.java      | 96 ++++++++++++--------
 .../framework/recipes/leader/LeaderLatch.java   | 32 +++----
 3 files changed, 89 insertions(+), 57 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/curator/blob/59076777/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
----------------------------------------------------------------------
diff --git a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
index 996e5fc..8dfb7d8 100644
--- a/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
+++ b/curator-framework/src/test/java/org/apache/curator/framework/imps/TestBlockUntilConnected.java
@@ -1,3 +1,21 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.curator.framework.imps;
 
 import java.util.Timer;

http://git-wip-us.apache.org/repos/asf/curator/blob/59076777/curator-recipes/src/main/java/org/apache/curator/framework/recipes/ExecuteAfterConnectionEstablished.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/ExecuteAfterConnectionEstablished.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/ExecuteAfterConnectionEstablished.java
index d213d37..408ed03 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/ExecuteAfterConnectionEstablished.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/ExecuteAfterConnectionEstablished.java
@@ -1,53 +1,73 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
 package org.apache.curator.framework.recipes;
 
-import java.util.concurrent.ExecutorService;
-
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.utils.ThreadUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
+import java.util.concurrent.Callable;
+import java.util.concurrent.ExecutorService;
 
 /**
  * Utility class to allow execution of logic once a ZooKeeper connection becomes available.
- *
  */
 public class ExecuteAfterConnectionEstablished
 {
-	private final static Logger log = LoggerFactory.getLogger(ExecuteAfterConnectionEstablished.class);
-	
-	/**
-	 * Spawns a new new background thread that will block until a connection is available and
-	 * then execute the 'runAfterConnection' logic
-	 * @param name The name of the spawned thread
-	 * @param client The curator client
-	 * @param runAfterConnection The logic to run
-	 */
-	public static void executeAfterConnectionEstablishedInBackground(String name,
-																     final CuratorFramework client,
-																     final Runnable runAfterConnection)
-	{
+    private final static Logger log = LoggerFactory.getLogger(ExecuteAfterConnectionEstablished.class);
+
+    /**
+     * Spawns a new new background thread that will block until a connection is available and
+     * then execute the 'runAfterConnection' logic
+     *
+     * @param client             The curator client
+     * @param runAfterConnection The logic to run
+     */
+    public static <T> T executeAfterConnectionEstablishedInBackground(final CuratorFramework client, final Callable<T> runAfterConnection) throws Exception
+    {
         //Block until connected
-        final ExecutorService executor = ThreadUtils.newSingleThreadExecutor(name);
-        executor.submit(new Runnable()
+        final ExecutorService executor = ThreadUtils.newSingleThreadExecutor(runAfterConnection.getClass().getSimpleName());
+        Callable<T> internalCall = new Callable<T>()
         {
-			
-			@Override
-			public void run()
-			{
-				try
-				{
-		        	client.blockUntilConnected();			        
-			        runAfterConnection.run();
-				}
-				catch(Exception e)
-				{
-					log.error("An error occurred blocking until a connection is available", e);
-				}
-				finally
-				{
-					executor.shutdown();
-				}
-			}
-        });
-	}
+            @Override
+            public T call() throws Exception
+            {
+                try
+                {
+                    client.blockUntilConnected();
+                    return runAfterConnection.call();
+                }
+                catch ( Exception e )
+                {
+                    log.error("An error occurred blocking until a connection is available", e);
+                    throw e;
+                }
+                finally
+                {
+                    executor.shutdown();
+                }
+            }
+        };
+        return executor.submit(internalCall).get();
+    }
+
+    private ExecuteAfterConnectionEstablished()
+    {
+    }
 }

http://git-wip-us.apache.org/repos/asf/curator/blob/59076777/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
----------------------------------------------------------------------
diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
index 13a9f21..f4c1cef 100644
--- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
+++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/leader/LeaderLatch.java
@@ -22,7 +22,6 @@ package org.apache.curator.framework.recipes.leader;
 import com.google.common.annotations.VisibleForTesting;
 import com.google.common.base.Function;
 import com.google.common.base.Preconditions;
-
 import org.apache.curator.framework.CuratorFramework;
 import org.apache.curator.framework.api.BackgroundCallback;
 import org.apache.curator.framework.api.CuratorEvent;
@@ -33,7 +32,6 @@ import org.apache.curator.framework.recipes.locks.LockInternalsSorter;
 import org.apache.curator.framework.recipes.locks.StandardLockInternalsDriver;
 import org.apache.curator.framework.state.ConnectionState;
 import org.apache.curator.framework.state.ConnectionStateListener;
-import org.apache.curator.utils.ThreadUtils;
 import org.apache.curator.utils.ZKPaths;
 import org.apache.zookeeper.CreateMode;
 import org.apache.zookeeper.KeeperException;
@@ -41,15 +39,14 @@ import org.apache.zookeeper.WatchedEvent;
 import org.apache.zookeeper.Watcher;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import java.io.Closeable;
 import java.io.EOFException;
 import java.io.IOException;
 import java.util.Collection;
 import java.util.List;
+import java.util.concurrent.Callable;
 import java.util.concurrent.CountDownLatch;
 import java.util.concurrent.Executor;
-import java.util.concurrent.ExecutorService;
 import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicBoolean;
 import java.util.concurrent.atomic.AtomicReference;
@@ -159,23 +156,20 @@ public class LeaderLatch implements Closeable
     {
         Preconditions.checkState(state.compareAndSet(State.LATENT, State.STARTED), "Cannot be started more than once");
 
-        ExecuteAfterConnectionEstablished.executeAfterConnectionEstablishedInBackground(LeaderLatch.class.getName(),
-        		client, new Runnable()
-        {					
-        	@Override
-			public void run()
-			{
-        		try
-        		{
-        			client.getConnectionStateListenable().addListener(listener);		        	
-        			reset();
-        		}
-        		catch(Exception ex)
+        ExecuteAfterConnectionEstablished.executeAfterConnectionEstablishedInBackground
+        (
+            client,
+            new Callable<Void>()
+            {
+                @Override
+                public Void call() throws Exception
                 {
-                	log.error("An error occurred checking resetting leadership.", ex);
+                    client.getConnectionStateListenable().addListener(listener);
+                    reset();
+                    return null;
                 }
-			}
-		});     
+            }
+        );
     }
 
     /**