This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 3a270f9e7ccb CAMEL-27103: camel-kamelet-main - do not build the
CamelContext before Camel Main has configured it (#31245)
3a270f9e7ccb is described below
commit 3a270f9e7ccb0c312d0b793530d29095660fa035
Author: Loris Boiteux <[email protected]>
AuthorDate: Wed Dec 2 11:52:27 2026 +0100
CAMEL-27103: camel-kamelet-main - do not build the CamelContext before
Camel Main has configured it (#31245)
With camel run (KameletMain),
camel.main.typeConverterStatisticsEnabled=true had no
effect: DependencyDownloaderKamelet and
DependencyDownloaderPropertiesComponent built the
CamelContext as soon as they were added as services
(getComponent("kamelet") and the
management event notifier), before Camel Main had configured it, so
build-time options
were read too early. They now use hasComponent and a lifecycle strategy, so
the context
is built only after Camel Main has configured it, as with Main (#30303).
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../main/download/DependencyDownloaderKamelet.java | 15 +++-
.../DependencyDownloaderPropertiesComponent.java | 32 ++++----
.../KameletMainTypeConverterStatisticsTest.java | 50 ++++++++++++
.../download/DependencyDownloaderKameletTest.java | 17 ++++
...ependencyDownloaderPropertiesComponentTest.java | 90 ++++++++++++++++++++++
5 files changed, 189 insertions(+), 15 deletions(-)
diff --git
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderKamelet.java
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderKamelet.java
index 0625fb1407a9..117085bf20d0 100644
---
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderKamelet.java
+++
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderKamelet.java
@@ -26,6 +26,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.apache.camel.CamelContext;
import org.apache.camel.CamelContextAware;
+import org.apache.camel.Component;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.kamelet.Kamelet;
@@ -43,6 +44,7 @@ import org.apache.camel.model.RouteTemplateDefinition;
import org.apache.camel.spi.Resource;
import org.apache.camel.spi.ResourceLoader;
import org.apache.camel.spi.RouteTemplateLoaderListener;
+import org.apache.camel.support.LifecycleStrategySupport;
import org.apache.camel.support.PluginHelper;
import org.apache.camel.support.service.ServiceHelper;
import org.apache.camel.support.service.ServiceSupport;
@@ -93,10 +95,19 @@ public final class DependencyDownloaderKamelet extends
ServiceSupport
@Override
protected void doBuild() throws Exception {
- var comp = camelContext.getComponent("kamelet");
- if (comp instanceof KameletComponent kc) {
+ // do not use getComponent: it builds the CamelContext, and
KameletMain adds this service before Camel Main has
+ // configured the CamelContext (for example
camel.main.typeConverterStatisticsEnabled is only used by build)
+ if (camelContext.hasComponent("kamelet") instanceof KameletComponent
kc) {
kc.setRouteTemplateLoaderListener(this);
}
+ camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
+ @Override
+ public void onComponentAdd(String name, Component component) {
+ if (component instanceof KameletComponent kc) {
+
kc.setRouteTemplateLoaderListener(DependencyDownloaderKamelet.this);
+ }
+ }
+ });
if (camelContext instanceof ModelCamelContext mcc) {
mcc.addModelLifecycleStrategy(this);
}
diff --git
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderPropertiesComponent.java
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderPropertiesComponent.java
index 9721071da39d..6db2fbb6385b 100644
---
a/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderPropertiesComponent.java
+++
b/dsl/camel-kamelet-main/src/main/java/org/apache/camel/main/download/DependencyDownloaderPropertiesComponent.java
@@ -23,10 +23,9 @@ import org.apache.camel.CamelContext;
import org.apache.camel.Expression;
import org.apache.camel.RuntimeCamelException;
import org.apache.camel.StaticService;
-import org.apache.camel.spi.CamelEvent;
import org.apache.camel.spi.Language;
import org.apache.camel.support.DefaultExchange;
-import org.apache.camel.support.SimpleEventNotifierSupport;
+import org.apache.camel.support.LifecycleStrategySupport;
import org.apache.camel.support.service.ServiceSupport;
import org.apache.camel.util.IOHelper;
import org.apache.camel.util.ObjectHelper;
@@ -55,18 +54,25 @@ public class DependencyDownloaderPropertiesComponent
extends ServiceSupport impl
@Override
protected void doBuild() throws Exception {
- camelContext.getManagementStrategy().addEventNotifier(new
SimpleEventNotifierSupport() {
+ // a lifecycle strategy and not an event notifier: the management
strategy, which holds the event notifiers, is
+ // only created when the CamelContext is built, and KameletMain adds
this service before.
+ // The scan now runs before MainAutowiredLifecycleStrategy (ordered
last), and no longer after all the lifecycle
+ // strategies: the beans that it binds (such as the DataSource of
spring.datasource.url) are in the registry
+ // before Camel Main autowires the components created so far.
+ camelContext.addLifecycleStrategy(new LifecycleStrategySupport() {
@Override
- public void notify(CamelEvent event) throws Exception {
- // scan properties and detect dependencies to download and
services to auto-configure
- properties =
camelContext.getPropertiesComponent().loadProperties();
- resolveKnownDependencies();
- autoConfigureServices();
- }
-
- @Override
- public boolean isEnabled(CamelEvent event) {
- return event instanceof
CamelEvent.CamelContextInitializingEvent;
+ public void onContextInitializing(CamelContext context) {
+ try {
+ // scan properties and detect dependencies to download and
services to auto-configure
+ properties =
camelContext.getPropertiesComponent().loadProperties();
+ resolveKnownDependencies();
+ autoConfigureServices();
+ } catch (Exception e) {
+ // the event notifier used before logged and ignored a
failure, so keep doing that
+ LOG.warn("Error resolving dependencies and
auto-configuring services from properties."
+ + " This exception will be ignored.",
+ e);
+ }
}
});
}
diff --git
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTypeConverterStatisticsTest.java
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTypeConverterStatisticsTest.java
new file mode 100644
index 000000000000..52e831510dea
--- /dev/null
+++
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/KameletMainTypeConverterStatisticsTest.java
@@ -0,0 +1,50 @@
+/*
+ * 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.main;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.spi.TypeConverterRegistry;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class KameletMainTypeConverterStatisticsTest {
+
+ @Test
+ void testTypeConverterStatisticsEnabled() throws Exception {
+ // the statistics of the type converter registry are chosen when the
registry is created, by build(): the
+ // CamelContext must not be built before Main has read
camel.main.typeConverterStatisticsEnabled
+ KameletMain main = new KameletMain();
+ main.setDownload(false);
+ main.addInitialProperty("camel.main.typeConverterStatisticsEnabled",
"true");
+ main.start();
+ try {
+ CamelContext context = main.getCamelContext();
+ assertTrue(context.isTypeConverterStatisticsEnabled());
+
+ TypeConverterRegistry.Statistics statistics =
context.getTypeConverterRegistry().getStatistics();
+ long before = statistics.getAttemptCounter();
+ for (int i = 0; i < 10; i++) {
+ context.getTypeConverter().convertTo(int.class,
String.valueOf(i));
+ }
+ assertEquals(10, statistics.getAttemptCounter() - before);
+ } finally {
+ main.stop();
+ }
+ }
+}
diff --git
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderKameletTest.java
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderKameletTest.java
index faa569d7a983..8a0a4a4e609d 100644
---
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderKameletTest.java
+++
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderKameletTest.java
@@ -31,6 +31,7 @@ import org.junit.jupiter.api.io.TempDir;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertTrue;
class DependencyDownloaderKameletTest {
@@ -81,6 +82,22 @@ class DependencyDownloaderKameletTest {
}
}
+ @Test
+ void theCamelContextIsNotBuiltWhenTheServiceIsAdded() throws Exception {
+ // KameletMain adds this service before Camel Main has configured the
CamelContext, which must not be built yet:
+ // its build creates the type converter registry, with the statistics
of camel.main.typeConverterStatisticsEnabled
+ try (DefaultCamelContext context = new DefaultCamelContext(false)) {
+ context.addService(new RecordingDownloader());
+ DependencyDownloaderKamelet service = new
DependencyDownloaderKamelet(context, "4.0.0");
+ context.addService(service);
+ assertFalse(context.isBuild());
+
+ // the Kamelet component is created afterwards, and still gets the
listener
+ KameletComponent kamelet = context.getComponent("kamelet",
KameletComponent.class);
+ assertSame(service, kamelet.getRouteTemplateLoaderListener());
+ }
+ }
+
@Test
void theNameOfTheKamelet() {
assertEquals("my-soap-action",
DependencyDownloaderKamelet.kameletName("kamelet:my-soap-action"));
diff --git
a/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderPropertiesComponentTest.java
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderPropertiesComponentTest.java
new file mode 100644
index 000000000000..09023b2be778
--- /dev/null
+++
b/dsl/camel-kamelet-main/src/test/java/org/apache/camel/main/download/DependencyDownloaderPropertiesComponentTest.java
@@ -0,0 +1,90 @@
+/*
+ * 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.main.download;
+
+import java.util.List;
+import java.util.Map;
+import java.util.Properties;
+import java.util.concurrent.CopyOnWriteArrayList;
+
+import org.apache.camel.impl.DefaultCamelContext;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+class DependencyDownloaderPropertiesComponentTest {
+
+ @Test
+ void
theKnownDependenciesOfThePropertiesAreDownloadedWhenTheCamelContextIsInitialized()
throws Exception {
+ try (DefaultCamelContext context = new DefaultCamelContext(false)) {
+ RecordingDownloader downloader = new RecordingDownloader();
+ context.addService(downloader);
+ Properties initial = new Properties();
+ initial.setProperty("my.dataSource", "org.example.MyDataSource");
+ context.getPropertiesComponent().setInitialProperties(initial);
+ KnownDependenciesResolver knownDependencies = new
KnownDependenciesResolver(context, null, null);
+ knownDependencies.addMappings(Map.of("org.example.MyDataSource",
"org.example:my-datasource:1.0.0"));
+
+ // KameletMain adds this service before Camel Main has configured
the CamelContext, which is not built yet
+ context.addService(new
DependencyDownloaderPropertiesComponent(context, knownDependencies, true));
+ assertFalse(context.isBuild());
+ assertTrue(downloader.downloaded.isEmpty(),
downloader.downloaded.toString());
+
+ // the properties are scanned when the CamelContext is initialized
+ context.init();
+ assertEquals(List.of("org.example:my-datasource:1.0.0"),
downloader.downloaded);
+ }
+ }
+
+ /** Records the downloads, with nothing on the classpath, and downloads
nothing. */
+ private static class RecordingDownloader extends MavenDependencyDownloader
{
+
+ private final List<String> downloaded = new CopyOnWriteArrayList<>();
+
+ @Override
+ protected void doBuild() {
+ // noop
+ }
+
+ @Override
+ protected void doInit() {
+ // noop
+ }
+
+ @Override
+ protected void doStart() {
+ // noop
+ }
+
+ @Override
+ protected void doStop() {
+ // noop
+ }
+
+ @Override
+ public void downloadDependency(String groupId, String artifactId,
String version) {
+ downloaded.add(groupId + ":" + artifactId + ":" + version);
+ }
+
+ @Override
+ public boolean alreadyOnClasspath(String groupId, String artifactId,
String version) {
+ return false;
+ }
+ }
+}