|
1 | 1 | /*
|
2 |
| - * Copyright 2021 the original author or authors. |
| 2 | + * Copyright 2021-2022 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.
|
@@ -41,13 +41,25 @@ public interface CommonErrorHandler extends DeliveryAttemptAware {
|
41 | 41 | * When true (default), all remaining records including the failed record are passed
|
42 | 42 | * to the error handler.
|
43 | 43 | * @return false to receive only the failed record.
|
| 44 | + * @deprecated in favor of {@link #seeksAfterHandling()}. |
44 | 45 | * @see #handleRecord(Exception, ConsumerRecord, Consumer, MessageListenerContainer)
|
45 | 46 | * @see #handleRemaining(Exception, List, Consumer, MessageListenerContainer)
|
46 | 47 | */
|
| 48 | + @Deprecated |
47 | 49 | default boolean remainingRecords() {
|
48 | 50 | return false;
|
49 | 51 | }
|
50 | 52 |
|
| 53 | + /** |
| 54 | + * Return true if this error handler performs seeks on the failed record and remaining |
| 55 | + * records (or just the remaining records after a failed record is recovered). |
| 56 | + * @return true if the next poll should fetch records. |
| 57 | + */ |
| 58 | + @SuppressWarnings("deprecation") |
| 59 | + default boolean seeksAfterHandling() { |
| 60 | + return remainingRecords(); |
| 61 | + } |
| 62 | + |
51 | 63 | /**
|
52 | 64 | * Return true if this error handler supports delivery attempts headers.
|
53 | 65 | * @return true if capable.
|
@@ -79,14 +91,42 @@ default void handleOtherException(Exception thrownException, Consumer<?, ?> cons
|
79 | 91 | * @param record the record.
|
80 | 92 | * @param consumer the consumer.
|
81 | 93 | * @param container the container.
|
| 94 | + * @deprecated in favor of |
| 95 | + * {@link #handleOne(Exception, ConsumerRecord, Consumer, MessageListenerContainer)}. |
82 | 96 | * @see #remainingRecords()
|
83 | 97 | */
|
| 98 | + @Deprecated |
84 | 99 | default void handleRecord(Exception thrownException, ConsumerRecord<?, ?> record, Consumer<?, ?> consumer,
|
85 | 100 | MessageListenerContainer container) {
|
86 | 101 |
|
87 | 102 | LogFactory.getLog(getClass()).error("'handleRecord' is not implemented by this handler", thrownException);
|
88 | 103 | }
|
89 | 104 |
|
| 105 | + /** |
| 106 | + * Handle the exception for a record listener when {@link #remainingRecords()} returns |
| 107 | + * false. Use this to handle just the single failed record. |
| 108 | + * @param thrownException the exception. |
| 109 | + * @param record the record. |
| 110 | + * @param consumer the consumer. |
| 111 | + * @param container the container. |
| 112 | + * @return true if the error was "handled" or false if not and the container will |
| 113 | + * re-submit the record to the listener. |
| 114 | + * @since 2.9 |
| 115 | + * @see #remainingRecords() |
| 116 | + */ |
| 117 | + @SuppressWarnings("deprecation") |
| 118 | + default boolean handleOne(Exception thrownException, ConsumerRecord<?, ?> record, Consumer<?, ?> consumer, |
| 119 | + MessageListenerContainer container) { |
| 120 | + |
| 121 | + try { |
| 122 | + handleRecord(thrownException, record, consumer, container); |
| 123 | + return true; |
| 124 | + } |
| 125 | + catch (Exception ex) { |
| 126 | + return false; |
| 127 | + } |
| 128 | + } |
| 129 | + |
90 | 130 | /**
|
91 | 131 | * Handle the exception for a record listener when {@link #remainingRecords()} returns
|
92 | 132 | * true. The failed record and all the remaining records from the poll are passed in.
|
@@ -120,6 +160,28 @@ default void handleBatch(Exception thrownException, ConsumerRecords<?, ?> data,
|
120 | 160 | LogFactory.getLog(getClass()).error("'handleBatch' is not implemented by this handler", thrownException);
|
121 | 161 | }
|
122 | 162 |
|
| 163 | + /** |
| 164 | + * Handle the exception for a batch listener. The complete {@link ConsumerRecords} |
| 165 | + * from the poll is supplied. Return the members of the batch that should be re-sent to |
| 166 | + * the listener. The returned records MUST be in the same order as the original records. |
| 167 | + * @param thrownException the exception. |
| 168 | + * @param data the consumer records. |
| 169 | + * @param consumer the consumer. |
| 170 | + * @param container the container. |
| 171 | + * @param invokeListener a callback to re-invoke the listener. |
| 172 | + * @param <K> the key type. |
| 173 | + * @param <V> the value type. |
| 174 | + * @return the consumer records, or a subset. |
| 175 | + * @since 2.9 |
| 176 | + */ |
| 177 | + default <K, V> ConsumerRecords<K, V> handleBatchAndReturnRemaining(Exception thrownException, |
| 178 | + ConsumerRecords<?, ?> data, Consumer<?, ?> consumer, MessageListenerContainer container, |
| 179 | + Runnable invokeListener) { |
| 180 | + |
| 181 | + handleBatch(thrownException, data, consumer, container, invokeListener); |
| 182 | + return ConsumerRecords.empty(); |
| 183 | + } |
| 184 | + |
123 | 185 | @Override
|
124 | 186 | default int deliveryAttempt(TopicPartitionOffset topicPartitionOffset) {
|
125 | 187 | return 0;
|
|
0 commit comments