|
37 | 37 | import org.springframework.kafka.listener.AfterRollbackProcessor;
|
38 | 38 | import org.springframework.kafka.listener.BatchErrorHandler;
|
39 | 39 | import org.springframework.kafka.listener.BatchInterceptor;
|
| 40 | +import org.springframework.kafka.listener.CommonErrorHandler; |
40 | 41 | import org.springframework.kafka.listener.ContainerProperties;
|
41 | 42 | import org.springframework.kafka.listener.ErrorHandler;
|
42 | 43 | import org.springframework.kafka.listener.GenericErrorHandler;
|
@@ -75,6 +76,8 @@ public abstract class AbstractKafkaListenerContainerFactory<C extends AbstractMe
|
75 | 76 |
|
76 | 77 | private GenericErrorHandler<?> errorHandler;
|
77 | 78 |
|
| 79 | + private CommonErrorHandler commonErrorHandler; |
| 80 | + |
78 | 81 | private ConsumerFactory<? super K, ? super V> consumerFactory;
|
79 | 82 |
|
80 | 83 | private Boolean autoStartup;
|
@@ -262,6 +265,16 @@ public void setBatchErrorHandler(BatchErrorHandler errorHandler) {
|
262 | 265 | this.errorHandler = errorHandler;
|
263 | 266 | }
|
264 | 267 |
|
| 268 | + /** |
| 269 | + * Set the {@link CommonErrorHandler} which can handle errors for both record |
| 270 | + * and batch listeners. Replaces the use of {@link GenericErrorHandler}s. |
| 271 | + * @param commonErrorHandler the handler. |
| 272 | + * @since 2.8 |
| 273 | + */ |
| 274 | + public void setCommonErrorHandler(CommonErrorHandler commonErrorHandler) { |
| 275 | + this.commonErrorHandler = commonErrorHandler; |
| 276 | + } |
| 277 | + |
265 | 278 | /**
|
266 | 279 | * Set a processor to invoke after a transaction rollback; typically will
|
267 | 280 | * seek the unprocessed topic/partition to reprocess the records.
|
@@ -342,7 +355,19 @@ public void setContainerCustomizer(ContainerCustomizer<K, V, C> containerCustomi
|
342 | 355 |
|
343 | 356 | @Override
|
344 | 357 | public void afterPropertiesSet() {
|
345 |
| - if (this.errorHandler != null) { |
| 358 | + if (this.commonErrorHandler != null) { |
| 359 | + if (Boolean.TRUE.equals(this.batchListener)) { |
| 360 | + Assert.state(this.commonErrorHandler.supportsBatch(), |
| 361 | + () -> "The common error handler must support batch listeners, it is a " + |
| 362 | + this.commonErrorHandler.getClass().getName()); |
| 363 | + } |
| 364 | + else { |
| 365 | + Assert.state(this.commonErrorHandler.supportsRecord(), |
| 366 | + () -> "The common error handler must support record listeners, it is a " + |
| 367 | + this.commonErrorHandler.getClass().getName()); |
| 368 | + } |
| 369 | + } |
| 370 | + if (this.commonErrorHandler == null && this.errorHandler != null) { |
346 | 371 | if (Boolean.TRUE.equals(this.batchListener)) {
|
347 | 372 | Assert.state(this.errorHandler instanceof BatchErrorHandler,
|
348 | 373 | () -> "The error handler must be a BatchErrorHandler, not " +
|
@@ -412,6 +437,7 @@ protected void initializeContainer(C instance, KafkaListenerEndpoint endpoint) {
|
412 | 437 | .acceptIfNotNull(this.containerProperties.getSubBatchPerPartition(),
|
413 | 438 | properties::setSubBatchPerPartition)
|
414 | 439 | .acceptIfNotNull(this.errorHandler, instance::setGenericErrorHandler)
|
| 440 | + .acceptIfNotNull(this.commonErrorHandler, instance::setCommonErrorHandler) |
415 | 441 | .acceptIfNotNull(this.missingTopicsFatal, instance.getContainerProperties()::setMissingTopicsFatal);
|
416 | 442 | Boolean autoStart = endpoint.getAutoStartup();
|
417 | 443 | if (autoStart != null) {
|
|
0 commit comments