|
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.
|
|
40 | 40 | import org.apache.kafka.clients.producer.ProducerRecord;
|
41 | 41 | import org.apache.kafka.common.PartitionInfo;
|
42 | 42 | import org.apache.kafka.common.TopicPartition;
|
| 43 | +import org.apache.kafka.common.header.Header; |
43 | 44 | import org.apache.kafka.common.header.Headers;
|
| 45 | +import org.apache.kafka.common.header.internals.RecordHeader; |
44 | 46 | import org.apache.kafka.common.header.internals.RecordHeaders;
|
45 | 47 |
|
46 | 48 | import org.springframework.core.log.LogAccessor;
|
@@ -224,7 +226,9 @@ public void setRetainExceptionHeader(boolean retainExceptionHeader) {
|
224 | 226 |
|
225 | 227 | /**
|
226 | 228 | * Set a function which will be called to obtain additional headers to add to the
|
227 |
| - * published record. |
| 229 | + * published record. If a {@link Header} returned is an instance of |
| 230 | + * {@link SingleRecordHeader}, then that header will replace any existing header of |
| 231 | + * that name, rather than being appended as a new value. |
228 | 232 | * @param headersFunction the headers function.
|
229 | 233 | * @since 2.5.4
|
230 | 234 | * @see #addHeadersFunction(BiFunction)
|
@@ -411,7 +415,10 @@ public void includeHeader(HeaderNames.HeadersToAdd... headers) {
|
411 | 415 | /**
|
412 | 416 | * Add a function which will be called to obtain additional headers to add to the
|
413 | 417 | * published record. Functions are called in the order that they are added, and after
|
414 |
| - * any function passed into {@link #setHeadersFunction(BiFunction)}. |
| 418 | + * any function passed into {@link #setHeadersFunction(BiFunction)}. If a |
| 419 | + * {@link Header} returned is an instance of {@link SingleRecordHeader}, then that |
| 420 | + * header will replace any existing header of that name, rather than being appended as |
| 421 | + * a new value. |
415 | 422 | * @param headersFunction the headers function.
|
416 | 423 | * @since 2.8.4
|
417 | 424 | * @see #setHeadersFunction(BiFunction)
|
@@ -707,7 +714,12 @@ private void enhanceHeaders(Headers kafkaHeaders, ConsumerRecord<?, ?> record, E
|
707 | 714 | maybeAddOriginalHeaders(kafkaHeaders, record, exception);
|
708 | 715 | Headers headers = this.headersFunction.apply(record, exception);
|
709 | 716 | if (headers != null) {
|
710 |
| - headers.forEach(kafkaHeaders::add); |
| 717 | + headers.forEach(header -> { |
| 718 | + if (header instanceof SingleRecordHeader) { |
| 719 | + kafkaHeaders.remove(header.key()); |
| 720 | + } |
| 721 | + kafkaHeaders.add(header); |
| 722 | + }); |
711 | 723 | }
|
712 | 724 | }
|
713 | 725 |
|
@@ -1374,4 +1386,34 @@ public interface ExceptionHeadersCreator {
|
1374 | 1386 |
|
1375 | 1387 | }
|
1376 | 1388 |
|
| 1389 | + /** |
| 1390 | + * A {@link Header} that indicates that this header should replace any existing headers |
| 1391 | + * with this name, rather than being appended to the headers, which is the normal behavior. |
| 1392 | + * |
| 1393 | + * @since 2.9.5 |
| 1394 | + * @see DeadLetterPublishingRecoverer#setHeadersFunction(BiFunction) |
| 1395 | + * @see DeadLetterPublishingRecoverer#addHeadersFunction(BiFunction) |
| 1396 | + */ |
| 1397 | + public static class SingleRecordHeader extends RecordHeader { |
| 1398 | + |
| 1399 | + /** |
| 1400 | + * Construct an instance. |
| 1401 | + * @param key the key. |
| 1402 | + * @param value the value. |
| 1403 | + */ |
| 1404 | + public SingleRecordHeader(String key, byte[] value) { |
| 1405 | + super(key, value); |
| 1406 | + } |
| 1407 | + |
| 1408 | + /** |
| 1409 | + * Construct an instance. |
| 1410 | + * @param keyBuffer the key buffer. |
| 1411 | + * @param valueBuffer the value buffer. |
| 1412 | + */ |
| 1413 | + public SingleRecordHeader(ByteBuffer keyBuffer, ByteBuffer valueBuffer) { |
| 1414 | + super(keyBuffer, valueBuffer); |
| 1415 | + } |
| 1416 | + |
| 1417 | + } |
| 1418 | + |
1377 | 1419 | }
|
0 commit comments