You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by lb...@apache.org on 2020/05/13 10:28:14 UTC

[camel-quarkus] branch master updated: Introduce RuntimeCamelContextCustomizerBuildItem to allow to customize the camel context before it is started

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-quarkus.git


The following commit(s) were added to refs/heads/master by this push:
     new d41007e  Introduce RuntimeCamelContextCustomizerBuildItem to allow to customize the camel context before it is started
d41007e is described below

commit d41007ed43552442fbed3e38915c1b628a4b65eb
Author: lburgazzoli <lb...@gmail.com>
AuthorDate: Tue May 12 15:58:20 2020 +0200

    Introduce RuntimeCamelContextCustomizerBuildItem to allow to customize the camel context before it is started
---
 .../quarkus/core/deployment/BuildProcessor.java    | 41 ++++++++++-------
 .../RuntimeCamelContextCustomizerBuildItem.java    | 37 +++++++++++++++
 .../camel/quarkus/core/CamelMainRecorder.java      | 12 +++++
 .../deployment/MicroProfileMetricsProcessor.java   | 18 ++++----
 .../runtime/CamelMicroProfileMetricsRecorder.java  | 53 +++++++++++++---------
 5 files changed, 114 insertions(+), 47 deletions(-)

diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java
index c69666c..77464e1 100644
--- a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/BuildProcessor.java
@@ -80,6 +80,7 @@ import org.apache.camel.quarkus.core.deployment.spi.CamelServicePatternBuildItem
 import org.apache.camel.quarkus.core.deployment.spi.CamelTypeConverterLoaderBuildItem;
 import org.apache.camel.quarkus.core.deployment.spi.CamelTypeConverterRegistryBuildItem;
 import org.apache.camel.quarkus.core.deployment.spi.ContainerBeansBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.RuntimeCamelContextCustomizerBuildItem;
 import org.apache.camel.quarkus.core.deployment.spi.UploadAttacherBuildItem;
 import org.apache.camel.quarkus.core.deployment.util.CamelSupport;
 import org.apache.camel.quarkus.core.deployment.util.PathFilter;
@@ -429,10 +430,9 @@ class BuildProcessor {
                     CamelSupport.getCamelVersion(),
                     config);
 
-            customizerBuildItems.stream()
-                    .forEach(customizer -> {
-                        recorder.customize(context, customizer.getCamelContextCustomizer());
-                    });
+            customizerBuildItems.forEach(customizer -> {
+                recorder.customize(context, customizer.getCamelContextCustomizer());
+            });
 
             return new CamelContextBuildItem(context);
         }
