|
1 | 1 | /*
|
2 |
| - * Copyright 2018-2022 the original author or authors. |
| 2 | + * Copyright 2018-2023 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
39 | 39 | import org.apache.kafka.clients.producer.ProducerRecord;
|
40 | 40 | import org.apache.kafka.common.PartitionInfo;
|
41 | 41 | import org.apache.kafka.common.TopicPartition;
|
| 42 | +import org.apache.kafka.common.header.Header; |
42 | 43 | import org.apache.kafka.common.header.Headers;
|
| 44 | +import org.apache.kafka.common.header.internals.RecordHeader; |
43 | 45 | import org.apache.kafka.common.header.internals.RecordHeaders;
|
44 | 46 |
|
45 | 47 | import org.springframework.core.log.LogAccessor;
|
@@ -226,7 +228,9 @@ public void setRetainExceptionHeader(boolean retainExceptionHeader) {
|
226 | 228 |
|
227 | 229 | /**
|
228 | 230 | * Set a function which will be called to obtain additional headers to add to the
|
229 |
| - * published record. |
| 231 | + * published record. If a {@link Header} returned is an instance of |
| 232 | + * {@link SingleRecordHeader}, then that header will replace any existing header of |
| 233 | + * that name, rather than being appended as a new value. |
230 | 234 | * @param headersFunction the headers function.
|
231 | 235 | * @since 2.5.4
|
232 | 236 | * @see #addHeadersFunction(BiFunction)
|
@@ -426,7 +430,10 @@ public void includeHeader(HeaderNames.HeadersToAdd... headers) {
|
426 | 430 | /**
|
427 | 431 | * Add a function which will be called to obtain additional headers to add to the
|
428 | 432 | * published record. Functions are called in the order that they are added, and after
|
429 |
| - * any function passed into {@link #setHeadersFunction(BiFunction)}. |
| 433 | + * any function passed into {@link #setHeadersFunction(BiFunction)}. If a |
| 434 | + * {@link Header} returned is an instance of {@link SingleRecordHeader}, then that |
| 435 | + * header will replace any existing header of that name, rather than being appended as |
| 436 | + * a new value. |
430 | 437 | * @param headersFunction the headers function.
|
431 | 438 | * @since 2.8.4
|
432 | 439 | * @see #setHeadersFunction(BiFunction)
|
@@ -722,7 +729,12 @@ private void enhanceHeaders(Headers kafkaHeaders, ConsumerRecord<?, ?> record, E
|
722 | 729 | maybeAddOriginalHeaders(kafkaHeaders, record, exception);
|
723 | 730 | Headers headers = this.headersFunction.apply(record, exception);
|
724 | 731 | if (headers != null) {
|
725 |
| - headers.forEach(kafkaHeaders::add); |
| 732 | + headers.forEach(header -> { |
| 733 | + if (header instanceof SingleRecordHeader) { |
| 734 | + kafkaHeaders.remove(header.key()); |
| 735 | + } |
| 736 | + kafkaHeaders.add(header); |
| 737 | + }); |
726 | 738 | }
|
727 | 739 | }
|
728 | 740 |
|
@@ -1389,4 +1401,34 @@ public interface ExceptionHeadersCreator {
|
1389 | 1401 |
|
1390 | 1402 | }
|
1391 | 1403 |
|
| 1404 | + /** |
| 1405 | + * A {@link Header} that indicates that this header should replace any existing headers |
| 1406 | + * with this name, rather than being appended to the headers, which is the normal behavior. |
| 1407 | + * |
| 1408 | + * @since 2.9.5 |
| 1409 | + * @see DeadLetterPublishingRecoverer#setHeadersFunction(BiFunction) |
| 1410 | + * @see DeadLetterPublishingRecoverer#addHeadersFunction(BiFunction) |
| 1411 | + */ |
| 1412 | + public static class SingleRecordHeader extends RecordHeader { |
| 1413 | + |
| 1414 | + /** |
| 1415 | + * Construct an instance. |
| 1416 | + * @param key the key. |
| 1417 | + * @param value the value. |
| 1418 | + */ |
| 1419 | + public SingleRecordHeader(String key, byte[] value) { |
| 1420 | + super(key, value); |
| 1421 | + } |
| 1422 | + |
| 1423 | + /** |
| 1424 | + * Construct an instance. |
| 1425 | + * @param keyBuffer the key buffer. |
| 1426 | + * @param valueBuffer the value buffer. |
| 1427 | + */ |
| 1428 | + public SingleRecordHeader(ByteBuffer keyBuffer, ByteBuffer valueBuffer) { |
| 1429 | + super(keyBuffer, valueBuffer); |
| 1430 | + } |
| 1431 | + |
| 1432 | + } |
| 1433 | + |
1392 | 1434 | }
|
0 commit comments