Skip to content

CAMEL-22760: Bridge spring.kafka.* properties to camel-kafka component#1709

Open
gnodet wants to merge 3 commits intomainfrom
feat/camel-22760-kafka-spring-boot-properties
Open

CAMEL-22760: Bridge spring.kafka.* properties to camel-kafka component#1709
gnodet wants to merge 3 commits intomainfrom
feat/camel-22760-kafka-spring-boot-properties

Conversation

@gnodet
Copy link
Contributor

@gnodet gnodet commented Mar 12, 2026

Summary

When using camel-kafka-starter with Spring Boot, users previously had to duplicate their Kafka configuration under both spring.kafka.* and camel.component.kafka.* properties. This PR adds automatic bridging so that Spring Boot's standard Kafka properties are picked up by the Camel Kafka component.

Properties bridged

Spring Boot (spring.kafka.*) Camel (camel.component.kafka.*)
bootstrap-servers brokers
client-id client-id
security.protocol security-protocol
consumer.group-id group-id
ssl.key-store-location ssl-keystore-location
ssl.key-store-password ssl-keystore-password
ssl.key-store-type ssl-keystore-type
ssl.key-password ssl-key-password
ssl.trust-store-location ssl-truststore-location
ssl.trust-store-password ssl-truststore-password
ssl.trust-store-type ssl-truststore-type
ssl.protocol ssl-protocol
properties[sasl.mechanism] sasl-mechanism
properties[sasl.jaas.config] sasl-jaas-config
properties[sasl.kerberos.service.name] sasl-kerberos-service-name

Design

  • Uses Spring Boot's Binder to detect which camel.component.kafka.* properties the user explicitly set
  • Only bridges a Spring Boot value when the user has NOT explicitly set the corresponding Camel property
  • Explicit camel.component.kafka.* settings always take precedence
  • @AutoConfigureBefore(KafkaComponentAutoConfiguration.class) ensures bridging happens before the generated auto-configuration copies properties to the component
  • @ConditionalOnClass(KafkaProperties.class) ensures it only activates when spring-boot-kafka is on the classpath (optional dependency)

Test plan

  • 9 unit tests covering all bridged properties, precedence, and edge cases
  • All tests pass
  • CI build passes
  • Integration test with a real Kafka broker (manual)

🤖 Generated with Claude Code

When using camel-kafka-starter with Spring Boot, users previously had
to duplicate their Kafka configuration under both spring.kafka.* and
camel.component.kafka.* properties.

This adds a SpringKafkaPropertiesAutoConfiguration that automatically
bridges Spring Boot's KafkaProperties to the Camel Kafka component
configuration, including:
- bootstrap-servers -> brokers
- security.protocol -> security-protocol
- ssl.* properties (keystore, truststore, types, passwords)
- consumer.group-id -> group-id
- client-id
- SASL properties from spring.kafka.properties map

Explicit camel.component.kafka.* settings always take precedence.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@davsclaus
Copy link
Contributor

this is a good idea, a JIRA would be good so we have it there also. And add a note in the 4.19 migration guide especially if this would default happen so end users would be aware when upgrading

@davsclaus
Copy link
Contributor

I guess if not already exists should be an easy option to set = true or = false to turn this bridge on|off

- Use @autoConfiguration(after/before) instead of @configuration + @AutoConfigureBefore
  for correct auto-configuration ordering
- Add @ConditionalOnBean(KafkaProperties.class) to gracefully skip when
  Spring Kafka auto-config is excluded
- Use per-property Binder.bind().isBound() for reliable relaxed binding
  support (camelCase, kebab-case, underscore) instead of Map.containsKey()
- Check isSpringPropertyBound("bootstrap-servers") for bootstrap servers
  since KafkaProperties defaults to ["localhost:9092"]
- Fix BinderKeyFormatTest to be a proper regression test

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet
Copy link
Contributor Author

gnodet commented Mar 13, 2026

Thanks for the review @davsclaus!

We've addressed your feedback and also fixed several issues found during an in-depth code review:

Review fixes (latest commit):

  1. @AutoConfiguration ordering — Changed from @Configuration + @AutoConfigureBefore to @AutoConfiguration(after = KafkaAutoConfiguration.class, before = KafkaComponentAutoConfiguration.class). This ensures the bridge runs after Spring Boot creates KafkaProperties and before Camel's component auto-configuration applies the settings.

  2. @ConditionalOnBean(KafkaProperties.class) — Added so the bridge gracefully skips when Spring Kafka auto-configuration is excluded (e.g., user excluded KafkaAutoConfiguration), preventing a startup failure from missing constructor dependency.

  3. Per-property Binder.bind().isBound() for precedence checks — The original code used Binder.bind("camel.component.kafka", mapOf(...)).containsKey("ssl-keystore-location") to check if a Camel property was explicitly set. However, the Binder does not normalize camelCase keys to kebab-case in map bindings, so containsKey("ssl-keystore-location") would miss sslKeystoreLocation set by the user, causing their explicit Camel property to be silently overwritten. We now use individual binder.bind("camel.component.kafka.ssl-keystore-location", String.class).isBound() calls which properly handle relaxed binding (camelCase, kebab-case, underscore variants).

  4. Bootstrap servers default handlingKafkaProperties.getBootstrapServers() defaults to ["localhost:9092"] and is never null/empty. The original null/empty check would always bridge, even when the user didn't set spring.kafka.bootstrap-servers. We now use isSpringPropertyBound("bootstrap-servers") to check if the property was explicitly set.

Regarding your suggestions:

  • JIRA ticket: CAMEL-22760 already exists and is linked to this PR.
  • On/off toggle: Good point — we could add a camel.component.kafka.bridge-spring-kafka-properties (or similar) property. We can add that in a follow-up if you'd like, or in this PR. What do you prefer?
  • Migration guide note: Will add a note to the 4.19 migration guide.

@gnodet gnodet marked this pull request as ready for review March 13, 2026 08:38
Users can set camel.component.kafka.bridge-spring-kafka-properties=false
to disable the bridge. Enabled by default.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@gnodet
Copy link
Contributor Author

gnodet commented Mar 13, 2026

Added the toggle in the latest commit. The bridge can be disabled with:

camel.component.kafka.bridge-spring-kafka-properties=false

It's enabled by default (matchIfMissing = true). Uses @ConditionalOnProperty so the entire auto-configuration class is skipped when disabled — no beans created, no overhead.

@davidkarlsen
Copy link
Contributor

What about transactional() - https://camel.apache.org/components/4.18.x/eips/transactional-client.html - spring has a KafkaTransactionManager (PlatformTransactionManager) - or would that be too complex?
Perhaps just add a note in https://camel.apache.org/components/4.18.x/kafka-component.html#_kafka_transaction about how it is intended to work with tx.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants