Author: davsclaus Date: Tue Mar 9 10:54:29 2010 New Revision: 920791 URL: http://svn.apache.org/viewvc?rev=920791&view=rev Log: CAMEL-1588: thread name can be set using a pattern like syntax. Camel now logs at INFO level if stream cache, tracer etc. is enabled.
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceStrategyTest.java (with props) Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceStrategy.java camel/trunk/camel-core/src/main/java/org/apache/camel/processor/resequencer/ResequencerEngine.java camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceStrategy.java camel/trunk/camel-core/src/main/java/org/apache/camel/util/concurrent/ExecutorServiceHelper.java Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java?rev=920791&r1=920790&r2=920791&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultCamelContext.java Tue Mar 9 10:54:29 2010 @@ -1101,22 +1101,20 @@ public class DefaultCamelContext extends if (isStreamCaching()) { // only add a new stream cache if not already configured if (StreamCaching.getStreamCaching(this) == null) { - if (LOG.isDebugEnabled()) { - LOG.debug("StreamCaching is enabled"); - } + LOG.info("StreamCaching is enabled on CamelContext" + getName()); addInterceptStrategy(new StreamCaching()); } } if (isTracing()) { // tracing is added in the DefaultChannel so we can enable it on the fly - LOG.debug("Tracing is enabled"); + LOG.info("Tracing is enabled on CamelContext" + getName()); } if (isHandleFault()) { // only add a new handle fault if not already configured if (HandleFault.getHandleFault(this) == null) { - LOG.debug("HandleFault is enabled"); + LOG.info("HandleFault is enabled on CamelContext" + getName()); addInterceptStrategy(new HandleFault()); } } @@ -1125,7 +1123,7 @@ public class DefaultCamelContext extends // only add a new delayer if not already configured if (Delayer.getDelayer(this) == null) { long millis = getDelayer(); - LOG.debug("Delayer is enabled with: " + millis + " ms."); + LOG.info("Delayer is enabled with: " + millis + " ms. on CamelContext" + getName()); addInterceptStrategy(new Delayer(millis)); } } Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceStrategy.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceStrategy.java?rev=920791&r1=920790&r2=920791&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceStrategy.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceStrategy.java Tue Mar 9 10:54:29 2010 @@ -31,41 +31,50 @@ import org.apache.camel.util.concurrent. public class DefaultExecutorServiceStrategy implements ExecutorServiceStrategy { private final CamelContext camelContext; + private String threadNamePattern = "Camel Thread ${counter} - ${name}"; public DefaultExecutorServiceStrategy(CamelContext camelContext) { this.camelContext = camelContext; } - public String getThreadName(String nameSuffix) { - return ExecutorServiceHelper.getThreadName(nameSuffix); + public String getThreadName(String name) { + return ExecutorServiceHelper.getThreadName(threadNamePattern, name); + } + + public String getThreadNamePattern() { + return threadNamePattern; + } + + public void setThreadNamePattern(String threadNamePattern) { + this.threadNamePattern = threadNamePattern; } public ExecutorService lookup(String executorServiceRef) { return camelContext.getRegistry().lookup(executorServiceRef, ExecutorService.class); } - public ExecutorService newCachedThreadPool(String nameSuffix) { - return ExecutorServiceHelper.newCachedThreadPool(nameSuffix, true); + public ExecutorService newCachedThreadPool(String name) { + return ExecutorServiceHelper.newCachedThreadPool(getThreadName(name), true); } - public ScheduledExecutorService newScheduledThreadPool(String nameSuffix, int poolSize) { - return ExecutorServiceHelper.newScheduledThreadPool(poolSize, nameSuffix, true); + public ScheduledExecutorService newScheduledThreadPool(String name, int poolSize) { + return ExecutorServiceHelper.newScheduledThreadPool(poolSize, getThreadName(name), true); } - public ExecutorService newFixedThreadPool(String nameSuffix, int poolSize) { - return ExecutorServiceHelper.newFixedThreadPool(poolSize, nameSuffix, true); + public ExecutorService newFixedThreadPool(String name, int poolSize) { + return ExecutorServiceHelper.newFixedThreadPool(poolSize, getThreadName(name), true); } - public ExecutorService newSingleThreadExecutor(String nameSuffix) { - return ExecutorServiceHelper.newSingleThreadExecutor(nameSuffix, true); + public ExecutorService newSingleThreadExecutor(String name) { + return ExecutorServiceHelper.newSingleThreadExecutor(getThreadName(name), true); } - public ExecutorService newThreadPool(String nameSuffix, int corePoolSize, int maxPoolSize) { - return ExecutorServiceHelper.newThreadPool(nameSuffix, corePoolSize, maxPoolSize); + public ExecutorService newThreadPool(String name, int corePoolSize, int maxPoolSize) { + return ExecutorServiceHelper.newThreadPool(getThreadName(name), corePoolSize, maxPoolSize); } - public ExecutorService newThreadPool(String nameSuffix, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, boolean daemon) { - return ExecutorServiceHelper.newThreadPool(nameSuffix, corePoolSize, maxPoolSize, keepAliveTime, timeUnit, daemon); + public ExecutorService newThreadPool(String name, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, boolean daemon) { + return ExecutorServiceHelper.newThreadPool(getThreadName(name), corePoolSize, maxPoolSize, keepAliveTime, timeUnit, daemon); } public void shutdown(ExecutorService executorService) { Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/resequencer/ResequencerEngine.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/resequencer/ResequencerEngine.java?rev=920791&r1=920790&r2=920791&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/resequencer/ResequencerEngine.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/resequencer/ResequencerEngine.java Tue Mar 9 10:54:29 2010 @@ -99,7 +99,7 @@ public class ResequencerEngine<E> { } public void start() { - timer = new Timer(ExecutorServiceHelper.getThreadName("Stream Resequencer Timer"), true); + timer = new Timer(ExecutorServiceHelper.getThreadName("Camel Thread ${counter} - ${suffix}", "Stream Resequencer Timer"), true); } /** Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceStrategy.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceStrategy.java?rev=920791&r1=920790&r2=920791&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceStrategy.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceStrategy.java Tue Mar 9 10:54:29 2010 @@ -39,10 +39,30 @@ public interface ExecutorServiceStrategy /** * Creates a full thread name * - * @param nameSuffix suffix which is appended to the thread name + * @param name name which is appended to the full thread name * @return the full thread name */ - String getThreadName(String nameSuffix); + String getThreadName(String name); + + /** + * Gets the thread name pattern used for creating the full thread name. + * + * @return the pattern + */ + String getThreadNamePattern(); + + /** + * Sets the thread name pattern used for creating the full thread name. + * <p/> + * The default pattern is: <tt>Camel Thread ${counter} - ${suffix}</tt> + * </br> + * Where <tt>${counter}</tt> is a unique incrementing counter. + * And <tt>${name}</tt> is the thread name. + * + * @param pattern the pattern + * @throws IllegalArgumentException if the pattern is invalid. + */ + void setThreadNamePattern(String pattern) throws IllegalArgumentException; /** * Lookup a {...@link java.util.concurrent.ExecutorService} from the {...@link org.apache.camel.spi.Registry}. @@ -55,53 +75,53 @@ public interface ExecutorServiceStrategy /** * Creates a new cached thread pool. * - * @param nameSuffix suffix which is appended to the thread name + * @param name name which is appended to the thread name * @return the thread pool */ - ExecutorService newCachedThreadPool(String nameSuffix); + ExecutorService newCachedThreadPool(String name); /** * Creates a new scheduled thread pool. * - * @param nameSuffix suffix which is appended to the thread name + * @param name name which is appended to the thread name * @param poolSize the core pool size * @return the thread pool */ - ScheduledExecutorService newScheduledThreadPool(String nameSuffix, int poolSize); + ScheduledExecutorService newScheduledThreadPool(String name, int poolSize); /** * Creates a new fixed thread pool. * - * @param nameSuffix suffix which is appended to the thread name + * @param name name which is appended to the thread name * @param poolSize the core pool size * @return the thread pool */ - ExecutorService newFixedThreadPool(String nameSuffix, int poolSize); + ExecutorService newFixedThreadPool(String name, int poolSize); /** * Creates a new single-threaded thread pool. This is often used for background threads. * - * @param nameSuffix suffix which is appended to the thread name + * @param name name which is appended to the thread name * @return the thread pool */ - ExecutorService newSingleThreadExecutor(String nameSuffix); + ExecutorService newSingleThreadExecutor(String name); /** * Creates a new custom thread pool. * <p/> * Will by default use 60 seconds for keep alive time for idle threads. * - * @param nameSuffix suffix which is appended to the thread name + * @param name name which is appended to the thread name * @param corePoolSize the core pool size * @param maxPoolSize the maximum pool size * @return the thread pool */ - ExecutorService newThreadPool(String nameSuffix, int corePoolSize, int maxPoolSize); + ExecutorService newThreadPool(String name, int corePoolSize, int maxPoolSize); /** * Creates a new custom thread pool. * - * @param nameSuffix suffix which is appended to the thread name + * @param name name which is appended to the thread name * @param corePoolSize the core pool size * @param maxPoolSize the maximum pool size * @param keepAliveTime keep alive time for idle threads @@ -109,7 +129,7 @@ public interface ExecutorServiceStrategy * @param daemon whether or not the created threads is daemon or not * @return the thread pool */ - ExecutorService newThreadPool(final String nameSuffix, int corePoolSize, int maxPoolSize, + ExecutorService newThreadPool(final String name, int corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, boolean daemon); /** Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/concurrent/ExecutorServiceHelper.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/concurrent/ExecutorServiceHelper.java?rev=920791&r1=920790&r2=920791&view=diff ============================================================================== --- camel/trunk/camel-core/src/main/java/org/apache/camel/util/concurrent/ExecutorServiceHelper.java (original) +++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/concurrent/ExecutorServiceHelper.java Tue Mar 9 10:54:29 2010 @@ -40,18 +40,25 @@ public final class ExecutorServiceHelper private ExecutorServiceHelper() { } + private static synchronized int nextThreadCounter() { + return threadCounter.getAndIncrement(); + } + /** * Creates a new thread name with the given prefix * - * @param name the prefix + * @param pattern the pattern + * @param name the name * @return the thread name, which is unique */ - public static String getThreadName(String name) { - return "Camel thread " + nextThreadCounter() + ": " + name; - } + public static String getThreadName(String pattern, String name) { + String answer = pattern.replaceFirst("\\$\\{counter\\}", "" + nextThreadCounter()); + answer = answer.replaceFirst("\\$\\{name\\}", name); + if (answer.indexOf("$") > -1 || answer.indexOf("${") > -1 || answer.indexOf("}") > -1) { + throw new IllegalArgumentException("Pattern is invalid: " + pattern); + } - protected static synchronized int nextThreadCounter() { - return threadCounter.getAndIncrement(); + return answer; } /** @@ -65,7 +72,7 @@ public final class ExecutorServiceHelper public static ScheduledExecutorService newScheduledThreadPool(final int poolSize, final String name, final boolean daemon) { return Executors.newScheduledThreadPool(poolSize, new ThreadFactory() { public Thread newThread(Runnable r) { - Thread answer = new Thread(r, getThreadName(name)); + Thread answer = new Thread(r, name); answer.setDaemon(daemon); return answer; } @@ -75,7 +82,7 @@ public final class ExecutorServiceHelper public static ExecutorService newFixedThreadPool(final int poolSize, final String name, final boolean daemon) { return Executors.newFixedThreadPool(poolSize, new ThreadFactory() { public Thread newThread(Runnable r) { - Thread answer = new Thread(r, getThreadName(name)); + Thread answer = new Thread(r, name); answer.setDaemon(daemon); return answer; } @@ -85,7 +92,7 @@ public final class ExecutorServiceHelper public static ExecutorService newSingleThreadExecutor(final String name, final boolean daemon) { return Executors.newSingleThreadExecutor(new ThreadFactory() { public Thread newThread(Runnable r) { - Thread answer = new Thread(r, getThreadName(name)); + Thread answer = new Thread(r, name); answer.setDaemon(daemon); return answer; } @@ -95,14 +102,14 @@ public final class ExecutorServiceHelper /** * Creates a new cached thread pool which should be the most commonly used. * - * @param name part of the thread name + * @param name the full thread name * @param daemon whether the threads is daemon or not * @return the created pool */ public static ExecutorService newCachedThreadPool(final String name, final boolean daemon) { return Executors.newCachedThreadPool(new ThreadFactory() { public Thread newThread(Runnable r) { - Thread answer = new Thread(r, getThreadName(name)); + Thread answer = new Thread(r, name); answer.setDaemon(daemon); return answer; } @@ -112,7 +119,7 @@ public final class ExecutorServiceHelper /** * Creates a new custom thread pool using 60 seconds as keep alive * - * @param name part of the thread name + * @param name the full thread name * @param corePoolSize the core size * @param maxPoolSize the maximum pool size * @return the created pool @@ -124,7 +131,7 @@ public final class ExecutorServiceHelper /** * Creates a new custom thread pool * - * @param name part of the thread name + * @param name the full thread name * @param corePoolSize the core size * @param maxPoolSize the maximum pool size * @param keepAliveTime keep alive @@ -145,7 +152,7 @@ public final class ExecutorServiceHelper keepAliveTime, timeUnit, new LinkedBlockingQueue<Runnable>()); answer.setThreadFactory(new ThreadFactory() { public Thread newThread(Runnable r) { - Thread answer = new Thread(r, getThreadName(name)); + Thread answer = new Thread(r, name); answer.setDaemon(daemon); return answer; } Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceStrategyTest.java URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceStrategyTest.java?rev=920791&view=auto ============================================================================== --- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceStrategyTest.java (added) +++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceStrategyTest.java Tue Mar 9 10:54:29 2010 @@ -0,0 +1,71 @@ +/** + * 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.camel.impl; + +import org.apache.camel.ContextTestSupport; + +/** + * @version $Revision$ + */ +public class DefaultExecutorServiceStrategyTest extends ContextTestSupport { + + public void testGetThreadName() throws Exception { + String foo = context.getExecutorServiceStrategy().getThreadName("foo"); + String bar = context.getExecutorServiceStrategy().getThreadName("bar"); + + assertNotSame(foo, bar); + assertTrue(foo.startsWith("Camel Thread ")); + assertTrue(foo.endsWith("foo")); + assertTrue(bar.startsWith("Camel Thread ")); + assertTrue(bar.endsWith("bar")); + } + + public void testGetThreadNameCustomPattern() throws Exception { + context.getExecutorServiceStrategy().setThreadNamePattern("#${counter} - ${name}"); + String foo = context.getExecutorServiceStrategy().getThreadName("foo"); + String bar = context.getExecutorServiceStrategy().getThreadName("bar"); + + assertNotSame(foo, bar); + assertTrue(foo.startsWith("#")); + assertTrue(foo.endsWith(" - foo")); + assertTrue(bar.startsWith("#")); + assertTrue(bar.endsWith(" - bar")); + } + + public void testGetThreadNameCustomPatternNoCounter() throws Exception { + context.getExecutorServiceStrategy().setThreadNamePattern("Cool ${name}"); + String foo = context.getExecutorServiceStrategy().getThreadName("foo"); + String bar = context.getExecutorServiceStrategy().getThreadName("bar"); + + assertNotSame(foo, bar); + assertEquals("Cool foo", foo); + assertEquals("Cool bar", bar); + } + + public void testGetThreadNameCustomPatternInvalid() throws Exception { + context.getExecutorServiceStrategy().setThreadNamePattern("Cool ${xxx}"); + try { + context.getExecutorServiceStrategy().getThreadName("foo"); + fail("Should thrown an exception"); + } catch (IllegalArgumentException e) { + assertEquals("Pattern is invalid: Cool ${xxx}", e.getMessage()); + } + + // reset it so we can shutdown properly + context.getExecutorServiceStrategy().setThreadNamePattern("Camel Thread ${counter} - ${name}"); + } +} Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceStrategyTest.java ------------------------------------------------------------------------------ svn:eol-style = native Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceStrategyTest.java ------------------------------------------------------------------------------ svn:keywords = Rev Date