Author: davsclaus
Date: Fri Aug 12 16:42:19 2011
New Revision: 1157183

URL: http://svn.apache.org/viewvc?rev=1157183&view=rev
Log:
CAMEL-4298: Lookup methods on ExecutorServiceManager no longer needed. Added 
custom ThreadPoolFactory test, and possible to configure in XML DSL.

Added:
    
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomThreadPoolFactoryTest.java
    
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomThreadPoolFactoryTest.java
      - copied, changed from r1157158, 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomExecutorServiceManagerTest.java
    
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomThreadPoolFactoryTest-context.xml
      - copied, changed from r1157158, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomExecutorServiceManagerTest-context.xml
Modified:
    
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
    
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceManager.java
    
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
    
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java?rev=1157183&r1=1157182&r2=1157183&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultExecutorServiceManager.java
 Fri Aug 12 16:42:19 2011
@@ -154,7 +154,6 @@ public class DefaultExecutorServiceManag
     @Override
     public void setThreadNamePattern(String threadNamePattern) {
         // must set camel id here in the pattern and let the other 
placeholders be resolved on demand
-        // TODO: Let ThreadHelper do this on demand
         String name = threadNamePattern.replaceFirst("\\$\\{camelId\\}", 
this.camelContext.getName());
         this.threadNamePattern = name;
     }
@@ -164,52 +163,6 @@ public class DefaultExecutorServiceManag
         return ThreadHelper.resolveThreadName(threadNamePattern, name);
     }
 
