This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit a2938d33a0ee01d8ec6bb6ece31391dd299ffe88
Author: Claus Ibsen <claus.ib...@gmail.com>
AuthorDate: Fri Jun 7 11:44:26 2019 +0200

    CAMEL-13515: Allow producer to lazy start until first message
---
 .../validator/ValidatorLazyStartProducerTest.java  | 26 ++++++++++++++++++----
 .../org/apache/camel/support/DefaultConsumer.java  |  6 +++++
 .../org/apache/camel/support/DefaultEndpoint.java  | 18 +++++++++------
 .../org/apache/camel/support/DefaultProducer.java  | 14 ++----------
 4 files changed, 41 insertions(+), 23 deletions(-)

diff --git 
a/core/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorLazyStartProducerTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorLazyStartProducerTest.java
index 34c15a3..a1565e7 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorLazyStartProducerTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/component/validator/ValidatorLazyStartProducerTest.java
@@ -25,13 +25,26 @@ import org.junit.Test;
 public class ValidatorLazyStartProducerTest extends ContextTestSupport {
 
     @Test
-    public void testLazyStartProducer() throws Exception {
+    public void testLazyStartProducerFail() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
         try {
-            template.sendBody("direct:start", "Hello World");
+            template.sendBody("direct:fail", "<mail 
xmlns='http://foo.com/bar'><subject>Hey</subject><body>Hello 
world!</body></mail>");
             fail("Should throw exception");
         } catch (Exception e) {
             assertIsInstanceOf(FileNotFoundException.class, e.getCause());
         }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
+    public void testLazyStartProducerOk() throws Exception {
+        getMockEndpoint("mock:result").expectedMessageCount(1);
+
+        template.sendBody("direct:ok", "<mail 
xmlns='http://foo.com/bar'><subject>Hey</subject><body>Hello 
world!</body></mail>");
+
+        assertMockEndpointsSatisfied();
     }
 
     @Override
@@ -39,8 +52,13 @@ public class ValidatorLazyStartProducerTest extends 
ContextTestSupport {
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                from("direct:start")
-                    
.to("validator:org/apache/camel/component/validator/unknown.xsd?lazyStartProducer=true");
+                from("direct:fail")
+                    
.to("validator:org/apache/camel/component/validator/unknown.xsd?lazyStartProducer=true")
+                    .to("mock:result");
+
+                from("direct:ok")
+                    
.to("validator:org/apache/camel/component/validator/schema.xsd?lazyStartProducer=true")
+                    .to("mock:result");
             }
         };
     }
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultConsumer.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultConsumer.java
index ad24ea5..b14f997 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultConsumer.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultConsumer.java
@@ -133,6 +133,12 @@ public class DefaultConsumer extends ServiceSupport 
implements Consumer, RouteAw
         this.exceptionHandler = exceptionHandler;
     }
 
+    @Override
+    protected void doInit() throws Exception {
+        log.debug("Init consumer: {}", this);
+        ServiceHelper.initService(processor);
+    }
+
     protected void doStop() throws Exception {
         log.debug("Stopping consumer: {}", this);
         ServiceHelper.stopService(processor);
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
index 941199e..a4a3139 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultEndpoint.java
@@ -59,7 +59,8 @@ public abstract class DefaultEndpoint extends ServiceSupport 
implements Endpoint
     @UriParam(label = "producer",
             description = "Whether the producer should be started lazy (on the 
first message). By starting lazy you can use this to allow CamelContext and 
routes to startup"
                     + " in situations where a producer may otherwise fail 
during starting and cause the route to fail being started. By deferring this 
startup to be lazy then"
-                    + " the startup failure can be handled during routing 
messages via Camel's routing error handlers.")
+                    + " the startup failure can be handled during routing 
messages via Camel's routing error handlers. Beware that when the first message 
is processed"
+                    + " then creating and starting the producer may take a 
little time and prolong the total processing time of the processing.")
     private boolean lazyStartProducer;
     @UriParam(label = "consumer", optionalPrefix = "consumer.", description = 
"Allows for bridging the consumer to the Camel routing Error Handler, which 
mean any exceptions occurred while"
                     + " the consumer is trying to pickup incoming messages, or 
the likes, will now be processed as a message and handled by the routing Error 
Handler."
@@ -196,10 +197,7 @@ public abstract class DefaultEndpoint extends 
ServiceSupport implements Endpoint
     }
 
     /**
-     * Returns the component that created this endpoint.
-     * 
-     * @return the component that created this endpoint, or <tt>null</tt> if
-     *         none set
+     * Returns the component that created this endpoint, or <tt>null</tt> if 
none set.
      */
     public Component getComponent() {
         return component;
@@ -279,7 +277,8 @@ public abstract class DefaultEndpoint extends 
ServiceSupport implements Endpoint
     /**
      * Whether the producer should be started lazy (on the first message). By 
starting lazy you can use this to allow CamelContext and routes to startup
      * in situations where a producer may otherwise fail during starting and 
cause the route to fail being started. By deferring this startup to be lazy then
-     * the startup failure can be handled during routing messages via Camel's 
routing error handlers.
+     * the startup failure can be handled during routing messages via Camel's 
routing error handlers. Beware that when the first message is processed
+     * then creating and starting the producer may take a little time and 
prolong the total processing time of the processing.
      */
     public void setLazyStartProducer(boolean lazyStartProducer) {
         this.lazyStartProducer = lazyStartProducer;
@@ -512,7 +511,7 @@ public abstract class DefaultEndpoint extends 
ServiceSupport implements Endpoint
     }
 
     @Override
-    protected void doStart() throws Exception {
+    protected void doInit() throws Exception {
         // the bridgeErrorHandler/exceptionHandler was originally configured 
with consumer. prefix, such as consumer.bridgeErrorHandler=true
         // so if they have been configured on the endpoint then map to the old 
naming style
         if (bridgeErrorHandler) {
@@ -524,6 +523,11 @@ public abstract class DefaultEndpoint extends 
ServiceSupport implements Endpoint
     }
 
     @Override
+    protected void doStart() throws Exception {
+        // noop
+    }
+
+    @Override
     protected void doStop() throws Exception {
         // noop
     }
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultProducer.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultProducer.java
index 3fc1b2c..32f0c31 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/DefaultProducer.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/DefaultProducer.java
@@ -53,23 +53,12 @@ public abstract class DefaultProducer extends 
ServiceSupport implements Producer
     /**
      * This implementation will delegate to the endpoint {@link 
org.apache.camel.Endpoint#isSingleton()}
      */
+    @Override
     public boolean isSingleton() {
         return endpoint.isSingleton();
     }
 
     @Override
-    public void start() {
-        if (getEndpoint() instanceof DefaultEndpoint) {
-            DefaultEndpoint de = (DefaultEndpoint) getEndpoint();
-            if (de.isLazyStartProducer()) {
-                // need to check if we can start now
-
-                return;
-            }
-        }
-        super.start();
-    }
-
     protected void doStart() throws Exception {
         // log at debug level for singletons, for prototype scoped log at 
trace level to not spam logs
         if (isSingleton()) {
@@ -79,6 +68,7 @@ public abstract class DefaultProducer extends ServiceSupport 
implements Producer
         }
     }
 
+    @Override
     protected void doStop() throws Exception {
         // log at debug level for singletons, for prototype scoped log at 
trace level to not spam logs
         if (isSingleton()) {

Reply via email to