@@ -604,18 +604,20 @@ class BuildProcessor {
         /**
          * This method is responsible to start camel-main ar runtime.
          *
-         * @param recorder  the recorder.
-         * @param main      a reference to a {@link CamelMain}.
-         * @param registry  a reference to a {@link org.apache.camel.spi.Registry}; note that this parameter is here as
-         *                  placeholder to
-         *                  ensure the {@link org.apache.camel.spi.Registry} is fully configured before starting camel-main.
-         * @param executor  the {@link org.apache.camel.spi.ReactiveExecutor} to be configured on camel-main, this
-         *                  happens during {@link ExecutionTime#RUNTIME_INIT} because the executor may need to start
-         *                  threads and so on.
-         * @param shutdown  a reference to a {@link io.quarkus.runtime.ShutdownContext} used to register shutdown logic.
-         * @param startList a placeholder to ensure camel-main start after the ArC container is fully initialized. This
-         *                  is required as under the hoods the camel registry may look-up beans form the
-         *                  container thus we need it to be fully initialized to avoid unexpected behaviors.
+         * @param recorder    the recorder.
+         * @param main        a reference to a {@link CamelMain}.
+         * @param registry    a reference to a {@link org.apache.camel.spi.Registry}; note that this parameter is here as
+         *                    placeholder to
+         *                    ensure the {@link org.apache.camel.spi.Registry} is fully configured before starting camel-main.
+         * @param executor    the {@link org.apache.camel.spi.ReactiveExecutor} to be configured on camel-main, this
+         *                    happens during {@link ExecutionTime#RUNTIME_INIT} because the executor may need to start
+         *                    threads and so on.
+         * @param shutdown    a reference to a {@link io.quarkus.runtime.ShutdownContext} used to register shutdown logic.
+         * @param startList   a placeholder to ensure camel-main start after the ArC container is fully initialized. This
+         *                    is required as under the hoods the camel registry may look-up beans form the
+         *                    container thus we need it to be fully initialized to avoid unexpected behaviors.
+         * @param customizers a list of {@link org.apache.camel.quarkus.core.CamelContextCustomizer} that will be executed
+         *                    during {@link ExecutionTime#RUNTIME_INIT} before starting the camel context.
          */
         @Record(ExecutionTime.RUNTIME_INIT)
         @BuildStep(onlyIf = Flags.MainEnabled.class)
@@ -625,7 +627,12 @@ class BuildProcessor {
                 CamelRuntimeRegistryBuildItem registry,
                 CamelReactiveExecutorBuildItem executor,
                 ShutdownContextBuildItem shutdown,
-                List<ServiceStartBuildItem> startList) {
+                List<ServiceStartBuildItem> startList,
+                List<RuntimeCamelContextCustomizerBuildItem> customizers) {
+
+            for (RuntimeCamelContextCustomizerBuildItem customizer : customizers) {
+                recorder.customize(main.getInstance(), customizer.getCamelContextCustomizer());
+            }
 
             recorder.setReactiveExecutor(main.getInstance(), executor.getInstance());
             recorder.start(shutdown, main.getInstance());
diff --git a/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/spi/RuntimeCamelContextCustomizerBuildItem.java b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/spi/RuntimeCamelContextCustomizerBuildItem.java
new file mode 100644
index 0000000..92f9e29
--- /dev/null
+++ b/extensions-core/core/deployment/src/main/java/org/apache/camel/quarkus/core/deployment/spi/RuntimeCamelContextCustomizerBuildItem.java
@@ -0,0 +1,37 @@
+/*
+ * 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.quarkus.core.deployment.spi;
+
+import io.quarkus.builder.item.MultiBuildItem;
+import io.quarkus.runtime.RuntimeValue;
+import org.apache.camel.quarkus.core.CamelContextCustomizer;
+
+/**
+ * A {@link MultiBuildItem} holding the {@link CamelContextCustomizer} {@link RuntimeValue} and could be used
+ * to customize the camel context before starting it.
+ */
+public final class RuntimeCamelContextCustomizerBuildItem extends MultiBuildItem {
+    private final RuntimeValue<CamelContextCustomizer> value;
+
+    public RuntimeCamelContextCustomizerBuildItem(RuntimeValue<CamelContextCustomizer> value) {
+        this.value = value;
+    }
+
+    public RuntimeValue<CamelContextCustomizer> getCamelContextCustomizer() {
+        return value;
+    }
+}
diff --git a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelMainRecorder.java b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelMainRecorder.java
index 8542ee6..33d0787 100644
--- a/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelMainRecorder.java
+++ b/extensions-core/core/runtime/src/main/java/org/apache/camel/quarkus/core/CamelMainRecorder.java
@@ -25,6 +25,7 @@ import org.apache.camel.ExtendedCamelContext;
 import org.apache.camel.RoutesBuilder;
 import org.apache.camel.impl.engine.DefaultReactiveExecutor;
 import org.apache.camel.main.MainListener;
+import org.apache.camel.main.MainListenerSupport;
 import org.apache.camel.main.RoutesCollector;
 import org.apache.camel.spi.ReactiveExecutor;
 import org.apache.camel.spi.XMLRoutesDefinitionLoader;
@@ -106,4 +107,15 @@ public class CamelMainRecorder {
         return new RuntimeValue<>(new CamelRoutesCollector(registryRoutesLoader.getValue(), xmlRoutesLoader.getValue()));
     }
 
+    public void customize(RuntimeValue<CamelMain> main, RuntimeValue<CamelContextCustomizer> contextCustomizer) {
+        final CamelContextCustomizer customizer = contextCustomizer.getValue();
+
+        main.getValue().addMainListener(new MainListenerSupport() {
+            @Override
+            public void configure(CamelContext context) {
+                customizer.customize(context);
+            }
+        });
+    }
+
 }
diff --git a/extensions/microprofile-metrics/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/deployment/MicroProfileMetricsProcessor.java b/extensions/microprofile-metrics/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/deployment/MicroProfileMetricsProcessor.java
index e82c505..ff648a8 100644
--- a/extensions/microprofile-metrics/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/deployment/MicroProfileMetricsProcessor.java
+++ b/extensions/microprofile-metrics/deployment/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/deployment/MicroProfileMetricsProcessor.java
@@ -24,8 +24,8 @@ import org.apache.camel.component.microprofile.metrics.MicroProfileMetricsConsta
 import org.apache.camel.quarkus.component.microprofile.metrics.runtime.CamelMicroProfileMetricsConfig;
 import org.apache.camel.quarkus.component.microprofile.metrics.runtime.CamelMicroProfileMetricsRecorder;
 import org.apache.camel.quarkus.core.deployment.spi.CamelBeanBuildItem;
-import org.apache.camel.quarkus.core.deployment.spi.CamelContextBuildItem;
-import org.apache.camel.quarkus.core.deployment.spi.CamelMainListenerBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.CamelContextCustomizerBuildItem;
+import org.apache.camel.quarkus.core.deployment.spi.RuntimeCamelContextCustomizerBuildItem;
 import org.eclipse.microprofile.metrics.MetricRegistry;
 
 class MicroProfileMetricsProcessor {
@@ -48,17 +48,19 @@ class MicroProfileMetricsProcessor {
 
     @Record(ExecutionTime.STATIC_INIT)
     @BuildStep
-    CamelMainListenerBuildItem contextConfigurerListener(
+    CamelContextCustomizerBuildItem contextCustomizer(
             CamelMicroProfileMetricsRecorder recorder,
             CamelMicroProfileMetricsConfig config) {
 
-        return new CamelMainListenerBuildItem(recorder.createContextConfigurerListener(config));
+        return new CamelContextCustomizerBuildItem(recorder.createContextCustomizer(config));
     }
 
-    @Record(ExecutionTime.STATIC_INIT)
+    @Record(ExecutionTime.RUNTIME_INIT)
     @BuildStep
-    public void configureCamelContext(CamelMicroProfileMetricsRecorder recorder, CamelMicroProfileMetricsConfig config,
-            CamelContextBuildItem camelContextBuildItem) {
-        recorder.configureCamelContext(config, camelContextBuildItem.getCamelContext());
+    RuntimeCamelContextCustomizerBuildItem runtimeContextCustomizer(
+            CamelMicroProfileMetricsRecorder recorder,
+            CamelMicroProfileMetricsConfig config) {
+
+        return new RuntimeCamelContextCustomizerBuildItem(recorder.createRuntimeContextCustomizer(config));
     }
 }
diff --git a/extensions/microprofile-metrics/runtime/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/runtime/CamelMicroProfileMetricsRecorder.java b/extensions/microprofile-metrics/runtime/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/runtime/CamelMicroProfileMetricsRecorder.java
index e0a2c75..8af93a4 100644
--- a/extensions/microprofile-metrics/runtime/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/runtime/CamelMicroProfileMetricsRecorder.java
+++ b/extensions/microprofile-metrics/runtime/src/main/java/org/apache/camel/quarkus/component/microprofile/metrics/runtime/CamelMicroProfileMetricsRecorder.java
@@ -25,8 +25,7 @@ import org.apache.camel.component.microprofile.metrics.event.notifier.exchange.M
 import org.apache.camel.component.microprofile.metrics.event.notifier.route.MicroProfileMetricsRouteEventNotifier;
 import org.apache.camel.component.microprofile.metrics.message.history.MicroProfileMetricsMessageHistoryFactory;
 import org.apache.camel.component.microprofile.metrics.route.policy.MicroProfileMetricsRoutePolicyFactory;
-import org.apache.camel.main.MainListener;
-import org.apache.camel.main.MainListenerSupport;
+import org.apache.camel.quarkus.core.CamelContextCustomizer;
 import org.apache.camel.spi.ManagementStrategy;
 import org.eclipse.microprofile.metrics.MetricRegistry;
 import org.jboss.logging.Logger;
@@ -38,42 +37,52 @@ public class CamelMicroProfileMetricsRecorder {
         return new RuntimeValue<>(MetricRegistries.get(MetricRegistry.Type.APPLICATION));
     }
 
-    public RuntimeValue<MainListener> createContextConfigurerListener(CamelMicroProfileMetricsConfig config) {
-        return new RuntimeValue<>(new MicroProfileMetricsContextConfigurerListener(config));
+    public RuntimeValue<CamelContextCustomizer> createContextCustomizer(CamelMicroProfileMetricsConfig config) {
+        return new RuntimeValue<>(new MicroProfileMetricsContextCustomizer(config));
     }
 
-    public void configureCamelContext(CamelMicroProfileMetricsConfig config,
-            RuntimeValue<CamelContext> camelContextRuntimeValue) {
-        CamelContext camelContext = camelContextRuntimeValue.getValue();
-        ManagementStrategy managementStrategy = camelContext.getManagementStrategy();
+    public RuntimeValue<CamelContextCustomizer> createRuntimeContextCustomizer(CamelMicroProfileMetricsConfig config) {
+        return new RuntimeValue<>(new MicroProfileMetricsRuntimeContextCustomizer(config));
+    }
 
-        if (config.enableRoutePolicy) {
-            camelContext.addRoutePolicyFactory(new MicroProfileMetricsRoutePolicyFactory());
-        }
+    private static class MicroProfileMetricsContextCustomizer implements CamelContextCustomizer {
+        private final CamelMicroProfileMetricsConfig config;
 
-        if (config.enableExchangeEventNotifier) {
-            managementStrategy.addEventNotifier(new MicroProfileMetricsExchangeEventNotifier());
+        public MicroProfileMetricsContextCustomizer(CamelMicroProfileMetricsConfig config) {
+            this.config = config;
         }
 
-        if (config.enableRouteEventNotifier) {
-            managementStrategy.addEventNotifier(new MicroProfileMetricsRouteEventNotifier());
-        }
+        @Override
+        public void customize(CamelContext camelContext) {
+            if (config.enableRoutePolicy) {
+                camelContext.addRoutePolicyFactory(new MicroProfileMetricsRoutePolicyFactory());
+            }
+
+            ManagementStrategy managementStrategy = camelContext.getManagementStrategy();
+            if (config.enableExchangeEventNotifier) {
+                managementStrategy.addEventNotifier(new MicroProfileMetricsExchangeEventNotifier());
+            }
 
-        if (config.enableCamelContextEventNotifier) {
-            managementStrategy.addEventNotifier(new MicroProfileMetricsCamelContextEventNotifier());
+            if (config.enableRouteEventNotifier) {
+                managementStrategy.addEventNotifier(new MicroProfileMetricsRouteEventNotifier());
+            }
+
+            if (config.enableCamelContextEventNotifier) {
+                managementStrategy.addEventNotifier(new MicroProfileMetricsCamelContextEventNotifier());
+            }
         }
     }
 
-    private static class MicroProfileMetricsContextConfigurerListener extends MainListenerSupport {
-        private static final Logger LOGGER = Logger.getLogger(MicroProfileMetricsContextConfigurerListener.class);
+    private static class MicroProfileMetricsRuntimeContextCustomizer implements CamelContextCustomizer {
+        private static final Logger LOGGER = Logger.getLogger(MicroProfileMetricsRuntimeContextCustomizer.class);
         private final CamelMicroProfileMetricsConfig config;
 
-        public MicroProfileMetricsContextConfigurerListener(CamelMicroProfileMetricsConfig config) {
+        public MicroProfileMetricsRuntimeContextCustomizer(CamelMicroProfileMetricsConfig config) {
             this.config = config;
         }
 
         @Override
-        public void configure(CamelContext camelContext) {
+        public void customize(CamelContext camelContext) {
             if (!config.enableMessageHistory) {
                 return;
             }