Croway commented on PR #30303:
URL: https://github.com/apache/camel/pull/30303#issuecomment-6960807418

   The registry now follows the context flag after build, but Camel Main also 
sets that flag after build: `DefaultConfigurationConfigurer.configure` (line 
300) calls 
`setTypeConverterStatisticsEnabled(config.isTypeConverterStatisticsEnabled())`, 
which is `false` by default. So statistics enabled in code before `build()`, as 
the previous `CamelContext` Javadoc said to, are now switched off.
   
   Reproducer:
   
   ```java
   Main main = new Main() {
       @Override
       protected CamelContext createCamelContext() {
           CamelContext context = super.createCamelContext();
           context.setTypeConverterStatisticsEnabled(true);
           return context;
       }
   };
   main.start();
   // read getAttemptCounter(), then convertTo(int.class, "123"), then read it 
again
   ```
   
   On `main` the registry keeps `TypeConverterStatistics` and the attempt 
counter goes up by 1. On this PR the registry ends up with 
`NoopTypeConverterStatistics` and it stays the same.
   
   This also reverses CAMEL-19897 (Camel 4.7), which made the statistics 
collector fixed once the registry exists. `type-converter.adoc` ("made 
immutable") and the comment in Spring XML's 
`CamelContextFactoryBean.createContext()` still describe that.
   
   **Suggestion: fix the order in Camel Main, and leave the registry as it 
is.** CAMEL-23479 fixed `camel.main.virtualThreadsEnabled` the same way, with 
`configureVirtualThreadsEarly`, which reads the option before 
`camelContext.build()`. The same can be done for this option in 
`BaseMainSupport.postProcessCamelContext`:
   
   ```java
           configureVirtualThreadsEarly(camelContext);
           // the type converter registry is created by build(), and its 
statistics are fixed from then on
           configureTypeConverterStatisticsEarly(camelContext);
   
           // ensure camel context is build
           camelContext.build();
   ```
   
   ```java
       private void configureTypeConverterStatisticsEarly(CamelContext 
camelContext) {
           if (mainConfigurationProperties.isTypeConverterStatisticsEnabled()
                   || isEarlyOptionEnabled(camelContext, 
"camel.main.typeConverterStatisticsEnabled")) {
               
mainConfigurationProperties.setTypeConverterStatisticsEnabled(true);
               camelContext.setTypeConverterStatisticsEnabled(true);
           }
       }
   ```
   
   `isEarlyOptionEnabled` is the lookup that `configureVirtualThreadsEarly` 
already does: initial properties, the properties component 
(`application.properties`), environment variables and JVM system properties. It 
can be extracted into a helper and used by both methods.
   
   With this, and with the changes to `CoreTypeConverterRegistry`, 
`SimpleCamelContext` and the `CamelContext` Javadoc reverted, I checked locally 
that:
   - `MainTypeConverterStatisticsEnabledTest` from this PR passes;
   - statistics are also counted when the option is set with 
`main.configure().withTypeConverterStatisticsEnabled(true)`, with a JVM system 
property, or in code before the build (the reproducer above);
   - the camel-main tests pass (351 tests).
   
   `TypeConverterRegistryStatisticsEnabledAfterBuildTest` would then go away, 
as changing the option after the build stays unsupported, which is what the 
user manual says. The registry field stays `final`, with no `volatile` read on 
every conversion. In the tests, reading the counter before `convertTo` and 
asserting the difference would also make them independent of conversions done 
during startup.
   
   Camel Spring Boot has the same order 
(`CamelAutoConfiguration.doConfigureCamelContext` calls `camelContext.build()` 
before `DefaultConfigurationConfigurer.configure`). There, `config.getMain()` 
is already bound, so I'd expect a line before `camelContext.build()` to be 
enough, in a separate PR on camel-spring-boot (I have not tested it):
   
   ```java
   if (config.getMain().isTypeConverterStatisticsEnabled()) {
       camelContext.setTypeConverterStatisticsEnabled(true);
   }
   ```
   
   _Claude Code on behalf of Croway_
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to