-
Notifications
You must be signed in to change notification settings - Fork 624
GH-2985: Add Kafka Listener Container Customizer interfaces and docum… #3015
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
.../java/org/springframework/cloud/stream/binder/kafka/KafkaListenerContainerCustomizer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
/* | ||
* Copyright 2024-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.stream.binder.kafka; | ||
|
||
import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; | ||
import org.springframework.cloud.stream.binder.kafka.properties.KafkaConsumerProperties; | ||
import org.springframework.cloud.stream.config.ListenerContainerCustomizer; | ||
import org.springframework.kafka.listener.AbstractMessageListenerContainer; | ||
|
||
/** | ||
* Extension of {@link ListenerContainerCustomizer} specific to Kafka binder. | ||
* This interface allows for customization of Kafka listener containers with | ||
* access to Kafka-specific extended consumer properties. | ||
* | ||
* @author Soby Chacko | ||
* @since 4.2.0 | ||
*/ | ||
public interface KafkaListenerContainerCustomizer extends ListenerContainerCustomizer<AbstractMessageListenerContainer<?, ?>> { | ||
|
||
/** | ||
* Configure the Kafka listener container with access to extended consumer properties. | ||
* | ||
* @param container the Kafka message listener container to configure | ||
* @param destinationName the name of the destination (topic) that this listener container is associated with | ||
* @param group the consumer group name | ||
* @param extendedConsumerProperties the extended consumer properties specific to Kafka | ||
*/ | ||
default void configure(AbstractMessageListenerContainer<?, ?> container, String destinationName, String group, | ||
ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties) { | ||
configure(container, destinationName, group); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
162 changes: 162 additions & 0 deletions
162
.../org/springframework/cloud/stream/binder/kafka/KafkaListenerContainerCustomizerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
/* | ||
* Copyright 2024-2024 the original author or authors. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.cloud.stream.binder.kafka; | ||
|
||
import java.util.function.Consumer; | ||
|
||
import org.junit.jupiter.api.Test; | ||
import org.mockito.ArgumentCaptor; | ||
|
||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.cloud.stream.binder.ExtendedConsumerProperties; | ||
import org.springframework.cloud.stream.binder.kafka.config.KafkaBinderConfiguration; | ||
import org.springframework.cloud.stream.binder.kafka.properties.KafkaConsumerProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.kafka.listener.AbstractMessageListenerContainer; | ||
import org.springframework.kafka.test.context.EmbeddedKafka; | ||
import org.springframework.test.annotation.DirtiesContext; | ||
|
||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.mockito.ArgumentMatchers.any; | ||
import static org.mockito.ArgumentMatchers.eq; | ||
import static org.mockito.ArgumentMatchers.isNull; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.timeout; | ||
import static org.mockito.Mockito.verify; | ||
|
||
/** | ||
* @author Soby Chacko | ||
* @since 4.2.0 | ||
*/ | ||
@SpringBootTest( | ||
classes = {KafkaBinderConfiguration.class, KafkaListenerContainerCustomizerTests.TestConfig.class}, | ||
properties = { | ||
"spring.cloud.function.definition=testConsumer", | ||
"spring.cloud.stream.bindings.testConsumer-in-0.destination=test-topic", | ||
"spring.cloud.stream.bindings.testConsumer-in-0.group=test-group", | ||
"spring.cloud.stream.kafka.bindings.testConsumer-in-0.consumer.enableDlq=true" | ||
} | ||
) | ||
@DirtiesContext | ||
@EmbeddedKafka(partitions = 1, topics = "test-topic") | ||
class KafkaListenerContainerCustomizerTests { | ||
|
||
@Autowired | ||
private KafkaListenerContainerCustomizer compositeCustomizer; | ||
|
||
@Test | ||
@SuppressWarnings("unchecked") | ||
void customizersInvoked() { | ||
KafkaListenerContainerCustomizer kafkaCustomizer = | ||
((TestConfig.CompositeCustomizer) compositeCustomizer).getKafkaCustomizer(); | ||
ListenerContainerWithDlqAndRetryCustomizer dlqCustomizer = | ||
((TestConfig.CompositeCustomizer) compositeCustomizer).getDlqCustomizer(); | ||
|
||
ArgumentCaptor<ExtendedConsumerProperties<KafkaConsumerProperties>> kafkaPropertiesCaptor = ArgumentCaptor.forClass(ExtendedConsumerProperties.class); | ||
ArgumentCaptor<ExtendedConsumerProperties<KafkaConsumerProperties>> dlqPropertiesCaptor = ArgumentCaptor.forClass(ExtendedConsumerProperties.class); | ||
|
||
verify(kafkaCustomizer, timeout(5000).times(1)).configure( | ||
any(AbstractMessageListenerContainer.class), | ||
eq("test-topic"), | ||
eq("test-group"), | ||
kafkaPropertiesCaptor.capture() | ||
); | ||
|
||
verify(dlqCustomizer, timeout(5000).times(1)).configure( | ||
any(AbstractMessageListenerContainer.class), | ||
eq("test-topic"), | ||
eq("test-group"), | ||
isNull(), | ||
isNull(), | ||
dlqPropertiesCaptor.capture() | ||
); | ||
|
||
ExtendedConsumerProperties<KafkaConsumerProperties> kafkaProperties = kafkaPropertiesCaptor.getValue(); | ||
ExtendedConsumerProperties<KafkaConsumerProperties> dlqProperties = dlqPropertiesCaptor.getValue(); | ||
|
||
// Assert common properties | ||
assertThat(kafkaProperties.getBindingName()).isEqualTo("testConsumer-in-0"); | ||
assertThat(dlqProperties.getBindingName()).isEqualTo("testConsumer-in-0"); | ||
|
||
// Assert Kafka-specific properties | ||
assertThat(kafkaProperties.getExtension()) | ||
.satisfies(extension -> { | ||
assertThat(extension.isEnableDlq()).isTrue(); | ||
assertThat(extension.isAutoRebalanceEnabled()).isTrue(); | ||
}); | ||
|
||
// Assert that both captured properties are the same instance | ||
assertThat(kafkaProperties).isSameAs(dlqProperties); | ||
} | ||
|
||
@Configuration | ||
@EnableAutoConfiguration | ||
static class TestConfig { | ||
|
||
@Bean | ||
public Consumer<String> testConsumer() { | ||
return message -> { | ||
// Do nothing, just to trigger consumer binding | ||
}; | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public KafkaListenerContainerCustomizer compositeCustomizer() { | ||
return new CompositeCustomizer( | ||
mock(KafkaListenerContainerCustomizer.class), | ||
mock(ListenerContainerWithDlqAndRetryCustomizer.class) | ||
); | ||
} | ||
|
||
static class CompositeCustomizer implements KafkaListenerContainerCustomizer { | ||
private final KafkaListenerContainerCustomizer kafkaCustomizer; | ||
private final ListenerContainerWithDlqAndRetryCustomizer dlqCustomizer; | ||
|
||
CompositeCustomizer(KafkaListenerContainerCustomizer kafkaCustomizer, | ||
ListenerContainerWithDlqAndRetryCustomizer dlqCustomizer) { | ||
this.kafkaCustomizer = kafkaCustomizer; | ||
this.dlqCustomizer = dlqCustomizer; | ||
} | ||
|
||
@Override | ||
public void configure(AbstractMessageListenerContainer<?, ?> container, String destinationName, | ||
String group, ExtendedConsumerProperties<KafkaConsumerProperties> extendedConsumerProperties) { | ||
kafkaCustomizer.configure(container, destinationName, group, extendedConsumerProperties); | ||
dlqCustomizer.configure(container, destinationName, group, null, null, extendedConsumerProperties); | ||
} | ||
|
||
public KafkaListenerContainerCustomizer getKafkaCustomizer() { | ||
return kafkaCustomizer; | ||
} | ||
|
||
public ListenerContainerWithDlqAndRetryCustomizer getDlqCustomizer() { | ||
return dlqCustomizer; | ||
} | ||
|
||
@Override | ||
public void configure(AbstractMessageListenerContainer<?, ?> container, String destinationName, String group) { | ||
|
||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use the JSR-250 @resource annotation (jakarta.annotation.Resource)
https://docs.spring.io/spring-framework/reference/core/beans/annotation-config/resource.html