lobtx opened a new pull request, #31245:
URL: https://github.com/apache/camel/pull/31245

   ## Problem
   
   With `camel run` (KameletMain), 
`camel.main.typeConverterStatisticsEnabled=true` has no effect:
   `isTypeConverterStatisticsEnabled()` returns `true`, but the registry has 
`NoopTypeConverterStatistics`, and the
   counters (and the JMX attributes) stay at 0. The same configuration with 
`Main` counts as expected (fixed by #30303).
   
   The statistics are chosen when the type converter registry is created, by 
`build()` of the CamelContext. Camel Main
   reads the option just before calling `build()` in `postProcessCamelContext`, 
but under KameletMain the CamelContext is
   already built by then:
   
   ```
   KameletMain.createCamelContext            answer.addService(new 
DependencyDownloaderKamelet(...))
    -> InternalServiceManager.doAddService   ServiceHelper.initService(service) 
  (right away)
    -> DependencyDownloaderKamelet.doBuild   
camelContext.getComponent("kamelet")
    -> AbstractCamelContext.getComponent     build()   <- builds the whole 
CamelContext
    -> AbstractCamelContext.doBuild          getOrCreateTypeConverter()   
(statistics still off)
   ```
   
   This is not intended: `createCamelContext` creates the context with `new 
DefaultCamelContext(false)`, commented
   "do not build/init camel context yet".
   
   ## Reproduction
   
   `KameletMainTypeConverterStatisticsTest` fails on main with `expected: <10> 
but was: <0>`, and
   
`DependencyDownloaderKameletTest.theCamelContextIsNotBuiltWhenTheServiceIsAdded`
 fails with
   `expected: <false> but was: <true>`:
   
   ```java
   KameletMain main = new KameletMain();
   main.setDownload(false);
   main.addInitialProperty("camel.main.typeConverterStatisticsEnabled", "true");
   main.start();
   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);
   ```
   
   With a real `camel run` (launcher built from main, then from this branch), a 
route that converts its body and logs
   `${camelContext.typeConverterRegistry.statistics.attemptCounter}`: 
`attempts=0` on main, `attempts=3`, `4` with the fix.
   
   ## Why it needs a fix
   
   Users enable the statistics to see the type conversions of their 
integration, and `camel run` is the usual way to run
   one: the option silently does nothing there. Building the CamelContext as a 
side effect of adding a service also
   makes KameletMain depend on the order of its services, as 
`DependencyDownloaderPropertiesComponent` shows (see below).
   
   ## Fix
   
   `DependencyDownloaderKamelet` no longer calls `getComponent("kamelet")` in 
`doBuild`: it sets its listener on a Kamelet
   component that already exists (`hasComponent`, which does not build), and on 
one created later through a
   `LifecycleStrategy.onComponentAdd`.
   
   `DependencyDownloaderPropertiesComponent` relied on that early build: its 
`doBuild` added an event notifier to the
   management strategy, which only exists once the context is built (NPE 
otherwise). It now uses
   `LifecycleStrategy.onContextInitializing`, called in `doInit` of the 
CamelContext just before the
   `CamelContextInitializingEvent` it listened to. A failure is logged and 
ignored, as `EventHelper` did for the notifier.
   
   Tests: camel-kamelet-main 125 tests (also with 
`-Dcamel.surefire.forkCount=8`), camel-jbang-run 404 tests,
   `-Psourcecheck`. Real `camel run` with a local Kamelet with a dependency 
(downloaded, Kamelet works) and
   `camel.beans.x=#class:...SimpleDriverDataSource` (camel-sql downloaded by 
the properties scan, bean configured).
   
   AI tool used: Claude Code (Claude Opus 5.5), reviewed and checked by me.
   
   _Claude Code on behalf of lobtx_
   
   🤖 Generated with [Claude Code](https://claude.com/claude-code)


-- 
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