-    // TODO: The lookup methods could possible be removed and replace using 
other methods/logic
-
-    @Override
-    public ExecutorService lookup(Object source, String name, String 
executorServiceRef) {
-        ExecutorService answer = 
camelContext.getRegistry().lookup(executorServiceRef, ExecutorService.class);
-        if (answer != null) {
-            LOG.debug("Looking up ExecutorService with ref: {} and found it 
from Registry: {}", executorServiceRef, answer);
-        }
-
-        if (answer == null) {
-            // try to see if we got a thread pool profile with that id
-            answer = newThreadPool(source, name, executorServiceRef);
-            if (answer != null) {
-                LOG.debug("Looking up ExecutorService with ref: {} and found a 
matching ThreadPoolProfile to create the ExecutorService: {}",
-                        executorServiceRef, answer);
-            }
-        }
-
-        return answer;
-    }
-
-    @Override
-    public ScheduledExecutorService lookupScheduled(Object source, String 
name, String executorServiceRef) {
-        ScheduledExecutorService answer = 
camelContext.getRegistry().lookup(executorServiceRef, 
ScheduledExecutorService.class);
-        if (answer != null) {
-            LOG.debug("Looking up ScheduledExecutorService with ref: {} and 
found it from Registry: {}", executorServiceRef, answer);
-        }
-
-        if (answer == null) {
-            ThreadPoolProfile profile = 
getThreadPoolProfile(executorServiceRef);
-            if (profile != null) {
-                Integer poolSize = profile.getPoolSize();
-                if (poolSize == null) {
-                    poolSize = getDefaultThreadPoolProfile().getPoolSize();
-                }
-                answer = newScheduledThreadPool(source, name, poolSize);
-                if (answer != null) {
-                    LOG.debug("Looking up ScheduledExecutorService with ref: 
{} and found a matching ThreadPoolProfile to create the 
ScheduledExecutorService: {}",
-                            executorServiceRef, answer);
-                }
-            }
-        }
-
-        return answer;
-    }
-
     @Override
     public ExecutorService newDefaultThreadPool(Object source, String name) {
         return newThreadPool(source, name, getDefaultThreadPoolProfile());

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java?rev=1157183&r1=1157182&r2=1157183&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorDefinitionHelper.java
 Fri Aug 12 16:42:19 2011
@@ -25,6 +25,7 @@ import java.util.concurrent.ScheduledExe
 import org.apache.camel.NoSuchBeanException;
 import org.apache.camel.spi.ExecutorServiceManager;
 import org.apache.camel.spi.RouteContext;
+import org.apache.camel.spi.ThreadPoolProfile;
 import org.apache.camel.util.ObjectHelper;
 
 /**
@@ -234,7 +235,12 @@ public final class ProcessorDefinitionHe
         if (definition.getExecutorService() != null) {
             return definition.getExecutorService();
         } else if (definition.getExecutorServiceRef() != null) {
-            ExecutorService answer = manager.lookup(definition, name, 
definition.getExecutorServiceRef());
+            // lookup in registry first and use existing thread pool if exists
+            ExecutorService answer = 
routeContext.getCamelContext().getRegistry().lookup(definition.getExecutorServiceRef(),
 ExecutorService.class);
+            if (answer == null) {
+                // then create a thread pool assuming the ref is a thread pool 
profile id
+                answer = manager.newThreadPool(definition, name, 
definition.getExecutorServiceRef());
+            }
             if (answer == null) {
                 throw new 
NoSuchBeanException(definition.getExecutorServiceRef(), "ExecutorService");
             }
@@ -278,7 +284,20 @@ public final class ProcessorDefinitionHe
             }
             throw new IllegalArgumentException("ExecutorServiceRef " + 
definition.getExecutorServiceRef() + " is not an ScheduledExecutorService 
instance");
         } else if (definition.getExecutorServiceRef() != null) {
-            ScheduledExecutorService answer = 
manager.lookupScheduled(definition, name, definition.getExecutorServiceRef());
+            ScheduledExecutorService answer = 
routeContext.getCamelContext().getRegistry().lookup(definition.getExecutorServiceRef(),
 ScheduledExecutorService.class);
+            if (answer == null) {
+                // then create a thread pool assuming the ref is a thread pool 
profile id
+                ThreadPoolProfile profile = 
manager.getThreadPoolProfile(definition.getExecutorServiceRef());
+                if (profile != null) {
+                    // okay we need to grab the pool size from the ref
+                    Integer poolSize = profile.getPoolSize();
+                    if (poolSize == null) {
+                        // fallback and use the default pool size, if none was 
set on the profile
+                        poolSize = 
manager.getDefaultThreadPoolProfile().getPoolSize();
+                    }
+                    answer = manager.newScheduledThreadPool(definition, name, 
poolSize);
+                }
+            }
             if (answer == null) {
                 throw new 
NoSuchBeanException(definition.getExecutorServiceRef(), 
"ScheduledExecutorService");
             }

Modified: 
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceManager.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceManager.java?rev=1157183&r1=1157182&r2=1157183&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceManager.java
 (original)
+++ 
camel/trunk/camel-core/src/main/java/org/apache/camel/spi/ExecutorServiceManager.java
 Fri Aug 12 16:42:19 2011
@@ -121,28 +121,6 @@ public interface ExecutorServiceManager 
     String getThreadNamePattern();
 
     /**
-     * Lookup a {@link java.util.concurrent.ExecutorService} from the {@link 
org.apache.camel.spi.Registry}
-     * and from known list of {@link org.apache.camel.spi.ThreadPoolProfile 
ThreadPoolProfile(s)}.
-     *
-     * @param source               the source object, usually it should be 
<tt>this</tt> passed in as parameter
-     * @param name                 name which is appended to the thread name
-     * @param executorServiceRef   reference to lookup
-     * @return the {@link java.util.concurrent.ExecutorService} or 
<tt>null</tt> if not found
-     */
-    ExecutorService lookup(Object source, String name, String 
executorServiceRef);
-
-    /**
-     * Lookup a {@link java.util.concurrent.ScheduledExecutorService} from the 
{@link org.apache.camel.spi.Registry}
-     * and from known list of {@link org.apache.camel.spi.ThreadPoolProfile 
ThreadPoolProfile(s)}.
-     *
-     * @param source               the source object, usually it should be 
<tt>this</tt> passed in as parameter
-     * @param name                 name which is appended to the thread name
-     * @param executorServiceRef   reference to lookup
-     * @return the {@link java.util.concurrent.ScheduledExecutorService} or 
<tt>null</tt> if not found
-     */
-    ScheduledExecutorService lookupScheduled(Object source, String name, 
String executorServiceRef);
-
-    /**
      * Creates a new thread pool using the default thread pool profile.
      *
      * @param source the source object, usually it should be <tt>this</tt> 
passed in as parameter

Added: 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomThreadPoolFactoryTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomThreadPoolFactoryTest.java?rev=1157183&view=auto
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomThreadPoolFactoryTest.java
 (added)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/CustomThreadPoolFactoryTest.java
 Fri Aug 12 16:42:19 2011
@@ -0,0 +1,79 @@
+/**
+ * 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 java.util.concurrent.ExecutorService;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.ContextTestSupport;
+
+/**
+ *
+ */
+public class CustomThreadPoolFactoryTest extends ContextTestSupport {
+
+    private MyCustomThreadPoolFactory factory = new 
MyCustomThreadPoolFactory();
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.getExecutorServiceManager().setThreadPoolFactory(factory);
+        return context;
+    }
+
+    public void testCustomThreadPoolFactory() {
+        context.getExecutorServiceManager().newSingleThreadExecutor(this, 
"foo");
+        assertTrue("Should use custom thread pool factory", 
factory.isInvoked());
+    }
+
+    public static final class MyCustomThreadPoolFactory extends 
DefaultThreadPoolFactory {
+
+        private volatile boolean invoked;
+
+        @Override
+        public ExecutorService newCachedThreadPool(ThreadFactory 
threadFactory) {
+            invoked = true;
+            return super.newCachedThreadPool(threadFactory);
+        }
+
+        @Override
+        public ExecutorService newFixedThreadPool(int poolSize, ThreadFactory 
threadFactory) {
+            invoked = true;
+            return super.newFixedThreadPool(poolSize, threadFactory);
+        }
+
+        @Override
+        public ScheduledExecutorService newScheduledThreadPool(int 
corePoolSize, ThreadFactory threadFactory) throws IllegalArgumentException {
+            invoked = true;
+            return super.newScheduledThreadPool(corePoolSize, threadFactory);
+        }
+
+        @Override
+        public ExecutorService newThreadPool(int corePoolSize, int 
maxPoolSize, long keepAliveTime, TimeUnit timeUnit, int maxQueueSize, 
RejectedExecutionHandler rejectedExecutionHandler, ThreadFactory threadFactory) 
throws IllegalArgumentException {
+            invoked = true;
+            return super.newThreadPool(corePoolSize, maxPoolSize, 
keepAliveTime, timeUnit, maxQueueSize, rejectedExecutionHandler, threadFactory);
+        }
+
+        public boolean isInvoked() {
+            return invoked;
+        }
+    }
+}

Modified: 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java?rev=1157183&r1=1157182&r2=1157183&view=diff
==============================================================================
--- 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
 (original)
+++ 
camel/trunk/camel-core/src/test/java/org/apache/camel/impl/DefaultExecutorServiceManagerTest.java
 Fri Aug 12 16:42:19 2011
@@ -17,6 +17,7 @@
 package org.apache.camel.impl;
 
 import java.util.concurrent.ExecutorService;
+import java.util.concurrent.ScheduledThreadPoolExecutor;
 import java.util.concurrent.ThreadPoolExecutor;
 import java.util.concurrent.TimeUnit;
 
@@ -296,9 +297,7 @@ public class DefaultExecutorServiceManag
         foo.setPoolSize(5);
         foo.setMaxQueueSize(2000);
 
-        context.getExecutorServiceManager().registerThreadPoolProfile(foo);
-
-        ExecutorService pool = 
context.getExecutorServiceManager().newThreadPool(this, "Cool", "foo");
+        ExecutorService pool = 
context.getExecutorServiceManager().newThreadPool(this, "Cool", foo);
         assertNotNull(pool);
 
         ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, 
pool);
@@ -312,14 +311,10 @@ public class DefaultExecutorServiceManag
         assertTrue(tp.isShutdown());
     }
 
