Utility to get a name for the thread
Project: http://git-wip-us.apache.org/repos/asf/curator/repo Commit: http://git-wip-us.apache.org/repos/asf/curator/commit/1c94c7ef Tree: http://git-wip-us.apache.org/repos/asf/curator/tree/1c94c7ef Diff: http://git-wip-us.apache.org/repos/asf/curator/diff/1c94c7ef Branch: refs/heads/master Commit: 1c94c7efa3b5256e14862495055805b4612ca4f4 Parents: 5907677 Author: randgalt <[email protected]> Authored: Mon Jun 16 14:07:24 2014 -0500 Committer: randgalt <[email protected]> Committed: Mon Jun 16 14:07:24 2014 -0500 ---------------------------------------------------------------------- .../org/apache/curator/utils/ThreadUtils.java | 9 +++ .../recipes/AfterConnectionEstablished.java | 73 ++++++++++++++++++++ .../ExecuteAfterConnectionEstablished.java | 73 -------------------- 3 files changed, 82 insertions(+), 73 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/curator/blob/1c94c7ef/curator-client/src/main/java/org/apache/curator/utils/ThreadUtils.java ---------------------------------------------------------------------- diff --git a/curator-client/src/main/java/org/apache/curator/utils/ThreadUtils.java b/curator-client/src/main/java/org/apache/curator/utils/ThreadUtils.java index f238124..9665dfe 100644 --- a/curator-client/src/main/java/org/apache/curator/utils/ThreadUtils.java +++ b/curator-client/src/main/java/org/apache/curator/utils/ThreadUtils.java @@ -53,4 +53,13 @@ public class ThreadUtils .setDaemon(true) .build(); } + + public static String getProcessName(Class<?> clazz) + { + if ( clazz.isAnonymousClass() ) + { + return getProcessName(clazz.getEnclosingClass()); + } + return clazz.getSimpleName(); + } } http://git-wip-us.apache.org/repos/asf/curator/blob/1c94c7ef/curator-recipes/src/main/java/org/apache/curator/framework/recipes/AfterConnectionEstablished.java ---------------------------------------------------------------------- diff --git a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/AfterConnectionEstablished.java b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/AfterConnectionEstablished.java new file mode 100644 index 0000000..f37f7c0 --- /dev/null +++ b/curator-recipes/src/main/java/org/apache/curator/framework/recipes/AfterConnectionEstablished.java @@ -0,0 +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 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 AfterConnectionEstablished +{ + private final static Logger log = LoggerFactory.getLogger(AfterConnectionEstablished.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 execute(final CuratorFramework client, final Callable<T> runAfterConnection) throws Exception + { + //Block until connected + final ExecutorService executor = ThreadUtils.newSingleThreadExecutor(runAfterConnection.getClass().getSimpleName()); + Callable<T> internalCall = new Callable<T>() + { + @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 AfterConnectionEstablished() + { + } +} http://git-wip-us.apache.org/repos/asf/curator/blob/1c94c7ef/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 deleted file mode 100644 index 408ed03..0000000 --- a/curator-recipes/src/main/java/org/apache/curator/framework/recipes/ExecuteAfterConnectionEstablished.java +++ /dev/null @@ -1,73 +0,0 @@ -/** - * 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 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 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(runAfterConnection.getClass().getSimpleName()); - Callable<T> internalCall = new Callable<T>() - { - @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() - { - } -}
