You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2011/01/31 10:19:41 UTC

svn commit: r1065534 - /camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java

Author: davsclaus
Date: Mon Jan 31 09:19:40 2011
New Revision: 1065534

URL: http://svn.apache.org/viewvc?rev=1065534&view=rev
Log:
CAMEL-3577: ProducerTemplate lazy creates thread pool on demand (only when asyncXXX is being used).

Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java?rev=1065534&r1=1065533&r2=1065534&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultProducerTemplate.java Mon Jan 31 09:19:40 2011
@@ -519,11 +519,7 @@ public class DefaultProducerTemplate ext
                 return requestBody(endpoint, body);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public <T> Future<T> asyncRequestBody(final Endpoint endpoint, final Object body, final Class<T> type) {
@@ -532,11 +528,7 @@ public class DefaultProducerTemplate ext
                 return requestBody(endpoint, body, type);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public Future<Object> asyncRequestBodyAndHeader(final Endpoint endpoint, final Object body, final String header,
@@ -546,11 +538,7 @@ public class DefaultProducerTemplate ext
                 return requestBodyAndHeader(endpoint, body, header, headerValue);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public <T> Future<T> asyncRequestBodyAndHeader(final Endpoint endpoint, final Object body, final String header,
@@ -560,11 +548,7 @@ public class DefaultProducerTemplate ext
                 return requestBodyAndHeader(endpoint, body, header, headerValue, type);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public Future<Object> asyncRequestBodyAndHeaders(final Endpoint endpoint, final Object body,
@@ -574,11 +558,7 @@ public class DefaultProducerTemplate ext
                 return requestBodyAndHeaders(endpoint, body, headers);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public <T> Future<T> asyncRequestBodyAndHeaders(final Endpoint endpoint, final Object body,
@@ -588,11 +568,7 @@ public class DefaultProducerTemplate ext
                 return requestBodyAndHeaders(endpoint, body, headers, type);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public Future<Exchange> asyncSend(final Endpoint endpoint, final Exchange exchange) {
@@ -601,11 +577,7 @@ public class DefaultProducerTemplate ext
                 return send(endpoint, exchange);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public Future<Exchange> asyncSend(final Endpoint endpoint, final Processor processor) {
@@ -614,11 +586,7 @@ public class DefaultProducerTemplate ext
                 return send(endpoint, processor);
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public Future<Object> asyncSendBody(final Endpoint endpoint, final Object body) {
@@ -629,11 +597,7 @@ public class DefaultProducerTemplate ext
                 return null;
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     private Future<Object> asyncCallback(final Endpoint endpoint, final ExchangePattern pattern, final Object body, final Synchronization onCompletion) {
@@ -660,11 +624,7 @@ public class DefaultProducerTemplate ext
                 }
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public Future<Exchange> asyncCallback(final Endpoint endpoint, final Exchange exchange, final Synchronization onCompletion) {
@@ -685,11 +645,7 @@ public class DefaultProducerTemplate ext
                 return exchange;
             }
         };
-
-        if (executor == null) {
-            throw new IllegalStateException("ProducerTemplate has not been started");
-        }
-        return executor.submit(task);
+        return getExecutorService().submit(task);
     }
 
     public Future<Exchange> asyncCallback(final Endpoint endpoint, final Processor processor, final Synchronization onCompletion) {
@@ -710,18 +666,35 @@ public class DefaultProducerTemplate ext
                 return answer;
             }
         };
+        return getExecutorService().submit(task);
+    }
 
-        if (executor == null) {
+    private ProducerCache getProducerCache() {
+        if (!isStarted()) {
             throw new IllegalStateException("ProducerTemplate has not been started");
         }
-        return executor.submit(task);
+        return producerCache;
     }
 
-    private ProducerCache getProducerCache() {
+    private ExecutorService getExecutorService() {
         if (!isStarted()) {
             throw new IllegalStateException("ProducerTemplate has not been started");
         }
-        return producerCache;
+
+        if (executor != null) {
+            return executor;
+        }
+
+        // create a default executor which must be synchronized
+        synchronized (this) {
+            if (executor != null) {
+                return executor;
+            }
+            executor = context.getExecutorServiceStrategy().newDefaultThreadPool(this, "ProducerTemplate");
+        }
+
+        ObjectHelper.notNull(executor, "ExecutorService");
+        return executor;
     }
 
     protected void doStart() throws Exception {
@@ -733,9 +706,6 @@ public class DefaultProducerTemplate ext
             }
         }
         ServiceHelper.startService(producerCache);
-        if (executor == null) {
-            executor = context.getExecutorServiceStrategy().newDefaultThreadPool(this, "ProducerTemplate");
-        }
     }
 
     protected void doStop() throws Exception {