-    public void testLookupThreadPoolProfile() throws Exception {
-        ExecutorService pool = 
context.getExecutorServiceManager().lookup(this, "Cool", "fooProfile");
-        // does not exists yet
-        assertNull(pool);
-
-        
assertNull(context.getExecutorServiceManager().getThreadPoolProfile("fooProfile"));
+    public void testNewThreadPoolProfileById() throws Exception {
+        
assertNull(context.getExecutorServiceManager().getThreadPoolProfile("foo"));
 
-        ThreadPoolProfile foo = new ThreadPoolProfile("fooProfile");
+        ThreadPoolProfile foo = new ThreadPoolProfile("foo");
         foo.setKeepAliveTime(20L);
         foo.setMaxPoolSize(40);
         foo.setPoolSize(5);
@@ -327,7 +322,7 @@ public class DefaultExecutorServiceManag
 
         context.getExecutorServiceManager().registerThreadPoolProfile(foo);
 
-        pool = context.getExecutorServiceManager().lookup(this, "Cool", 
"fooProfile");
+        ExecutorService pool = 
context.getExecutorServiceManager().newThreadPool(this, "Cool", "foo");
         assertNotNull(pool);
 
         ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, 
pool);
@@ -341,6 +336,98 @@ public class DefaultExecutorServiceManag
         assertTrue(tp.isShutdown());
     }
 
