Skip to content

Commit 7a30178

Browse files
committed
spring-projectsGH-2155: DeadLetterPublishingRecoverer Improvement
Resolves spring-projects#2155 - Add a `BitSet` property to suppress individual standard headers - Support multiple `headersFunction` - Allow complete customization of exception headers - Add `setHeadersFunction` to the DLPR factory for retryable topics
1 parent f0ad7b0 commit 7a30178

File tree

7 files changed

+541
-35
lines changed

7 files changed

+541
-35
lines changed

spring-kafka-docs/src/main/asciidoc/kafka.adoc

+30-1
Original file line numberDiff line numberDiff line change
@@ -5641,7 +5641,7 @@ Key exceptions are only caused by `DeserializationException` s so there is no `D
56415641
There are two mechanisms to add more headers.
56425642

56435643
1. Subclass the recoverer and override `createProducerRecord()` - call `super.createProducerRecord()` and add more headers.
5644-
2. Provide a `BiFunction` to receive the consumer record and exception, returning a `Headers` object; headers from there will be copied to the final producer record.
5644+
2. Provide a `BiFunction` to receive the consumer record and exception, returning a `Headers` object; headers from there will be copied to the final producer record; also see <<dlpr-headers>>.
56455645
Use `setHeadersFunction()` to set the `BiFunction`.
56465646

56475647
The second is simpler to implement but the first has more information available, including the already assembled standard headers.
@@ -5736,6 +5736,35 @@ The reason for the two properties is because, while you might want to retain onl
57365736

57375737
`appendOriginalHeaders` is applied to all headers named `*ORIGINAL*` while `stripPreviousExceptionHeaders` is applied to all headers named `*EXCEPTION*`.
57385738

5739+
Starting with version 2.8.4, you now can control which of the standard headers will be added to the output record.
5740+
5741+
The there is a new property `whichHeaders`, which is a `BitSet`; for example, to suppress the addition of adding the stack trace header, use the following:
5742+
5743+
====
5744+
[source, java]
5745+
----
5746+
DeadLetterPublishingRecoverer recoverer = new DeadLetterPublishingRecoverer(template);
5747+
recoverer.getWhichHeaders().clear(HeaderNames.HeadersToAdd.EX_STACKTRACE.getBit());
5748+
----
5749+
====
5750+
5751+
See the `enum HeadersToAdd` for the bit names of the (currently) 10 standard headers that are added by default.
5752+
5753+
In addition, you can completely customize the addition of exception headers by adding an `ExceptionHeadersCreator`; this also disables all standard exception headers.
5754+
5755+
====
5756+
[source, java]
5757+
----
5758+
DeadLetterPublishingRecoverer recoverer = new DeadLetterPublishingRecoverer(template);
5759+
recoverer.setExceptionHeadersCreator((kafkaHeaders, exception, isKey, headerNames) -> {
5760+
kafkaHeaders.add(new RecordHeader(..., ...));
5761+
});
5762+
----
5763+
====
5764+
5765+
Also starting with version 2.8.4, you can now provide multiple headers functions, via the `addHeadersFunction` method.
5766+
This allows additional functions to apply, even if another function has already been registered, for example, when using <<retry-topic>>.
5767+
57395768
Also see <<retry-headers>> with <<retry-topic>>.
57405769

57415770
[[exp-backoff]]

spring-kafka-docs/src/main/asciidoc/retrytopic.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,8 @@ DeadLetterPublishingRecovererFactory factory(DestinationTopicResolver resolver)
433433
----
434434
====
435435

436+
Starting with version 2.8.4, if you wish to add custom headers (in addition to the retry information headers added by the factory, you can add a `headersFunction` to the factory - `factory.setHeadersFunction((rec, ex) -> { ... })`
437+
436438
[[retry-topic-combine-blocking]]
437439
==== Combining blocking and non-blocking retries
438440

spring-kafka-docs/src/main/asciidoc/whats-new.adoc

+2
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ See <<delegating-serialization>> for more information.
8383

8484
The property `stripPreviousExceptionHeaders` is now `true` by default.
8585

86+
There are now several techniques to customize which headers are added to the output record.
87+
8688
See <<dlpr-headers>> for more information.
8789

8890
[[x28-retryable-topics-changes]]

0 commit comments

Comments
 (0)