Skip to content

Add Kafka consumer record interception hook#1348

Open
graemerocher wants to merge 5 commits into
6.1.xfrom
issue-1204-wrap-record-consumption
Open

Add Kafka consumer record interception hook#1348
graemerocher wants to merge 5 commits into
6.1.xfrom
issue-1204-wrap-record-consumption

Conversation

@graemerocher

Copy link
Copy Markdown
Contributor

Summary

  • add a ConsumerRecordInterceptor API for Kafka listeners with listener-method matching and an InterceptionContext
  • apply matching interceptors before single-record and batch listener binding while preserving Micronaut-managed commit behavior for filtered records
  • document the new interception hook and add focused processor tests for ordering, filtering, context metadata, and method selection

Verification

  • ./gradlew :micronaut-kafka:test --tests io.micronaut.configuration.kafka.processor.ConsumerStateSingleSpec --tests io.micronaut.configuration.kafka.processor.ConsumerStateBatchSpec --tests io.micronaut.configuration.kafka.processor.KafkaConsumerProcessorGracefulShutdownSpec
  • ./gradlew publishGuide docs

Resolves #1204

@graemerocher graemerocher added the type: enhancement New feature or request label Apr 29, 2026
@graemerocher graemerocher self-assigned this Apr 29, 2026
@graemerocher graemerocher requested a review from Copilot April 29, 2026 14:59

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot encountered an error and was unable to review this pull request. You can try again by re-requesting a review.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Copilot reviewed 11 out of 11 changed files in this pull request and generated 4 comments.

Comment thread src/main/docs/guide/kafkaListener/kafkaListenerInterceptors.adoc Outdated
@graemerocher

Copy link
Copy Markdown
Contributor Author

@copilot apply changes based on the comments in this thread

Copilot AI commented Apr 30, 2026

Copy link
Copy Markdown
Contributor

@copilot apply changes based on the comments in this thread

Applied all four changes in 791d468:

  1. Docs (kafkaListenerInterceptors.adoc): "a ann:..." → "an ann:..."
  2. KafkaConsumerProcessor: TopicPartition in interceptRecords now built from intercepted.topic() / intercepted.partition() so wrapped records bucket correctly.
  3. ConsumerStateBatch: Empty intercepted batch now calls handleResult(normalizeResult(null), consumerRecords) before returning, keeping the commit path consistent with successful processing.
  4. ConsumerStateSingle: Hoisted interceptedConsumerRecord declaration before the try/catch; the catch block now selects the intercepted record (when non-null) for resolveWithErrorStrategy and resetTheFollowingPartitions so error metadata matches what was actually processed.

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • checkstyle.org
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.25.1/x64/codeql/tools/linux64/java/bin/java /opt/hostedtoolcache/CodeQL/2.25.1/x64/codeql/tools/linux64/java/bin/java -jar /opt/hostedtoolcache/CodeQL/2.25.1/x64/codeql/xml/tools/xml-extractor.jar --fileList=/tmp/codeql-scratch-5b054857fe035941/dbs/java/working/files-to-index16628508787889242430.list --sourceArchiveDir=/tmp/codeql-scratch-5b054857fe035941/dbs/java/src --outputDir=/tmp/codeql-scratch-5b054857fe035941/dbs/java/trap/java (dns block)
  • ge.micronaut.io
    • Triggering command: /usr/lib/jvm/temurin-25-jdk-amd64/bin/java /usr/lib/jvm/temurin-25-jdk-amd64/bin/java --add-opens=java.base/java.lang=ALL-UNNAMED --add-opens=java.base/java.lang.invoke=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.api=ALL-UNNAMED --add-exports=jdk.compiler/com.sun.tools.javac.util=ALL-UNNAMED --add-opens=java.base/java.util=ALL-UNNAMED --add-opens=java.prefs/java.util.prefs=ALL-UNNAMED --add-opens=java.base/java.nio.charset=ALL-UNNAMED --add-opens=java.base/java.net=ALL-UNNAMED --add-opens=java.base/java.util.concurrent=ALL-UNNAMED --add-opens=java.base/java.util.concurrent.atomic=ALL-UNNAMED --add-opens=java.xml/javax.xml.namespace=ALL-UNNAMED --add-opens=java.base/java.time=ALL-UNNAMED --enable-native-access=ALL-UNNAMED -Xmx2048M -Dfile.encoding=UTF-8 -Duser.country -Duser.language=en (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

@graemerocher graemerocher force-pushed the issue-1204-wrap-record-consumption branch from 791d468 to f1759bf Compare April 30, 2026 07:55
graemerocher and others added 3 commits April 30, 2026 12:12
Resolves #1204

Co-Authored-By: Codex with GPT-5 <codex@openai.com>
…it path, error record selection, and docs typo

Agent-Logs-Url: https://github.com/micronaut-projects/micronaut-kafka/sessions/d513a696-a1bd-4396-a074-79da5f5ae36b

Co-authored-by: graemerocher <66626+graemerocher@users.noreply.github.com>
Restore the multi-method ConsumerInfo constructor used by the new routing specs and stub pass-through interception in the state tests so they exercise the real dispatch path.

Co-Authored-By: Codex with GPT-5 <codex@openai.com>
@graemerocher graemerocher force-pushed the issue-1204-wrap-record-consumption branch from 9a97bcc to d50de0d Compare April 30, 2026 12:15
Co-Authored-By: Codex with GPT-5 <codex@openai.com>
Co-Authored-By: Codex with GPT-5 <codex@openai.com>
@sonarqubecloud

sonarqubecloud Bot commented May 5, 2026

Copy link
Copy Markdown

}
final ExecutableMethod<Object, ?> method = info.methodForTopic(topic);
Optional.ofNullable(info.ackArg(topic)).ifPresent(argument -> {
final Map<TopicPartition, OffsetAndMetadata> batchOffsets = getAckOffsets(interceptedTopicRecords);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should it be getAckOffsets(originalTopicRecords)?

@sdelamo sdelamo removed this from 5.0.0 Release May 7, 2026
@github-project-automation github-project-automation Bot moved this to Backlog in 5.1.0 Release May 7, 2026
@graemerocher graemerocher changed the base branch from 6.0.x to 6.1.x June 9, 2026 16:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type: enhancement New feature or request

Projects

Status: Backlog

Development

Successfully merging this pull request may close these issues.

Wrap record Consumption

5 participants