-    // TODO: Add unit test for the newXXX methods
+    public void testNewThreadPoolMinMax() throws Exception {
+        ExecutorService pool = 
context.getExecutorServiceManager().newThreadPool(this, "Cool", 5, 10);
+        assertNotNull(pool);
+
+        ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, 
pool);
+        assertEquals(60, tp.getKeepAliveTime(TimeUnit.SECONDS));
+        assertEquals(10, tp.getMaximumPoolSize());
+        assertEquals(5, tp.getCorePoolSize());
+        assertFalse(tp.isShutdown());
+
+        context.stop();
+
+        assertTrue(tp.isShutdown());
+    }
+
+    public void testNewFixedThreadPool() throws Exception {
+        ExecutorService pool = 
context.getExecutorServiceManager().newFixedThreadPool(this, "Cool", 5);
+        assertNotNull(pool);
+
+        ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, 
pool);
+        // a fixed dont use keep alive
+        assertEquals(0, tp.getKeepAliveTime(TimeUnit.SECONDS));
+        assertEquals(5, tp.getMaximumPoolSize());
+        assertEquals(5, tp.getCorePoolSize());
+        assertFalse(tp.isShutdown());
+
+        context.stop();
+
+        assertTrue(tp.isShutdown());
+    }
+
+    public void testNewSingleThreadExecutor() throws Exception {
+        ExecutorService pool = 
context.getExecutorServiceManager().newSingleThreadExecutor(this, "Cool");
+        assertNotNull(pool);
+
+        ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, 
pool);
+        // a single dont use keep alive
+        assertEquals(0, tp.getKeepAliveTime(TimeUnit.SECONDS));
+        assertEquals(1, tp.getMaximumPoolSize());
+        assertEquals(1, tp.getCorePoolSize());
+        assertFalse(tp.isShutdown());
+
+        context.stop();
+
+        assertTrue(tp.isShutdown());
+    }
+
+    public void testNewScheduledThreadPool() throws Exception {
+        ExecutorService pool = 
context.getExecutorServiceManager().newScheduledThreadPool(this, "Cool", 5);
+        assertNotNull(pool);
+
+        ScheduledThreadPoolExecutor tp = 
assertIsInstanceOf(ScheduledThreadPoolExecutor.class, pool);
+        // a scheduled dont use keep alive
+        assertEquals(0, tp.getKeepAliveTime(TimeUnit.SECONDS));
+        assertEquals(Integer.MAX_VALUE, tp.getMaximumPoolSize());
+        assertEquals(5, tp.getCorePoolSize());
+        assertFalse(tp.isShutdown());
+
+        context.stop();
+
+        assertTrue(tp.isShutdown());
+    }
+
+    public void testNewSingleThreadScheduledExecutor() throws Exception {
+        ExecutorService pool = 
context.getExecutorServiceManager().newSingleThreadScheduledExecutor(this, 
"Cool");
+        assertNotNull(pool);
+
+        ScheduledThreadPoolExecutor tp = 
assertIsInstanceOf(ScheduledThreadPoolExecutor.class, pool);
+        // a scheduled dont use keep alive
+        assertEquals(0, tp.getKeepAliveTime(TimeUnit.SECONDS));
+        assertEquals(Integer.MAX_VALUE, tp.getMaximumPoolSize());
+        assertEquals(1, tp.getCorePoolSize());
+        assertFalse(tp.isShutdown());
+
+        context.stop();
+
+        assertTrue(tp.isShutdown());
+    }
+
+    public void testNewCachedThreadPool() throws Exception {
+        ExecutorService pool = 
context.getExecutorServiceManager().newCachedThreadPool(this, "Cool");
+        assertNotNull(pool);
+
+        ThreadPoolExecutor tp = assertIsInstanceOf(ThreadPoolExecutor.class, 
pool);
+        assertEquals(60, tp.getKeepAliveTime(TimeUnit.SECONDS));
+        assertEquals(Integer.MAX_VALUE, tp.getMaximumPoolSize());
+        assertEquals(0, tp.getCorePoolSize());
+        assertFalse(tp.isShutdown());
+
+        context.stop();
+
+        assertTrue(tp.isShutdown());
+    }
 
 }

