Author: davsclaus
Date: Mon Mar 15 09:56:27 2010
New Revision: 923141

URL: http://svn.apache.org/viewvc?rev=923141&view=rev
Log:
CAMEL-1588: Fixed creating thread name based on pattern.

Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceStrategy.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/util/concurrent/ExecutorServiceHelper.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateShutdownThreadPoolTest.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/util/DefaultTimeoutMapTest.java

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=923141&r1=923140&r2=923141&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
 Mon Mar 15 09:56:27 2010
@@ -54,27 +54,27 @@ public class DefaultExecutorServiceStrat
     }
 
     public ExecutorService newCachedThreadPool(Object source, String name) {
-        return ExecutorServiceHelper.newCachedThreadPool(getThreadName(name), 
true);
+        return ExecutorServiceHelper.newCachedThreadPool(threadNamePattern, 
name, true);
     }
 
     public ScheduledExecutorService newScheduledThreadPool(Object source, 
String name, int poolSize) {
-        return ExecutorServiceHelper.newScheduledThreadPool(poolSize, 
getThreadName(name), true);
+        return ExecutorServiceHelper.newScheduledThreadPool(poolSize, 
threadNamePattern, name, true);
     }
 
     public ExecutorService newFixedThreadPool(Object source, String name, int 
poolSize) {
-        return ExecutorServiceHelper.newFixedThreadPool(poolSize, 
getThreadName(name), true);
+        return ExecutorServiceHelper.newFixedThreadPool(poolSize, 
threadNamePattern, name, true);
     }
 
     public ExecutorService newSingleThreadExecutor(Object source, String name) 
{
-        return 
ExecutorServiceHelper.newSingleThreadExecutor(getThreadName(name), true);
+        return 
ExecutorServiceHelper.newSingleThreadExecutor(threadNamePattern, name, true);
     }
 
     public ExecutorService newThreadPool(Object source, String name, int 
corePoolSize, int maxPoolSize) {
-        return ExecutorServiceHelper.newThreadPool(getThreadName(name), 
corePoolSize, maxPoolSize);
+        return ExecutorServiceHelper.newThreadPool(threadNamePattern, name, 
corePoolSize, maxPoolSize);
     }
 
     public ExecutorService newThreadPool(Object source, String name, int 
corePoolSize, int maxPoolSize, long keepAliveTime, TimeUnit timeUnit, boolean 
daemon) {
-        return ExecutorServiceHelper.newThreadPool(getThreadName(name), 
corePoolSize, maxPoolSize, keepAliveTime, timeUnit, daemon);
+        return ExecutorServiceHelper.newThreadPool(threadNamePattern, name, 
corePoolSize, maxPoolSize, keepAliveTime, timeUnit, daemon);
     }
 
     public void shutdown(ExecutorService executorService) {

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=923141&r1=923140&r2=923141&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
 Mon Mar 15 09:56:27 2010
@@ -44,6 +44,7 @@ import org.apache.camel.util.ObjectHelpe
  */
 public final class ExecutorServiceHelper {
 
+    public static final String DEFAULT_PATTERN = "Camel Thread ${counter} - 
${name}";
     private static AtomicInteger threadCounter = new AtomicInteger();
 
     private ExecutorServiceHelper() {
@@ -61,6 +62,10 @@ public final class ExecutorServiceHelper
      * @return the thread name, which is unique
      */
     public static String getThreadName(String pattern, String name) {
+        if (pattern == null) {
+            pattern = DEFAULT_PATTERN;
+        }
+
         String answer = pattern.replaceFirst("\\$\\{counter\\}", ""  + 
nextThreadCounter());
         answer = answer.replaceFirst("\\$\\{name\\}", name);
         if (answer.indexOf("$") > -1 || answer.indexOf("${") > -1 || 
answer.indexOf("}") > -1) {
@@ -74,34 +79,52 @@ public final class ExecutorServiceHelper
      * Creates a new scheduled thread pool which can schedule threads.
      *
      * @param poolSize the core pool size
-     * @param name     part of the thread name
+     * @param pattern  pattern of the thread name
+     * @param name     ${name} in the pattern name
      * @param daemon   whether the threads is daemon or not
      * @return the created pool
      */
-    public static ScheduledExecutorService newScheduledThreadPool(final int 
poolSize, final String name, final boolean daemon) {
+    public static ScheduledExecutorService newScheduledThreadPool(final int 
poolSize, final String pattern, final String name, final boolean daemon) {
         return Executors.newScheduledThreadPool(poolSize, new ThreadFactory() {
             public Thread newThread(Runnable r) {
-                Thread answer = new Thread(r, name);
+                Thread answer = new Thread(r, getThreadName(pattern, name));
                 answer.setDaemon(daemon);
                 return answer;
             }
         });
     }
 
-    public static ExecutorService newFixedThreadPool(final int poolSize, final 
String name, final boolean daemon) {
+    /**
+     * Creates a new fixed thread pool
+     *
+     * @param poolSize the fixed pool size
+     * @param pattern  pattern of the thread name
+     * @param name     ${name} in the pattern name
+     * @param daemon   whether the threads is daemon or not
+     * @return the created pool
+     */
+    public static ExecutorService newFixedThreadPool(final int poolSize, final 
String pattern, final String name, final boolean daemon) {
         return Executors.newFixedThreadPool(poolSize, new ThreadFactory() {
             public Thread newThread(Runnable r) {
-                Thread answer = new Thread(r, name);
+                Thread answer = new Thread(r, getThreadName(pattern, name));
                 answer.setDaemon(daemon);
                 return answer;
             }
         });
     }
 
-    public static ExecutorService newSingleThreadExecutor(final String name, 
final boolean daemon) {
+    /**
+     * Creates a new single thread pool (usually for background tasks)
+     *
+     * @param pattern  pattern of the thread name
+     * @param name     ${name} in the pattern name
+     * @param daemon   whether the threads is daemon or not
+     * @return the created pool
+     */
+    public static ExecutorService newSingleThreadExecutor(final String 
pattern, final String name, final boolean daemon) {
         return Executors.newSingleThreadExecutor(new ThreadFactory() {
             public Thread newThread(Runnable r) {
-                Thread answer = new Thread(r, name);
+                Thread answer = new Thread(r, getThreadName(pattern, name));
                 answer.setDaemon(daemon);
                 return answer;
             }
@@ -111,14 +134,15 @@ public final class ExecutorServiceHelper
     /**
      * Creates a new cached thread pool which should be the most commonly used.
      *
-     * @param name    the full thread name
-     * @param daemon  whether the threads is daemon or not
+     * @param pattern  pattern of the thread name
+     * @param name     ${name} in the pattern name
+     * @param daemon   whether the threads is daemon or not
      * @return the created pool
      */
-    public static ExecutorService newCachedThreadPool(final String name, final 
boolean daemon) {
+    public static ExecutorService newCachedThreadPool(final String pattern, 
final String name, final boolean daemon) {
         return Executors.newCachedThreadPool(new ThreadFactory() {
             public Thread newThread(Runnable r) {
-                Thread answer = new Thread(r, name);
+                Thread answer = new Thread(r, getThreadName(pattern, name));
                 answer.setDaemon(daemon);
                 return answer;
             }
@@ -128,19 +152,21 @@ public final class ExecutorServiceHelper
     /**
      * Creates a new custom thread pool using 60 seconds as keep alive
      *
-     * @param name          the full thread name
+     * @param pattern       pattern of the thread name
+     * @param name          ${name} in the pattern name
      * @param corePoolSize  the core size
      * @param maxPoolSize   the maximum pool size
      * @return the created pool
      */
-    public static ExecutorService newThreadPool(final String name, int 
corePoolSize, int maxPoolSize) {
-        return ExecutorServiceHelper.newThreadPool(name, corePoolSize, 
maxPoolSize, 60, TimeUnit.SECONDS, true);
+    public static ExecutorService newThreadPool(final String pattern, final 
String name, int corePoolSize, int maxPoolSize) {
+        return ExecutorServiceHelper.newThreadPool(pattern, name, 
corePoolSize, maxPoolSize, 60, TimeUnit.SECONDS, true);
     }
 
     /**
      * Creates a new custom thread pool
      *
-     * @param name          the full thread name
+     * @param pattern       pattern of the thread name
+     * @param name          ${name} in the pattern name
      * @param corePoolSize  the core size
      * @param maxPoolSize   the maximum pool size
      * @param keepAliveTime keep alive
@@ -149,7 +175,7 @@ public final class ExecutorServiceHelper
      * @return the created pool
      * @throws IllegalArgumentException if parameters is not valid
      */
-    public static ExecutorService newThreadPool(final String name, int 
corePoolSize, int maxPoolSize,
+    public static ExecutorService newThreadPool(final String pattern, final 
String name, int corePoolSize, int maxPoolSize,
                                                 long keepAliveTime, TimeUnit 
timeUnit, final boolean daemon) {
 
         // validate max >= core
@@ -161,7 +187,7 @@ public final class ExecutorServiceHelper
                                                            keepAliveTime, 
timeUnit, new LinkedBlockingQueue<Runnable>());
         answer.setThreadFactory(new ThreadFactory() {
             public Thread newThread(Runnable r) {
-                Thread answer = new Thread(r, name);
+                Thread answer = new Thread(r, getThreadName(pattern, name));
                 answer.setDaemon(daemon);
                 return answer;
             }

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateShutdownThreadPoolTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateShutdownThreadPoolTest.java?rev=923141&r1=923140&r2=923141&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateShutdownThreadPoolTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregateShutdownThreadPoolTest.java
 Mon Mar 15 09:56:27 2010
@@ -28,7 +28,7 @@ import org.apache.camel.util.concurrent.
  */
 public class AggregateShutdownThreadPoolTest extends ContextTestSupport {
 
-    private ExecutorService myPool = 
ExecutorServiceHelper.newCachedThreadPool("myPool", true);
+    private ExecutorService myPool = 
ExecutorServiceHelper.newCachedThreadPool(null, "myPool", true);
 
     public void testAggregateShutdownDefaultThreadPoolTest() throws Exception {
         getMockEndpoint("mock:aggregated").expectedBodiesReceived("A+B+C");

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/DefaultTimeoutMapTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/util/DefaultTimeoutMapTest.java?rev=923141&r1=923140&r2=923141&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/DefaultTimeoutMapTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/util/DefaultTimeoutMapTest.java
 Mon Mar 15 09:56:27 2010
@@ -104,7 +104,7 @@ public class DefaultTimeoutMapTest exten
     }
 
     public void testExecutor() throws Exception {
-        ScheduledExecutorService e = 
ExecutorServiceHelper.newScheduledThreadPool(2, "foo", true);
+        ScheduledExecutorService e = 
ExecutorServiceHelper.newScheduledThreadPool(2, null, "foo", true);
 
         DefaultTimeoutMap map = new DefaultTimeoutMap(e, 500);
         assertEquals(500, map.getPurgePollTime());


Reply via email to