|
1 | 1 | /* |
2 | | - * Copyright 2018-2020 the original author or authors. |
| 2 | + * Copyright 2018-2021 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. |
@@ -168,24 +168,21 @@ public void setHeadersFunction(BiFunction<ConsumerRecord<?, ?>, Exception, Heade |
168 | 168 | @Override |
169 | 169 | public void accept(ConsumerRecord<?, ?> record, Exception exception) { |
170 | 170 | TopicPartition tp = this.destinationResolver.apply(record, exception); |
171 | | - boolean isKey = false; |
172 | | - DeserializationException deserEx = ListenerUtils.getExceptionFromHeader(record, |
| 171 | + DeserializationException vDeserEx = ListenerUtils.getExceptionFromHeader(record, |
173 | 172 | ErrorHandlingDeserializer.VALUE_DESERIALIZER_EXCEPTION_HEADER, this.logger); |
174 | | - if (deserEx == null) { |
175 | | - deserEx = ListenerUtils.getExceptionFromHeader(record, |
| 173 | + DeserializationException kDeserEx = ListenerUtils.getExceptionFromHeader(record, |
176 | 174 | ErrorHandlingDeserializer.KEY_DESERIALIZER_EXCEPTION_HEADER, this.logger); |
177 | | - isKey = true; |
| 175 | + Headers headers = new RecordHeaders(record.headers().toArray()); |
| 176 | + if (kDeserEx != null && !this.retainExceptionHeader) { |
| 177 | + headers.remove(ErrorHandlingDeserializer.KEY_DESERIALIZER_EXCEPTION_HEADER); |
| 178 | + addExceptionInfoHeaders(headers, kDeserEx, true); |
178 | 179 | } |
179 | | - Headers headers; |
180 | | - if (deserEx == null || this.retainExceptionHeader) { |
181 | | - headers = new RecordHeaders(record.headers().toArray()); |
182 | | - } |
183 | | - else { |
184 | | - headers = deserEx.getHeaders(); |
| 180 | + if (vDeserEx != null && !this.retainExceptionHeader) { |
| 181 | + headers.remove(ErrorHandlingDeserializer.VALUE_DESERIALIZER_EXCEPTION_HEADER); |
185 | 182 | } |
186 | 183 | enhanceHeaders(headers, record, exception); // NOSONAR headers are never null |
187 | 184 | ProducerRecord<Object, Object> outRecord = createProducerRecord(record, tp, headers, |
188 | | - deserEx == null ? null : deserEx.getData(), isKey); |
| 185 | + kDeserEx == null ? null : kDeserEx.getData(), vDeserEx == null ? null : vDeserEx.getData()); |
189 | 186 | KafkaOperations<Object, Object> kafkaTemplate = findTemplateForValue(outRecord.value()); |
190 | 187 | if (this.transactional && !kafkaTemplate.inTransaction() && !kafkaTemplate.isAllowNonTransactional()) { |
191 | 188 | kafkaTemplate.executeInTransaction(t -> { |
@@ -236,18 +233,18 @@ private KafkaOperations<Object, Object> findTemplateForValue(@Nullable Object va |
236 | 233 | * @param topicPartition the {@link TopicPartition} returned by the destination |
237 | 234 | * resolver. |
238 | 235 | * @param headers the headers - original record headers plus DLT headers. |
239 | | - * @param data the value to use instead of the consumer record value. |
240 | | - * @param isKey true if key deserialization failed. |
| 236 | + * @param key the key to use instead of the consumer record key. |
| 237 | + * @param value the value to use instead of the consumer record value. |
241 | 238 | * @return the producer record to send. |
242 | 239 | * @see KafkaHeaders |
243 | 240 | */ |
244 | 241 | protected ProducerRecord<Object, Object> createProducerRecord(ConsumerRecord<?, ?> record, |
245 | | - TopicPartition topicPartition, Headers headers, @Nullable byte[] data, boolean isKey) { |
| 242 | + TopicPartition topicPartition, Headers headers, @Nullable byte[] key, @Nullable byte[] value) { |
246 | 243 |
|
247 | 244 | return new ProducerRecord<>(topicPartition.topic(), |
248 | 245 | topicPartition.partition() < 0 ? null : topicPartition.partition(), |
249 | | - isKey && data != null ? data : record.key(), |
250 | | - data == null || isKey ? record.value() : data, headers); |
| 246 | + key != null ? key : record.key(), |
| 247 | + value != null ? value : record.value(), headers); |
251 | 248 | } |
252 | 249 |
|
253 | 250 | /** |
@@ -280,19 +277,27 @@ private void enhanceHeaders(Headers kafkaHeaders, ConsumerRecord<?, ?> record, E |
280 | 277 | ByteBuffer.allocate(Long.BYTES).putLong(record.timestamp()).array())); |
281 | 278 | kafkaHeaders.add(new RecordHeader(KafkaHeaders.DLT_ORIGINAL_TIMESTAMP_TYPE, |
282 | 279 | record.timestampType().toString().getBytes(StandardCharsets.UTF_8))); |
283 | | - kafkaHeaders.add(new RecordHeader(KafkaHeaders.DLT_EXCEPTION_FQCN, |
| 280 | + addExceptionInfoHeaders(kafkaHeaders, exception, false); |
| 281 | + Headers headers = this.headersFunction.apply(record, exception); |
| 282 | + if (headers != null) { |
| 283 | + headers.forEach(header -> kafkaHeaders.add(header)); |
| 284 | + } |
| 285 | + } |
| 286 | + |
| 287 | + private void addExceptionInfoHeaders(Headers kafkaHeaders, Exception exception, boolean isKey) { |
| 288 | + kafkaHeaders.add(new RecordHeader(isKey ? KafkaHeaders.DLT_KEY_EXCEPTION_FQCN : KafkaHeaders.DLT_EXCEPTION_FQCN, |
284 | 289 | exception.getClass().getName().getBytes(StandardCharsets.UTF_8))); |
285 | 290 | String message = exception.getMessage(); |
286 | 291 | if (message != null) { |
287 | | - kafkaHeaders.add(new RecordHeader(KafkaHeaders.DLT_EXCEPTION_MESSAGE, |
| 292 | + kafkaHeaders.add(new RecordHeader(isKey |
| 293 | + ? KafkaHeaders.DLT_KEY_EXCEPTION_MESSAGE |
| 294 | + : KafkaHeaders.DLT_EXCEPTION_MESSAGE, |
288 | 295 | exception.getMessage().getBytes(StandardCharsets.UTF_8))); |
289 | 296 | } |
290 | | - kafkaHeaders.add(new RecordHeader(KafkaHeaders.DLT_EXCEPTION_STACKTRACE, |
| 297 | + kafkaHeaders.add(new RecordHeader(isKey |
| 298 | + ? KafkaHeaders.DLT_KEY_EXCEPTION_STACKTRACE |
| 299 | + : KafkaHeaders.DLT_EXCEPTION_STACKTRACE, |
291 | 300 | getStackTraceAsString(exception).getBytes(StandardCharsets.UTF_8))); |
292 | | - Headers headers = this.headersFunction.apply(record, exception); |
293 | | - if (headers != null) { |
294 | | - headers.forEach(header -> kafkaHeaders.add(header)); |
295 | | - } |
296 | 301 | } |
297 | 302 |
|
298 | 303 | private String getStackTraceAsString(Throwable cause) { |
|
0 commit comments