Modified: 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java?rev=1157183&r1=1157182&r2=1157183&view=diff
==============================================================================
--- 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
 (original)
+++ 
camel/trunk/components/camel-core-xml/src/main/java/org/apache/camel/core/xml/AbstractCamelContextFactoryBean.java
 Fri Aug 12 16:42:19 2011
@@ -77,6 +77,7 @@ import org.apache.camel.spi.PackageScanC
 import org.apache.camel.spi.PackageScanFilter;
 import org.apache.camel.spi.ProcessorFactory;
 import org.apache.camel.spi.ShutdownStrategy;
+import org.apache.camel.spi.ThreadPoolFactory;
 import org.apache.camel.spi.ThreadPoolProfile;
 import org.apache.camel.spi.UuidGenerator;
 import org.apache.camel.util.CamelContextHelper;
@@ -162,6 +163,11 @@ public abstract class AbstractCamelConte
             LOG.info("Using custom ExecutorServiceStrategy: " + 
executorServiceStrategy);
             getContext().setExecutorServiceManager(executorServiceStrategy);
         }
+        ThreadPoolFactory threadPoolFactory = 
getBeanForType(ThreadPoolFactory.class);
+        if (threadPoolFactory != null) {
+            LOG.info("Using custom ThreadPoolFactory: " + threadPoolFactory);
+            
getContext().getExecutorServiceManager().setThreadPoolFactory(threadPoolFactory);
+        }
         ProcessorFactory processorFactory = 
