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

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

commit 5c131659db31534ca7cdde230abc32bafd26cbc2
Author: lburgazzoli <lburgazz...@gmail.com>
AuthorDate: Thu Aug 20 00:15:16 2020 +0200

    lifecycle: split inititialization event in initializing and initialized
---
 .../org/apache/camel/spi/LifecycleStrategy.java    | 11 +++++++++
 .../camel/spi/OnCamelContextInitialized.java       |  2 +-
 ...alized.java => OnCamelContextInitializing.java} |  4 ++--
 .../camel/impl/engine/AbstractCamelContext.java    | 17 +++++++++++++-
 .../engine/OnCamelContextLifecycleStrategy.java    | 13 +++++++++++
 .../camel/impl/LifecycleStrategyDiscoveryTest.java | 27 ++++++++++++++++------
 .../org/apache/camel/main/ContextEventsTest.java   | 14 ++++++++---
 .../camel/support/LifecycleStrategySupport.java    | 14 +++++++++++
 8 files changed, 88 insertions(+), 14 deletions(-)

diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java 
b/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java
index 560a1e9..a3e3c0f 100644
--- a/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java
+++ b/core/camel-api/src/main/java/org/apache/camel/spi/LifecycleStrategy.java
@@ -41,6 +41,17 @@ public interface LifecycleStrategy {
      *                                        exceptions will be logged at 
<tt>WARN</tt> level by Camel will continue
      *                                        starting itself.
      */
+    default void onContextInitializing(CamelContext context) throws 
VetoCamelContextStartException {
+    }
+
+    /**
+     * Notification on initialized a {@link CamelContext}.
+     *
+     * @param  context                        the camel context
+     * @throws VetoCamelContextStartException can be thrown to veto starting 
{@link CamelContext}. Any other runtime
+     *                                        exceptions will be logged at 
<tt>WARN</tt> level by Camel will continue
+     *                                        starting itself.
+     */
     default void onContextInitialized(CamelContext context) throws 
VetoCamelContextStartException {
     }
 
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java
 
b/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java
index 1588a89..737bdc0 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java
@@ -19,7 +19,7 @@ package org.apache.camel.spi;
 import org.apache.camel.CamelContext;
 
 /**
- * Notification on initializing a {@link CamelContext}.
+ * Notification on initialized a {@link CamelContext}.
  */
 @FunctionalInterface
 public interface OnCamelContextInitialized extends OnCamelContextEvent {
diff --git 
a/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java
 
b/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitializing.java
similarity index 88%
copy from 
core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java
copy to 
core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitializing.java
index 1588a89..db323cc 100644
--- 
a/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitialized.java
+++ 
b/core/camel-api/src/main/java/org/apache/camel/spi/OnCamelContextInitializing.java
@@ -22,6 +22,6 @@ import org.apache.camel.CamelContext;
  * Notification on initializing a {@link CamelContext}.
  */
 @FunctionalInterface
-public interface OnCamelContextInitialized extends OnCamelContextEvent {
-    void onContextInitialized(CamelContext context);
+public interface OnCamelContextInitializing extends OnCamelContextEvent {
+    void onContextInitializing(CamelContext context);
 }
diff --git 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
index e350d33..364fe95 100644
--- 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
+++ 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/AbstractCamelContext.java
@@ -2559,7 +2559,7 @@ public abstract class AbstractCamelContext extends 
BaseService
         ServiceHelper.initService(lifecycleStrategies);
         for (LifecycleStrategy strategy : lifecycleStrategies) {
             try {
-                strategy.onContextInitialized(this);
+                strategy.onContextInitializing(this);
             } catch (VetoCamelContextStartException e) {
                 // okay we should not start Camel since it was vetoed
                 LOG.warn("Lifecycle strategy " + strategy + " vetoed 
initializing CamelContext ({}) due to: {}", getName(),
@@ -2640,6 +2640,21 @@ public abstract class AbstractCamelContext extends 
BaseService
         // start the route definitions before the routes is started
         startRouteDefinitions();
 
+        for (LifecycleStrategy strategy : lifecycleStrategies) {
+            try {
+                strategy.onContextInitialized(this);
+            } catch (VetoCamelContextStartException e) {
+                // okay we should not start Camel since it was vetoed
+                LOG.warn("Lifecycle strategy " + strategy + " vetoed 
initializing CamelContext ({}) due to: {}", getName(),
+                        e.getMessage());
+                throw e;
+            } catch (Exception e) {
+                LOG.warn("Lifecycle strategy " + strategy + " failed 
initializing CamelContext ({}) due to: {}", getName(),
+                        e.getMessage());
+                throw e;
+            }
+        }
+
         EventHelper.notifyCamelContextInitialized(this);
     }
 
diff --git 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/OnCamelContextLifecycleStrategy.java
 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/OnCamelContextLifecycleStrategy.java
index 95e77b3..9449aac 100644
--- 
a/core/camel-base/src/main/java/org/apache/camel/impl/engine/OnCamelContextLifecycleStrategy.java
+++ 
b/core/camel-base/src/main/java/org/apache/camel/impl/engine/OnCamelContextLifecycleStrategy.java
@@ -21,6 +21,7 @@ import org.apache.camel.RoutesBuilder;
 import org.apache.camel.VetoCamelContextStartException;
 import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.OnCamelContextInitialized;
+import org.apache.camel.spi.OnCamelContextInitializing;
 import org.apache.camel.spi.OnCamelContextStart;
 import org.apache.camel.spi.OnCamelContextStop;
 import org.apache.camel.support.LifecycleStrategySupport;
@@ -32,6 +33,18 @@ import org.apache.camel.support.LifecycleStrategySupport;
 class OnCamelContextLifecycleStrategy extends LifecycleStrategySupport {
 
     @Override
+    public void onContextInitializing(CamelContext context) throws 
VetoCamelContextStartException {
+        for (OnCamelContextInitializing handler : 
context.getRegistry().findByType(OnCamelContextInitializing.class)) {
+            // RoutesBuilder should register them-self to the camel context
+            // to avoid invoking them multiple times if routes are discovered
+            // from the registry (i.e. camel-main)
+            if (!(handler instanceof RoutesBuilder)) {
+                handler.onContextInitializing(context);
+            }
+        }
+    }
+
+    @Override
     public void onContextInitialized(CamelContext context) throws 
VetoCamelContextStartException {
         for (OnCamelContextInitialized handler : 
context.getRegistry().findByType(OnCamelContextInitialized.class)) {
             // RoutesBuilder should register them-self to the camel context
diff --git 
a/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyDiscoveryTest.java
 
b/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyDiscoveryTest.java
index 00ca357..7347808 100644
--- 
a/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyDiscoveryTest.java
+++ 
b/core/camel-core/src/test/java/org/apache/camel/impl/LifecycleStrategyDiscoveryTest.java
@@ -22,37 +22,48 @@ import org.apache.camel.CamelContext;
 import org.apache.camel.TestSupport;
 import org.apache.camel.builder.RouteBuilder;
 import org.apache.camel.spi.OnCamelContextInitialized;
+import org.apache.camel.spi.OnCamelContextInitializing;
 import org.apache.camel.spi.OnCamelContextStart;
 import org.apache.camel.spi.OnCamelContextStop;
 import org.junit.jupiter.api.Test;
 
 import static 
org.apache.camel.support.LifecycleStrategySupport.onCamelContextInitialized;
+import static 
org.apache.camel.support.LifecycleStrategySupport.onCamelContextInitializing;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 
 public class LifecycleStrategyDiscoveryTest extends TestSupport {
     @Test
     public void testLifecycleStrategyDiscovery() throws Exception {
-        final AtomicInteger onInit = new AtomicInteger();
+        final AtomicInteger onInitializing = new AtomicInteger();
+        final AtomicInteger onInitialized = new AtomicInteger();
         final AtomicInteger onStart = new AtomicInteger();
         final AtomicInteger onStop = new AtomicInteger();
-        final AtomicInteger onInitRoute = new AtomicInteger();
+        final AtomicInteger onInitializingRoute = new AtomicInteger();
+        final AtomicInteger onInitializedRoute = new AtomicInteger();
         final AtomicInteger onStartRoute = new AtomicInteger();
         final AtomicInteger onStopRoute = new AtomicInteger();
 
         CamelContext context = new DefaultCamelContext();
-        context.getRegistry().bind("myOnInit", onCamelContextInitialized(c -> 
onInit.incrementAndGet()));
+        context.getRegistry().bind("myOnInitializing", 
onCamelContextInitializing(c -> onInitializing.incrementAndGet()));
+        context.getRegistry().bind("myOnInitialized", 
onCamelContextInitialized(c -> onInitialized.incrementAndGet()));
         context.getRegistry().bind("myOnStart", onCamelContextInitialized(c -> 
onStart.incrementAndGet()));
         context.getRegistry().bind("myOnStop", onCamelContextInitialized(c -> 
onStop.incrementAndGet()));
 
         try {
-            class MyBuilder extends RouteBuilder implements 
OnCamelContextInitialized, OnCamelContextStart, OnCamelContextStop {
+            class MyBuilder extends RouteBuilder
+                    implements OnCamelContextInitializing, 
OnCamelContextInitialized, OnCamelContextStart, OnCamelContextStop {
                 @Override
                 public void configure() throws Exception {
                 }
 
                 @Override
+                public void onContextInitializing(CamelContext context) {
+                    onInitializingRoute.incrementAndGet();
+                }
+
+                @Override
                 public void onContextInitialized(CamelContext context) {
-                    onInitRoute.incrementAndGet();
+                    onInitializedRoute.incrementAndGet();
                 }
 
                 @Override
@@ -72,10 +83,12 @@ public class LifecycleStrategyDiscoveryTest extends 
TestSupport {
             context.stop();
         }
 
-        assertEquals(1, onInit.get());
+        assertEquals(1, onInitializing.get());
+        assertEquals(1, onInitialized.get());
         assertEquals(1, onStart.get());
         assertEquals(1, onStop.get());
-        assertEquals(1, onInitRoute.get());
+        assertEquals(1, onInitializingRoute.get());
+        assertEquals(1, onInitializedRoute.get());
         assertEquals(1, onStartRoute.get());
         assertEquals(1, onStopRoute.get());
     }
diff --git 
a/core/camel-main/src/test/java/org/apache/camel/main/ContextEventsTest.java 
b/core/camel-main/src/test/java/org/apache/camel/main/ContextEventsTest.java
index 8083a65..314feb5 100644
--- a/core/camel-main/src/test/java/org/apache/camel/main/ContextEventsTest.java
+++ b/core/camel-main/src/test/java/org/apache/camel/main/ContextEventsTest.java
@@ -20,6 +20,7 @@ import java.util.concurrent.atomic.AtomicInteger;
 
 import org.apache.camel.BindToRegistry;
 import org.apache.camel.spi.OnCamelContextInitialized;
+import org.apache.camel.spi.OnCamelContextInitializing;
 import org.apache.camel.spi.OnCamelContextStart;
 import org.apache.camel.spi.OnCamelContextStop;
 import org.junit.jupiter.api.Test;
@@ -36,19 +37,26 @@ public class ContextEventsTest {
         main.configure().addConfiguration(config);
         main.run();
 
-        assertEquals(1, config.onInit.get());
+        assertEquals(1, config.onInitializing.get());
+        assertEquals(1, config.onInitialized.get());
         assertEquals(1, config.onStart.get());
         assertEquals(1, config.onStop.get());
     }
 
     public static class MyConfig {
-        final AtomicInteger onInit = new AtomicInteger();
+        final AtomicInteger onInitializing = new AtomicInteger();
+        final AtomicInteger onInitialized = new AtomicInteger();
         final AtomicInteger onStart = new AtomicInteger();
         final AtomicInteger onStop = new AtomicInteger();
 
         @BindToRegistry
+        public OnCamelContextInitializing onContextInitializing() {
+            return context -> onInitializing.incrementAndGet();
+        }
+
+        @BindToRegistry
         public OnCamelContextInitialized onContextInitialized() {
-            return context -> onInit.incrementAndGet();
+            return context -> onInitialized.incrementAndGet();
         }
 
         @BindToRegistry
diff --git 
a/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
 
b/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
index b5013f4..37627bd 100644
--- 
a/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
+++ 
b/core/camel-support/src/main/java/org/apache/camel/support/LifecycleStrategySupport.java
@@ -31,6 +31,7 @@ import org.apache.camel.VetoCamelContextStartException;
 import org.apache.camel.spi.LifecycleStrategy;
 import org.apache.camel.spi.OnCamelContextEvent;
 import org.apache.camel.spi.OnCamelContextInitialized;
+import org.apache.camel.spi.OnCamelContextInitializing;
 import org.apache.camel.spi.OnCamelContextStart;
 import org.apache.camel.spi.OnCamelContextStop;
 
@@ -141,6 +142,15 @@ public abstract class LifecycleStrategySupport implements 
LifecycleStrategy {
         };
     }
 
+    public static LifecycleStrategy adapt(OnCamelContextInitializing handler) {
+        return new LifecycleStrategySupport() {
+            @Override
+            public void onContextInitializing(CamelContext context) throws 
VetoCamelContextStartException {
+                handler.onContextInitializing(context);
+            }
+        };
+    }
+
     public static LifecycleStrategy adapt(OnCamelContextInitialized handler) {
         return new LifecycleStrategySupport() {
             @Override
@@ -168,6 +178,10 @@ public abstract class LifecycleStrategySupport implements 
LifecycleStrategy {
         };
     }
 
+    public static OnCamelContextInitializing 
onCamelContextInitializing(Consumer<CamelContext> consumer) {
+        return consumer::accept;
+    }
+
     public static OnCamelContextInitialized 
onCamelContextInitialized(Consumer<CamelContext> consumer) {
         return consumer::accept;
     }

Reply via email to