CAMEL-24305: Fix autowiring of KafkaClientFactory via addComponent() - #25230
CAMEL-24305: Fix autowiring of KafkaClientFactory via addComponent()#25230gnodet wants to merge 1 commit into
Conversation
Move default KafkaClientFactory creation from doInit() to doStart() in KafkaComponent. When a component is registered via addComponent() (the path used by Spring Boot), doInit() runs before the autowiring lifecycle strategy, causing the default factory to block injection of a custom one. Also restore the DefaultKafkaClientFactory fallback in KafkaEndpoint's doBuild() method for resilience when the endpoint builds before the component starts. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
|
🌟 Thank you for your contribution to the Apache Camel project! 🌟 🐫 Apache Camel Committers, please review the following items:
|
|
🧪 CI tested the following changed modules:
🔬 Scalpel shadow comparison — Scalpel: 9 tested, 29 compile-only — current: 9 all testedMaveniverse Scalpel detected 38 affected modules (current approach: 9).
|
gnodet
left a comment
There was a problem hiding this comment.
Looks good ✅
Correct fix for a real regression where CAMEL-17262 re-introduced the autowiring bug originally fixed in CAMEL-16500. The root cause is clear: creating the default KafkaClientFactory in doInit() blocks autowiring because the lifecycle ordering in addComponent() is:
ServiceHelper.initService→doInit()(default factory created here)postInitComponent→onComponentAdd→doAutoWire()(skips non-null fields)
Moving default creation to doStart() allows autowiring to inject a custom factory first. This follows the same pattern used in MapstructComponent and VertxHttpComponent.
Key observations:
- The defensive fallback in
KafkaEndpoint.doBuild()is appropriate — sincedoBuild()runs during endpoint creation (before component start in some lifecycle paths), the component's factory may still be null at that point kafkaManualCommitFactorydoesn't need the same treatment since no default is ever created for it- Good test coverage with the new
KafkaAutowireTest
CI passes on both JDK 17 and JDK 25.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of Guillaume Nodet
gnodet
left a comment
There was a problem hiding this comment.
Review Summary (informational — not a formal approval)
Correct fix for a verified regression where CAMEL-17262 re-introduced the autowiring bug originally fixed in CAMEL-16500. The lifecycle analysis is accurate, the fix follows established component patterns, and the test coverage is thorough.
Regression chain (verified via git history)
- CAMEL-16500 (commit
5ae05407c02) — Fixed autowiring by removing the field initializer= new DefaultKafkaClientFactory()and adding an endpoint-level fallback - CAMEL-17262 (commit
74c5a4ad2a7) — IntroduceddoInit()with default factory creation, which re-blocked autowiring in theaddComponent()path - This PR — Correctly moves default creation to
doStart()(after autowiring has run)
Key observations
- The
doStart()approach follows the same pattern used byMapstructComponentandVertxHttpComponent - Moving the
kafkaManualCommitFactorywarning todoStart()avoids false alarms in theaddComponent()path - The defensive fallback
new DefaultKafkaClientFactory()indoBuild()is benign sinceDefaultKafkaClientFactoryis stateless - Tests are well-designed:
testKafkaComponentAutowiringViaAddComponent()directly exercises the failing code path
Minor convention note
New test methods use JUnit assertions instead of AssertJ. Consistent with the existing test file, but project-wide preference is AssertJ.
Note: This is an informational review only — an agent cannot approve its operator's own PRs. Human review is required.
This review was generated by an AI agent and may contain inaccuracies. Please verify all suggestions before applying.
Claude Code on behalf of @gnodet
Summary
Claude Code on behalf of gnodet
Fixes CAMEL-24305:
KafkaComponentdoes not autowire a customKafkaClientFactorybean when the component is registered viaaddComponent()(the path used by Spring Boot).Root Cause
When a component is registered via
CamelContext.addComponent(), the lifecycle order is:ServiceHelper.initService(component)— callsdoBuild()+doInit()postInitComponent()— triggers autowiring viaLifecycleStrategySupport.doAutoWire()Since
KafkaComponent.doInit()was creating aDefaultKafkaClientFactoryin step 1, the field was already non-null when autowiring checked it in step 2, and the custom factory was silently skipped.This is a regression from CAMEL-17262 (commit
74c5a4ad2a7), which reintroduced the bug originally fixed in CAMEL-16500 (commit5ae05407c02).Fix
KafkaClientFactorycreation fromdoInit()todoStart(), giving the autowiring lifecycle strategy time to inject a custom factory before the default is created. This follows the same pattern used byMapstructComponentandVertxHttpComponent.DefaultKafkaClientFactoryfallback indoBuild()(originally from CAMEL-16500) for resilience when the endpoint builds before the component starts.Tests
testKafkaComponentAutowiringViaAddComponent()— exercises theaddComponent()path with a customKafkaClientFactoryregistered in the registry, verifying autowiring workstestKafkaComponentDefaultFactoryWhenNoneRegistered()— verifies the default factory is still created when no custom factory is availabletestKafkaComponentAutowiring()continues to pass (exercises thegetComponent()path)camel-kafkapass with 0 failures🤖 Generated with Claude Code