getBeanForType(ProcessorFactory.class);
         if (processorFactory != null) {
             LOG.info("Using custom ProcessorFactory: " + processorFactory);

Copied: 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomThreadPoolFactoryTest.java
 (from r1157158, 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomExecutorServiceManagerTest.java)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomThreadPoolFactoryTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomThreadPoolFactoryTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomExecutorServiceManagerTest.java&r1=1157158&r2=1157183&rev=1157183&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomExecutorServiceManagerTest.java
 (original)
+++ 
camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/config/CustomThreadPoolFactoryTest.java
 Fri Aug 12 16:42:19 2011
@@ -16,8 +16,15 @@
  */
 package org.apache.camel.spring.config;
 
+import java.util.concurrent.ExecutorService;
+import java.util.concurrent.RejectedExecutionHandler;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.ThreadFactory;
+import java.util.concurrent.TimeUnit;
+
 import junit.framework.Assert;
 import org.apache.camel.CamelContext;
+import org.apache.camel.impl.DefaultThreadPoolFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.test.context.ContextConfiguration;
 import 
org.springframework.test.context.junit38.AbstractJUnit38SpringContextTests;
@@ -26,13 +33,51 @@ import org.springframework.test.context.
  * @version 
  */
 @ContextConfiguration
-public class CustomExecutorServiceManagerTest extends 
AbstractJUnit38SpringContextTests {
+public class CustomThreadPoolFactoryTest extends 
AbstractJUnit38SpringContextTests {
 
     @Autowired
     protected CamelContext context;
 
-    public void testCustomExecutorService() throws Exception {
-        Assert.assertTrue(context.getExecutorServiceManager() instanceof 
CustomExecutorServiceManager);
+    public void testCustomThreadPoolFactory() throws Exception {
+        context.getExecutorServiceManager().newSingleThreadExecutor(this, 
"foo");
+        
Assert.assertTrue(context.getExecutorServiceManager().getThreadPoolFactory() 
instanceof MyCustomThreadPoolFactory);
+
+        MyCustomThreadPoolFactory factory = (MyCustomThreadPoolFactory) 
context.getExecutorServiceManager().getThreadPoolFactory();
+        assertTrue("Should use custom thread pool factory", 
factory.isInvoked());
     }
 
+    public static class MyCustomThreadPoolFactory extends 
DefaultThreadPoolFactory {
+
+        private volatile boolean invoked;
+
+        @Override
+        public ExecutorService newCachedThreadPool(ThreadFactory 
threadFactory) {
+            invoked = true;
+            return super.newCachedThreadPool(threadFactory);
+        }
+
+        @Override
+        public ExecutorService newFixedThreadPool(int poolSize, ThreadFactory 
threadFactory) {
+            invoked = true;
+            return super.newFixedThreadPool(poolSize, threadFactory);
+        }
+
+        @Override
+        public ScheduledExecutorService newScheduledThreadPool(int 
corePoolSize, ThreadFactory threadFactory) throws IllegalArgumentException {
+            invoked = true;
+            return super.newScheduledThreadPool(corePoolSize, threadFactory);
+        }
+
+        @Override
+        public ExecutorService newThreadPool(int corePoolSize, int 
maxPoolSize, long keepAliveTime, TimeUnit timeUnit, int maxQueueSize, 
RejectedExecutionHandler rejectedExecutionHandler, ThreadFactory threadFactory) 
throws IllegalArgumentException {
+            invoked = true;
+            return super.newThreadPool(corePoolSize, maxPoolSize, 
keepAliveTime, timeUnit, maxQueueSize, rejectedExecutionHandler, threadFactory);
+        }
+
+        public boolean isInvoked() {
+            return invoked;
+        }
+    }
+
+
 }
\ No newline at end of file

Copied: 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomThreadPoolFactoryTest-context.xml
 (from r1157158, 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomExecutorServiceManagerTest-context.xml)
URL: 
http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomThreadPoolFactoryTest-context.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomThreadPoolFactoryTest-context.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomExecutorServiceManagerTest-context.xml&r1=1157158&r2=1157183&rev=1157183&view=diff
==============================================================================
--- 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomExecutorServiceManagerTest-context.xml
 (original)
+++ 
camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/config/CustomThreadPoolFactoryTest-context.xml
 Fri Aug 12 16:42:19 2011
@@ -22,9 +22,7 @@
        http://camel.apache.org/schema/spring 
http://camel.apache.org/schema/spring/camel-spring.xsd
     ">
 
-    <bean id="myCustomPoolStrategy" depends-on="camel" 
class="org.apache.camel.spring.config.CustomExecutorServiceManager">
-        <constructor-arg index="0" ref="camel"/>
-    </bean>
+    <bean id="myCustomThreadPoolFactory" depends-on="camel" 
class="org.apache.camel.spring.config.CustomThreadPoolFactoryTest$MyCustomThreadPoolFactory"/>
 
     <camelContext id="camel" xmlns="http://camel.apache.org/schema/spring";>
         <jmxAgent id="jmxAgent" disabled="true"/>


Reply via email to