diff --git a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/message-listener-container.adoc b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/message-listener-container.adoc index 2cf7287375..48ef1a0fd7 100644 --- a/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/message-listener-container.adoc +++ b/spring-kafka-docs/src/main/antora/modules/ROOT/pages/kafka/receiving-messages/message-listener-container.adoc @@ -22,25 +22,24 @@ IMPORTANT: If the interceptor mutates the record (by creating a new one), the `t The `CompositeRecordInterceptor` and `CompositeBatchInterceptor` can be used to invoke multiple interceptors. -Starting with version 4.0, `AbstractMessageListenerContainer` exposes `getRecordInterceptor()` and `getBatchInterceptor()` as public methods. +Starting with version 4.0, `AbstractKafkaListenerContainerFactory` and `AbstractMessageListenerContainer` exposes `getRecordInterceptor()` and `getBatchInterceptor()` as public methods. If the returned interceptor is an instance of `CompositeRecordInterceptor` or `CompositeBatchInterceptor`, additional `RecordInterceptor` or `BatchInterceptor` instances can be added to it even after the container instance extending `AbstractMessageListenerContainer` has been created and a `RecordInterceptor` or `BatchInterceptor` has already been configured. The following example shows how to do so: [source, java] ---- -public void configureRecordInterceptor(KafkaMessageListenerContainer container) { +public void configureRecordInterceptor(AbstractKafkaListenerContainerFactory containerFactory) { CompositeRecordInterceptor compositeInterceptor; - RecordInterceptor previousInterceptor = container.getRecordInterceptor(); + RecordInterceptor previousInterceptor = containerFactory.getRecordInterceptor(); if (previousInterceptor instanceof CompositeRecordInterceptor interceptor) { compositeInterceptor = interceptor; } else { compositeInterceptor = new CompositeRecordInterceptor<>(); - container.setRecordInterceptor(compositeInterceptor); - } - - if (previousInterceptor != null) { - compositeRecordInterceptor.addRecordInterceptor(previousInterceptor); + containerFactory.setRecordInterceptor(compositeInterceptor); + if (previousInterceptor != null) { + compositeRecordInterceptor.addRecordInterceptor(previousInterceptor); + } } RecordInterceptor recordInterceptor1 = new RecordInterceptor() {...}; diff --git a/spring-kafka/src/main/java/org/springframework/kafka/config/AbstractKafkaListenerContainerFactory.java b/spring-kafka/src/main/java/org/springframework/kafka/config/AbstractKafkaListenerContainerFactory.java index 5f09a3a947..d26ef01dc6 100644 --- a/spring-kafka/src/main/java/org/springframework/kafka/config/AbstractKafkaListenerContainerFactory.java +++ b/spring-kafka/src/main/java/org/springframework/kafka/config/AbstractKafkaListenerContainerFactory.java @@ -61,6 +61,7 @@ * @author Stephane Nicoll * @author Gary Russell * @author Artem Bilan + * @author Christian Fredriksson * * @see AbstractMessageListenerContainer */ @@ -275,6 +276,15 @@ public ContainerProperties getContainerProperties() { return this.containerProperties; } + /** + * Get the {@link RecordInterceptor} for modification, if configured. + * @return the {@link RecordInterceptor}, or {@code null} if not configured + * @since 4.0 + */ + public @Nullable RecordInterceptor getRecordInterceptor() { + return this.recordInterceptor; + } + /** * Set an interceptor to be called before calling the listener. * Only used with record listeners. @@ -286,6 +296,15 @@ public void setRecordInterceptor(RecordInterceptor recordInterceptor) { this.recordInterceptor = recordInterceptor; } + /** + * Get the {@link BatchInterceptor} for modification, if configured. + * @return the {@link BatchInterceptor}, or {@code null} if not configured + * @since 4.0 + */ + public @Nullable BatchInterceptor getBatchInterceptor() { + return this.batchInterceptor; + } + /** * Set a batch interceptor to be called before and after calling the listener. * Only used with batch listeners.