|
1 | 1 | /* |
2 | | - * Copyright 2018-2023 the original author or authors. |
| 2 | + * Copyright 2018-2024 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. |
|
54 | 54 | * |
55 | 55 | * @author Tomaz Fernandes |
56 | 56 | * @author Gary Russell |
| 57 | + * @author Soby Chacko |
57 | 58 | * @since 2.7 |
58 | 59 | * |
59 | 60 | */ |
@@ -237,6 +238,35 @@ public DeadLetterPublishingRecoverer create(String mainListenerId) { |
237 | 238 | return recoverer; |
238 | 239 | } |
239 | 240 |
|
| 241 | + /** |
| 242 | + * Log the exception before sending the record in error to the retry topic. |
| 243 | + * This method can be overridden by downstream applications to customize how the error is logged. |
| 244 | + * @param exception the exception that caused the error |
| 245 | + * @param consumerRecord the original consumer record |
| 246 | + * @param nextDestination the next topic where the record goes |
| 247 | + * @since 3.3.0 |
| 248 | + */ |
| 249 | + protected void maybeLogListenerException(Exception exception, ConsumerRecord<?, ?> consumerRecord, DestinationTopic nextDestination) { |
| 250 | + if (nextDestination.isDltTopic() |
| 251 | + && !ListenerExceptionLoggingStrategy.NEVER.equals(this.loggingStrategy)) { |
| 252 | + LOGGER.error(exception, () -> getErrorMessage(consumerRecord) + " and won't be retried. " |
| 253 | + + "Sending to DLT with name " + nextDestination.getDestinationName() + "."); |
| 254 | + } |
| 255 | + else if (nextDestination.isNoOpsTopic() |
| 256 | + && !ListenerExceptionLoggingStrategy.NEVER.equals(this.loggingStrategy)) { |
| 257 | + LOGGER.error(exception, () -> getErrorMessage(consumerRecord) + " and won't be retried. " |
| 258 | + + "No further action will be taken with this record."); |
| 259 | + } |
| 260 | + else if (ListenerExceptionLoggingStrategy.EACH_ATTEMPT.equals(this.loggingStrategy)) { |
| 261 | + LOGGER.error(exception, () -> getErrorMessage(consumerRecord) + ". " |
| 262 | + + "Sending to retry topic " + nextDestination.getDestinationName() + "."); |
| 263 | + } |
| 264 | + else { |
| 265 | + LOGGER.debug(exception, () -> getErrorMessage(consumerRecord) + ". " |
| 266 | + + "Sending to retry topic " + nextDestination.getDestinationName() + "."); |
| 267 | + } |
| 268 | + } |
| 269 | + |
240 | 270 | private DeadLetterPublishingRecoverer create( |
241 | 271 | Function<ProducerRecord<?, ?>, KafkaOperations<?, ?>> templateResolver, |
242 | 272 | BiFunction<ConsumerRecord<?, ?>, Exception, TopicPartition> destinationResolver) { |
@@ -271,27 +301,6 @@ private DeadLetterPublishingRecoverer create( |
271 | 301 | }; |
272 | 302 | } |
273 | 303 |
|
274 | | - private void maybeLogListenerException(Exception e, ConsumerRecord<?, ?> cr, DestinationTopic nextDestination) { |
275 | | - if (nextDestination.isDltTopic() |
276 | | - && !ListenerExceptionLoggingStrategy.NEVER.equals(this.loggingStrategy)) { |
277 | | - LOGGER.error(e, () -> getErrorMessage(cr) + " and won't be retried. " |
278 | | - + "Sending to DLT with name " + nextDestination.getDestinationName() + "."); |
279 | | - } |
280 | | - else if (nextDestination.isNoOpsTopic() |
281 | | - && !ListenerExceptionLoggingStrategy.NEVER.equals(this.loggingStrategy)) { |
282 | | - LOGGER.error(e, () -> getErrorMessage(cr) + " and won't be retried. " |
283 | | - + "No further action will be taken with this record."); |
284 | | - } |
285 | | - else if (ListenerExceptionLoggingStrategy.EACH_ATTEMPT.equals(this.loggingStrategy)) { |
286 | | - LOGGER.error(e, () -> getErrorMessage(cr) + ". " |
287 | | - + "Sending to retry topic " + nextDestination.getDestinationName() + "."); |
288 | | - } |
289 | | - else { |
290 | | - LOGGER.debug(e, () -> getErrorMessage(cr) + ". " |
291 | | - + "Sending to retry topic " + nextDestination.getDestinationName() + "."); |
292 | | - } |
293 | | - } |
294 | | - |
295 | 304 | private static String getErrorMessage(ConsumerRecord<?, ?> cr) { |
296 | 305 | return "Record: " + getRecordInfo(cr) + " threw an error at topic " + cr.topic(); |
297 | 306 | } |
|
0 commit comments