This is an automated email from the ASF dual-hosted git repository. gnodet pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/camel.git
commit d7faf7c3564383c62b2002352e8596ea4af373df Author: Guillaume Nodet <[email protected]> AuthorDate: Mon May 4 09:47:09 2020 +0200 [CAMEL-15002] Add a beforeInitialize method on the MainListener and deprecate the configure method --- ...tomixClientClusteredRoutePolicyFactoryMain.java | 5 ++-- .../ConsulClusteredRoutePolicyFactoryMain.java | 5 ++-- .../ZooKeeperClusteredRoutePolicyFactoryMain.java | 5 ++-- .../zookeeper/cluster/ZooKeeperMasterMain.java | 5 ++-- .../org/apache/camel/main/BaseMainSupport.java | 5 ++++ .../java/org/apache/camel/main/MainListener.java | 16 ++++++++++- .../org/apache/camel/main/MainListenerSupport.java | 11 ++++++++ .../java/org/apache/camel/main/MainSupport.java | 1 + .../org/apache/camel/main/MainListenerTest.java | 32 ++++++++++++++++++++++ .../java/org/apache/camel/main/MainVetoTest.java | 4 +-- .../modules/ROOT/pages/camel-3x-upgrade-guide.adoc | 2 ++ 11 files changed, 80 insertions(+), 11 deletions(-) diff --git a/components/camel-atomix/src/test/java/org/apache/camel/component/atomix/cluster/AtomixClientClusteredRoutePolicyFactoryMain.java b/components/camel-atomix/src/test/java/org/apache/camel/component/atomix/cluster/AtomixClientClusteredRoutePolicyFactoryMain.java index 416a529..1c84c10 100644 --- a/components/camel-atomix/src/test/java/org/apache/camel/component/atomix/cluster/AtomixClientClusteredRoutePolicyFactoryMain.java +++ b/components/camel-atomix/src/test/java/org/apache/camel/component/atomix/cluster/AtomixClientClusteredRoutePolicyFactoryMain.java @@ -24,6 +24,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.cluster.ClusteredRoutePolicyFactory; import org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy; +import org.apache.camel.main.BaseMainSupport; import org.apache.camel.main.Main; import org.apache.camel.main.MainListenerSupport; @@ -37,14 +38,14 @@ public final class AtomixClientClusteredRoutePolicyFactoryMain { Main main = new Main(); main.addMainListener(new MainListenerSupport() { @Override - public void configure(CamelContext context) { + public void afterConfigure(BaseMainSupport main) { try { AtomixClusterService service = new AtomixClusterService(); service.setId("node-" + id); service.setStorageLevel(StorageLevel.MEMORY); service.setAddress(new Address(args[0])); service.setNodes(args.length > 1 ? args[1] : args[0]); - + CamelContext context = main.getCamelContext(); context.setNameStrategy(new ExplicitCamelContextNameStrategy("camel-" + id)); context.addService(service); context.addRoutePolicyFactory(ClusteredRoutePolicyFactory.forNamespace("my-ns")); diff --git a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryMain.java b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryMain.java index a2cf563..5b9f235 100644 --- a/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryMain.java +++ b/components/camel-consul/src/test/java/org/apache/camel/component/consul/cluster/ConsulClusteredRoutePolicyFactoryMain.java @@ -22,6 +22,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.cluster.ClusteredRoutePolicyFactory; import org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy; +import org.apache.camel.main.BaseMainSupport; import org.apache.camel.main.Main; import org.apache.camel.main.MainListenerSupport; @@ -35,12 +36,12 @@ public final class ConsulClusteredRoutePolicyFactoryMain { Main main = new Main(); main.addMainListener(new MainListenerSupport() { @Override - public void configure(CamelContext context) { + public void afterConfigure(BaseMainSupport main) { try { ConsulClusterService service = new ConsulClusterService(); service.setId("node-" + id); service.setUrl(args[0]); - + CamelContext context = main.getCamelContext(); context.setNameStrategy(new ExplicitCamelContextNameStrategy("camel-" + id)); context.addService(service); context.addRoutePolicyFactory(ClusteredRoutePolicyFactory.forNamespace("my-ns")); diff --git a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperClusteredRoutePolicyFactoryMain.java b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperClusteredRoutePolicyFactoryMain.java index 567d7cb..f29490e 100644 --- a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperClusteredRoutePolicyFactoryMain.java +++ b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperClusteredRoutePolicyFactoryMain.java @@ -22,6 +22,7 @@ import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.cluster.ClusteredRoutePolicyFactory; import org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy; +import org.apache.camel.main.BaseMainSupport; import org.apache.camel.main.Main; import org.apache.camel.main.MainListenerSupport; @@ -35,13 +36,13 @@ public final class ZooKeeperClusteredRoutePolicyFactoryMain { Main main = new Main(); main.addMainListener(new MainListenerSupport() { @Override - public void configure(CamelContext context) { + public void afterConfigure(BaseMainSupport main) { try { ZooKeeperClusterService service = new ZooKeeperClusterService(); service.setId("node-" + id); service.setNodes(args[0]); service.setBasePath("/camel"); - + CamelContext context = main.getCamelContext(); context.setNameStrategy(new ExplicitCamelContextNameStrategy("camel-" + id)); context.addService(service); context.addRoutePolicyFactory(ClusteredRoutePolicyFactory.forNamespace("my-ns")); diff --git a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperMasterMain.java b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperMasterMain.java index 5a9ea9d..86ece46 100644 --- a/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperMasterMain.java +++ b/components/camel-zookeeper/src/test/java/org/apache/camel/component/zookeeper/cluster/ZooKeeperMasterMain.java @@ -22,6 +22,7 @@ import java.util.concurrent.ThreadLocalRandom; import org.apache.camel.CamelContext; import org.apache.camel.builder.RouteBuilder; import org.apache.camel.impl.engine.ExplicitCamelContextNameStrategy; +import org.apache.camel.main.BaseMainSupport; import org.apache.camel.main.Main; import org.apache.camel.main.MainListenerSupport; @@ -36,13 +37,13 @@ public final class ZooKeeperMasterMain { Main main = new Main(); main.addMainListener(new MainListenerSupport() { @Override - public void configure(CamelContext context) { + public void afterConfigure(BaseMainSupport main) { try { ZooKeeperClusterService service = new ZooKeeperClusterService(); service.setId("node-" + nodeId); service.setNodes(address); service.setBasePath("/camel/master"); - + CamelContext context = main.getCamelContext(); context.setNameStrategy(new ExplicitCamelContextNameStrategy("camel-" + nodeId)); context.addService(service); } catch (Exception e) { diff --git a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java index 7b646ad..bcd9481 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/BaseMainSupport.java @@ -605,6 +605,10 @@ public abstract class BaseMainSupport extends BaseService { // ensure camel is initialized camelContext.build(); + for (MainListener listener : listeners) { + listener.beforeInitialize(this); + } + configurePropertiesService(camelContext); // allow to do configuration before its started @@ -618,6 +622,7 @@ public abstract class BaseMainSupport extends BaseService { // allow to do configuration before its started for (MainListener listener : listeners) { + listener.afterConfigure(this); listener.configure(camelContext); } } diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java b/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java index fb6a546..ae19f3a 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/MainListener.java @@ -28,13 +28,27 @@ public interface MainListener { * * @param main the main instance */ + void beforeInitialize(BaseMainSupport main); + + /** + * Callback invoked after the the CamelContext has been created and before the + * auto-configured step starts. + * + * @param main the main instance + */ void beforeConfigure(BaseMainSupport main); /** * Callback to configure the created CamelContext. * - * @param context the created CamelContext + * @param main the main instance + */ + void afterConfigure(BaseMainSupport main); + + /** + * @deprecated Use {@link #afterConfigure(BaseMainSupport)} instead */ + @Deprecated void configure(CamelContext context); /** diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java index 3008c58..b5ea730 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/MainListenerSupport.java @@ -23,11 +23,22 @@ import org.apache.camel.CamelContext; */ public class MainListenerSupport implements MainListener { @Override + public void beforeInitialize(BaseMainSupport main) { + // noop + } + + @Override public void beforeConfigure(BaseMainSupport main) { // noop } @Override + public void afterConfigure(BaseMainSupport main) { + // noop + } + + @Override + @Deprecated public void configure(CamelContext context) { // noop } diff --git a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java index 1ef8714..5a13c1c 100644 --- a/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java +++ b/core/camel-main/src/main/java/org/apache/camel/main/MainSupport.java @@ -77,6 +77,7 @@ public abstract class MainSupport extends BaseMainSupport { */ public void run() throws Exception { if (!completed.get()) { + init(); internalBeforeStart(); // if we have an issue starting then propagate the exception to caller beforeStart(); diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainListenerTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainListenerTest.java index e68059c..c2c8e8a 100644 --- a/core/camel-main/src/test/java/org/apache/camel/main/MainListenerTest.java +++ b/core/camel-main/src/test/java/org/apache/camel/main/MainListenerTest.java @@ -19,9 +19,41 @@ package org.apache.camel.main; import org.junit.Assert; import org.junit.Test; +import java.lang.reflect.Proxy; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + import static org.apache.camel.util.CollectionHelper.propertiesOf; public class MainListenerTest extends Assert { + + @Test + public void testEventOrder() throws Exception { + List<String> events = new ArrayList<>(); + Main main = new Main(); + main.addMainListener((MainListener) Proxy.newProxyInstance( + MainListener.class.getClassLoader(), + new Class[]{MainListener.class}, + (proxy, method, args) -> { + events.add(method.getName()); + return null; + })); + Thread thread = new Thread(() -> { + try { + main.run(); + } catch (Exception e) { + fail("Unexpected exception: " + e); + } + }); + thread.start(); + Thread.sleep(100); + main.completed(); + thread.join(); + assertEquals(Arrays.asList("beforeConfigure", "configure", "beforeStart", + "afterStart", "beforeStop", "afterStop"), events); + } + @Test public void testBeforeConfigure() { Main main = new Main(); diff --git a/core/camel-main/src/test/java/org/apache/camel/main/MainVetoTest.java b/core/camel-main/src/test/java/org/apache/camel/main/MainVetoTest.java index 6a327c0..a8ecc1f 100644 --- a/core/camel-main/src/test/java/org/apache/camel/main/MainVetoTest.java +++ b/core/camel-main/src/test/java/org/apache/camel/main/MainVetoTest.java @@ -34,8 +34,8 @@ public class MainVetoTest extends Assert { main.addRoutesBuilder(new MyRoute()); main.addMainListener(new MainListenerSupport() { @Override - public void configure(CamelContext context) { - context.addLifecycleStrategy(new MyVetoLifecycle()); + public void afterConfigure(BaseMainSupport main) { + main.getCamelContext().addLifecycleStrategy(new MyVetoLifecycle()); } }); diff --git a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc index 7d764e0..346907f 100644 --- a/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc +++ b/docs/user-manual/modules/ROOT/pages/camel-3x-upgrade-guide.adoc @@ -1124,6 +1124,8 @@ as they were not in use by Camel. In relation to the dump model classes removal, in camel-karaf the following commands were removed: `context-info`, `route-info`, `route-profile` and `route-step`. +The `configure(CamelContext)` method of the ``org.apache.camel.main.MainListener` has been deprecated and will be removed in a future release. The `afterConfigure(BaseMainSupport)` method should be used instead. + === camel-jackson The dependency on `jackson-module-jaxb-annotations` and the option `enableJaxbAnnotationModule` have been removed. To
