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 f04cf99201be CAMEL-27103: camel-main - typeConverterStatisticsEnabled
now enables the statistics (#30303)
f04cf99201be is described below
commit f04cf99201bedb43eeb7976c918fc611f625cb0e
Author: Loris Boiteux <[email protected]>
AuthorDate: Wed Nov 18 07:42:22 2026 +0100
CAMEL-27103: camel-main - typeConverterStatisticsEnabled now enables the
statistics (#30303)
Camel Main built the CamelContext before it applied
camel.main.typeConverterStatisticsEnabled, so the type converter registry,
whose statistics are fixed when it is created (Camel 4.7), kept the noop
statistics and the counters stayed at 0.
BaseMainSupport now reads the option before camelContext.build(), as it does
for virtual threads. The early lookup is shared by both options and uses the
same sources and priority as the auto configuration (JVM system properties
over OS environment variables over the properties component, key in any
case,
initial properties as fallback), so a false wins as well, and
CAMEL_MAIN_VIRTUALTHREADSENABLED is now seen early too. A value with a
property placeholder cannot be resolved before the build: it is ignored,
with
a WARN.
Co-authored-by: Claude Opus 5.5 <[email protected]>
---
.../org/apache/camel/main/BaseMainSupport.java | 108 +++++++++++-------
.../MainTypeConverterStatisticsEnabledTest.java | 124 +++++++++++++++++++++
2 files changed, 194 insertions(+), 38 deletions(-)
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 da95ad458be6..444f6b1c6733 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
@@ -855,44 +855,12 @@ public abstract class BaseMainSupport extends BaseService
{
// Check programmatic configuration first
(main.configure().withVirtualThreadsEnabled(true))
boolean enabled =
mainConfigurationProperties.isVirtualThreadsEnabled();
- if (!enabled &&
mainConfigurationProperties.isAutoConfigurationEnabled()) {
- // Check initial properties (main.addInitialProperty /
main.setInitialProperties)
- if (initialProperties != null) {
- String val =
initialProperties.getProperty(optionKey("camel.main.virtualThreadsEnabled"));
- if (val != null) {
- enabled = Boolean.parseBoolean(val);
- }
- }
-
- if (!enabled) {
- // Load from PropertiesComponent (application.properties, etc.)
- OrderedLocationProperties props = (OrderedLocationProperties)
camelContext.getPropertiesComponent()
- .loadProperties(name -> name.startsWith("camel."),
MainHelper::optionKey);
- String val =
props.getProperty(optionKey("camel.main.virtualThreadsEnabled"));
- if (val != null) {
- enabled = Boolean.parseBoolean(val);
- }
- }
-
- if (!enabled &&
mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled()) {
- Properties envProps =
MainHelper.loadEnvironmentVariablesAsProperties(new String[] { "camel.main." });
- String val =
envProps.getProperty(optionKey("camel.main.virtualThreadsEnabled"));
- if (val != null) {
- enabled = Boolean.parseBoolean(val);
- }
- }
-
- if (!enabled &&
mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
- Properties sysProps =
MainHelper.loadJvmSystemPropertiesAsProperties(new String[] { "camel.main." });
- String val =
sysProps.getProperty("camel.main.virtualThreadsEnabled");
- if (val != null) {
- enabled = Boolean.parseBoolean(val);
- }
- }
-
- if (enabled) {
- mainConfigurationProperties.setVirtualThreadsEnabled(true);
- }
+ Boolean configured = lookupEarlyOption(camelContext,
"camel.main.virtualThreadsEnabled");
+ if (configured != null) {
+ enabled = configured;
+ }
+ if (enabled) {
+ mainConfigurationProperties.setVirtualThreadsEnabled(true);
}
if (enabled) {
@@ -979,6 +947,68 @@ public abstract class BaseMainSupport extends BaseService {
}
}
+ /**
+ * Enables the type converter statistics before the CamelContext is built,
as the type converter registry is created
+ * when the context is built, and its statistics cannot be enabled
afterwards (CAMEL-27103).
+ */
+ private void configureTypeConverterStatisticsEarly(CamelContext
camelContext) {
+ // Check programmatic configuration first
(main.configure().withTypeConverterStatisticsEnabled(true))
+ boolean enabled =
mainConfigurationProperties.isTypeConverterStatisticsEnabled();
+
+ Boolean configured = lookupEarlyOption(camelContext,
"camel.main.typeConverterStatisticsEnabled");
+ if (configured != null) {
+ enabled = configured;
+ }
+ if (enabled) {
+
mainConfigurationProperties.setTypeConverterStatisticsEnabled(true);
+ camelContext.setTypeConverterStatisticsEnabled(true);
+ }
+ }
+
+ /**
+ * Looks up a boolean option of Camel Main that must be known before the
CamelContext is built, which is before the
+ * auto configuration applies the configuration. The sources and their
priority are the same as in
+ * {@link #autoConfigurationSingleOption}: JVM system properties over OS
environment variables over the properties
+ * component, with the key in any case, and the initial properties as
fallback.
+ * <p>
+ * The value cannot be resolved before the context is built, as resolving
a property placeholder needs the type
+ * converter, so a value with a placeholder is ignored, with a WARN.
+ *
+ * @return the value, or <code>null</code> if the option is not configured
+ */
+ private Boolean lookupEarlyOption(CamelContext camelContext, String
optionName) {
+ if (!mainConfigurationProperties.isAutoConfigurationEnabled()) {
+ return null;
+ }
+
+ OrderedLocationProperties prop = (OrderedLocationProperties)
camelContext.getPropertiesComponent()
+ .loadProperties(name -> name.startsWith("camel."),
MainHelper::optionKey);
+ if
(mainConfigurationProperties.isAutoConfigurationEnvironmentVariablesEnabled()) {
+ putAllOverride(prop, "ENV",
MainHelper.loadEnvironmentVariablesAsProperties(new String[] { "camel.main."
}));
+ }
+ if
(mainConfigurationProperties.isAutoConfigurationSystemPropertiesEnabled()) {
+ putAllOverride(prop, "SYS",
MainHelper.loadJvmSystemPropertiesAsProperties(new String[] { "camel.main." }));
+ }
+ String key = findKeyIgnoreCase(prop, optionName);
+ Object value = key != null ? prop.get(key) : null;
+ if (ObjectHelper.isEmpty(value)) {
+ // fallback to initial properties
+ value = getInitialProperties().getProperty(optionName);
+ }
+ if (ObjectHelper.isEmpty(value)) {
+ return null;
+ }
+
+ String str = value.toString().trim();
+ if (str.contains(PropertiesComponent.PREFIX_TOKEN)) {
+ LOG.warn("The option {}={} is ignored, as it must be known before
the CamelContext is built,"
+ + " which is before property placeholders can be
resolved. Use true or false instead.",
+ optionName, str);
+ return null;
+ }
+ return Boolean.parseBoolean(str);
+ }
+
protected void configureRoutesLoader(CamelContext camelContext) {
// use main based routes loader
ExtendedCamelContext ecc = camelContext.getCamelContextExtension();
@@ -1068,6 +1098,8 @@ public abstract class BaseMainSupport extends BaseService
{
configureMainListener(camelContext);
// configure virtual threads early before build() to avoid ThreadType
DCL race
configureVirtualThreadsEarly(camelContext);
+ // the type converter registry is created by build(), and its
statistics cannot be enabled afterwards
+ configureTypeConverterStatisticsEarly(camelContext);
// ensure camel context is build
camelContext.build();
diff --git
a/core/camel-main/src/test/java/org/apache/camel/main/MainTypeConverterStatisticsEnabledTest.java
b/core/camel-main/src/test/java/org/apache/camel/main/MainTypeConverterStatisticsEnabledTest.java
new file mode 100644
index 000000000000..b2aa25b9e863
--- /dev/null
+++
b/core/camel-main/src/test/java/org/apache/camel/main/MainTypeConverterStatisticsEnabledTest.java
@@ -0,0 +1,124 @@
+/*
+ * 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.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Camel Main builds the CamelContext, which creates the type converter
registry, before it applies the configuration,
+ * so camel.main.typeConverterStatisticsEnabled must be applied before the
context is built (CAMEL-27103).
+ */
+public class MainTypeConverterStatisticsEnabledTest {
+
+ @Test
+ public void testTypeConverterStatisticsEnabledFromProperties() throws
Exception {
+ Main main = new Main();
+ main.addInitialProperty("camel.main.typeConverterStatisticsEnabled",
"true");
+ assertStatisticsEnabled(main);
+ }
+
+ @Test
+ public void testTypeConverterStatisticsEnabledFromConfigure() throws
Exception {
+ Main main = new Main();
+ main.configure().withTypeConverterStatisticsEnabled(true);
+ assertStatisticsEnabled(main);
+ }
+
+ @Test
+ public void testTypeConverterStatisticsEnabledFromSystemProperty() throws
Exception {
+ // lower case, as the auto configuration ignores case
+ System.setProperty("camel.main.typeconverterstatisticsenabled",
"true");
+ try {
+ assertStatisticsEnabled(new Main());
+ } finally {
+ System.clearProperty("camel.main.typeconverterstatisticsenabled");
+ }
+ }
+
+ @Test
+ public void testTypeConverterStatisticsEnabledOnContextBeforeBuild()
throws Exception {
+ // enabled in code before the context is built: the default (false) of
camel.main must not turn it off
+ Main main = new Main() {
+ @Override
+ protected CamelContext createCamelContext() {
+ CamelContext context = super.createCamelContext();
+ context.setTypeConverterStatisticsEnabled(true);
+ return context;
+ }
+ };
+ main.start();
+ try {
+ CamelContext context = main.getCamelContext();
+ TypeConverterRegistry.Statistics statistics =
context.getTypeConverterRegistry().getStatistics();
+ long before = statistics.getAttemptCounter();
+
+ assertEquals(123, context.getTypeConverter().convertTo(int.class,
"123"));
+
+ assertEquals(before + 1, statistics.getAttemptCounter());
+ } finally {
+ main.stop();
+ }
+ }
+
+ @Test
+ public void testSystemPropertyFalseOverridesInitialPropertyTrue() throws
Exception {
+ // same priority as the auto configuration: a JVM system property wins
over an initial property
+ System.setProperty("camel.main.typeConverterStatisticsEnabled",
"false");
+ try {
+ Main main = new Main();
+
main.addInitialProperty("camel.main.typeConverterStatisticsEnabled", "true");
+ main.start();
+ try {
+ CamelContext context = main.getCamelContext();
+ TypeConverterRegistry.Statistics statistics =
context.getTypeConverterRegistry().getStatistics();
+ long before = statistics.getAttemptCounter();
+
+ assertEquals(123,
context.getTypeConverter().convertTo(int.class, "123"));
+
+ assertFalse(context.isTypeConverterStatisticsEnabled());
+ assertEquals(before, statistics.getAttemptCounter());
+ } finally {
+ main.stop();
+ }
+ } finally {
+ System.clearProperty("camel.main.typeConverterStatisticsEnabled");
+ }
+ }
+
+ private static void assertStatisticsEnabled(Main main) throws Exception {
+ main.start();
+ try {
+ CamelContext context = main.getCamelContext();
+ assertTrue(context.isTypeConverterStatisticsEnabled());
+
+ TypeConverterRegistry.Statistics statistics =
context.getTypeConverterRegistry().getStatistics();
+ long before = statistics.getAttemptCounter();
+
+ assertEquals(123, context.getTypeConverter().convertTo(int.class,
"123"));
+
+ assertEquals(before + 1, statistics.getAttemptCounter());
+ } finally {
+ main.stop();
+ }
+ }
+}