-
Couldn't load subscription status.
- Fork 1.7k
GH-3407 : Support KafkaHeaders.DELIVERY_ATTEMP for batch listeners. #3539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
artembilan
merged 10 commits into
spring-projects:main
from
chickenchickenlove:241006-batch-attempt
Oct 10, 2024
Merged
Changes from 5 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
ebae43a
GH-3407 Draft
chickenchickenlove 9ce3d04
Fixes lint error.
chickenchickenlove fd3e4ff
Fixes flaky integration tests.
chickenchickenlove 47f13e5
Add adocs for new feature.
chickenchickenlove b6a0d23
Merge branch 'main' into 241006-batch-attempt
chickenchickenlove 0d852f1
Update adocs, test code refactoring, remove useless comment.
chickenchickenlove 61746ac
Merge branch 'main' into 241006-batch-attempt
chickenchickenlove 5a9a68f
Fixes lint error
chickenchickenlove 6ab72b9
Remove useless code.
chickenchickenlove f33aa49
Fixes lint error
chickenchickenlove File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
...main/antora/modules/ROOT/pages/retrytopic/kafka-headers-for-batch-listener.adoc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| [[kafka-headers-for-batch-listener]] | ||
| = Kafka Headers for batch listener | ||
|
|
||
| When processing `ConsumerRecord` with the `BatchListener`, the `KafkaHeaders.DELIVERY_ATTEMPT` header can be present in a different way compared to `SingleRecordListener`. | ||
|
|
||
| To inject the `KafkaHeaders.DELIVERY_ATTEMPT` header into `ConsumerRecord` when using the `BatchListener`, set the `DeliveryAttemptAwareRetryListener` as the `RetryListener` in the `ErrorHandler`. | ||
|
|
||
| Please refer to the code below. | ||
| [source, java] | ||
| ---- | ||
| final FixedBackOff fixedBackOff = new FixedBackOff(1, 10); | ||
| final DefaultErrorHandler errorHandler = new DefaultErrorHandler(fixedBackOff); | ||
| errorHandler.setRetryListeners(new DeliveryAttemptAwareRetryListener()); | ||
| ConcurrentKafkaListenerContainerFactory<String, String> factory = new ConcurrentKafkaListenerContainerFactory<>(); | ||
| factory.setConsumerFactory(consumerFactory); | ||
| factory.setCommonErrorHandler(errorHandler); | ||
| ---- | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...a/src/main/java/org/springframework/kafka/listener/DeliveryAttemptAwareRetryListener.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| /* | ||
| * Copyright 2021-2024 the original author or authors. | ||
| * | ||
| * Licensed under the Apache License, Version 2.0 (the "License"); | ||
| * you may not use this file except in compliance with the License. | ||
| * You may obtain a copy of the License at | ||
| * | ||
| * https://www.apache.org/licenses/LICENSE-2.0 | ||
| * | ||
| * Unless required by applicable law or agreed to in writing, software | ||
| * distributed under the License is distributed on an "AS IS" BASIS, | ||
| * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| * See the License for the specific language governing permissions and | ||
| * limitations under the License. | ||
| */ | ||
|
|
||
| package org.springframework.kafka.listener; | ||
|
|
||
| import java.nio.ByteBuffer; | ||
|
|
||
| import org.apache.kafka.clients.consumer.ConsumerRecord; | ||
| import org.apache.kafka.clients.consumer.ConsumerRecords; | ||
| import org.apache.kafka.common.header.internals.RecordHeader; | ||
|
|
||
| import org.springframework.kafka.support.KafkaHeaders; | ||
|
|
||
| /** | ||
| * The DeliveryAttemptAwareRetryListener class for {@link RetryListener} implementations. | ||
| * The DeliveryAttemptAwareRetryListener adds the {@link KafkaHeaders}.DELIVERY_ATTEMPT header | ||
| * to the record's headers when batch records fail and are retried. | ||
| * Note that DeliveryAttemptAwareRetryListener modifies the headers of the original record. | ||
| * | ||
| * @author Sanghyeok An | ||
| * @since 3.3 | ||
| * @see KafkaConsumerBackoffManager | ||
artembilan marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| */ | ||
|
|
||
| public class DeliveryAttemptAwareRetryListener implements RetryListener { | ||
|
|
||
| @Override | ||
| public void failedDelivery(ConsumerRecord<?, ?> record, Exception ex, int deliveryAttempt) { | ||
| // Pass | ||
| } | ||
|
|
||
| /** | ||
| * Invoke after delivery failure for batch records. | ||
| * If the {@link KafkaHeaders}.DELIVERY_ATTEMPT header already exists in the {@link ConsumerRecord}'s headers, | ||
| * it will be removed. Then, the provided `deliveryAttempt` is added to the {@link ConsumerRecord}'s headers. | ||
| * @param records the records. | ||
| * @param ex the exception. | ||
| * @param deliveryAttempt the delivery attempt, if available. | ||
| */ | ||
| @Override | ||
| public void failedDelivery(ConsumerRecords<?, ?> records, Exception ex, int deliveryAttempt) { | ||
| for (ConsumerRecord<?, ?> record : records) { | ||
| record.headers().remove(KafkaHeaders.DELIVERY_ATTEMPT); | ||
|
|
||
| byte[] buff = new byte[4]; // NOSONAR (magic #) | ||
| ByteBuffer bb = ByteBuffer.wrap(buff); | ||
| bb.putInt(deliveryAttempt); | ||
| record.headers().add(new RecordHeader(KafkaHeaders.DELIVERY_ATTEMPT, buff)); | ||
| } | ||
| } | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.