-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add KafkaListener support for shared consumer containers #3988
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
artembilan
merged 6 commits into
spring-projects:main
from
sobychacko:KL-shared-consumer
Jul 3, 2025
+355
−22
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
69f4e3b
Add KafkaListener support for shared consumer containers
sobychacko 8ae7ff9
Addressing PR review
sobychacko 20978de
Addressing PR review
sobychacko e9ee474
Addressing PR review
sobychacko d0f6965
Addressing PR review
sobychacko 8829ac9
Addressing PR review
sobychacko 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
171 changes: 171 additions & 0 deletions
171
...ka/src/main/java/org/springframework/kafka/config/ShareKafkaListenerContainerFactory.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,171 @@ | ||
/* | ||
* Copyright 2025-present 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.kafka.config; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collection; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jspecify.annotations.Nullable; | ||
|
||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.kafka.core.ShareConsumerFactory; | ||
import org.springframework.kafka.listener.ContainerProperties; | ||
import org.springframework.kafka.listener.ShareKafkaMessageListenerContainer; | ||
import org.springframework.kafka.support.JavaUtils; | ||
import org.springframework.kafka.support.TopicPartitionOffset; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* A {@link KafkaListenerContainerFactory} implementation to create {@link ShareKafkaMessageListenerContainer} | ||
* instances for Kafka's share consumer model. | ||
* <p> | ||
* This factory provides common configuration and lifecycle management for share consumer containers. | ||
* It handles the creation of containers based on endpoints, topics, or patterns, and applies common | ||
* configuration properties to the created containers. | ||
* <p> | ||
* The share consumer model enables cooperative rebalancing, allowing consumers to maintain ownership of | ||
* some partitions while relinquishing others during rebalances, which can reduce disruption compared to | ||
* the classic consumer model. | ||
* | ||
* @param <K> the key type | ||
* @param <V> the value type | ||
* | ||
* @author Soby Chacko | ||
* @since 4.0 | ||
*/ | ||
public class ShareKafkaListenerContainerFactory<K, V> | ||
implements KafkaListenerContainerFactory<ShareKafkaMessageListenerContainer<K, V>>, ApplicationEventPublisherAware, ApplicationContextAware { | ||
|
||
private final ShareConsumerFactory<? super K, ? super V> shareConsumerFactory; | ||
|
||
private @Nullable Boolean autoStartup; | ||
|
||
private @Nullable Integer phase; | ||
|
||
private @Nullable ApplicationEventPublisher applicationEventPublisher; | ||
|
||
private @Nullable ApplicationContext applicationContext; | ||
|
||
/** | ||
* Construct an instance with the provided consumer factory. | ||
* @param shareConsumerFactory the share consumer factory | ||
*/ | ||
public ShareKafkaListenerContainerFactory(ShareConsumerFactory<K, V> shareConsumerFactory) { | ||
this.shareConsumerFactory = shareConsumerFactory; | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
/** | ||
* Set whether containers created by this factory should auto-start. | ||
* @param autoStartup true to auto-start | ||
*/ | ||
public void setAutoStartup(Boolean autoStartup) { | ||
this.autoStartup = autoStartup; | ||
} | ||
|
||
/** | ||
* Set the phase in which containers created by this factory should start and stop. | ||
* @param phase the phase | ||
*/ | ||
public void setPhase(Integer phase) { | ||
this.phase = phase; | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | ||
this.applicationEventPublisher = applicationEventPublisher; | ||
} | ||
|
||
@Override | ||
@SuppressWarnings({"unchecked", "rawtypes"}) | ||
public ShareKafkaMessageListenerContainer<K, V> createListenerContainer(KafkaListenerEndpoint endpoint) { | ||
ShareKafkaMessageListenerContainer<K, V> instance = createContainerInstance(endpoint); | ||
JavaUtils.INSTANCE | ||
.acceptIfNotNull(endpoint.getId(), instance::setBeanName); | ||
if (endpoint instanceof AbstractKafkaListenerEndpoint abstractKafkaListenerEndpoint) { | ||
configureEndpoint(abstractKafkaListenerEndpoint); | ||
} | ||
// TODO: No message converter for queue at the moment | ||
endpoint.setupListenerContainer(instance, null); | ||
initializeContainer(instance, endpoint); | ||
return instance; | ||
} | ||
|
||
private void configureEndpoint(AbstractKafkaListenerEndpoint<K, V> endpoint) { | ||
// Minimal configuration; can add more properties later | ||
} | ||
|
||
/** | ||
* Initialize the provided container with common configuration properties. | ||
* @param instance the container instance | ||
* @param endpoint the endpoint | ||
*/ | ||
protected void initializeContainer(ShareKafkaMessageListenerContainer<K, V> instance, KafkaListenerEndpoint endpoint) { | ||
ContainerProperties properties = instance.getContainerProperties(); | ||
Boolean effectiveAutoStartup = endpoint.getAutoStartup() != null ? endpoint.getAutoStartup() : this.autoStartup; | ||
JavaUtils.INSTANCE | ||
.acceptIfNotNull(effectiveAutoStartup, instance::setAutoStartup) | ||
.acceptIfNotNull(this.phase, instance::setPhase) | ||
.acceptIfNotNull(this.applicationContext, instance::setApplicationContext) | ||
.acceptIfNotNull(this.applicationEventPublisher, instance::setApplicationEventPublisher) | ||
.acceptIfNotNull(endpoint.getGroupId(), properties::setGroupId) | ||
.acceptIfNotNull(endpoint.getClientIdPrefix(), properties::setClientId) | ||
.acceptIfNotNull(endpoint.getConsumerProperties(), properties::setKafkaConsumerProperties); | ||
} | ||
|
||
@Override | ||
public ShareKafkaMessageListenerContainer<K, V> createContainer(TopicPartitionOffset... topicPartitions) { | ||
throw new UnsupportedOperationException("ShareConsumer does not support explicit partition assignment"); | ||
} | ||
|
||
@Override | ||
public ShareKafkaMessageListenerContainer<K, V> createContainer(String... topics) { | ||
return createContainerInstance(new KafkaListenerEndpointAdapter() { | ||
|
||
@Override | ||
public Collection<String> getTopics() { | ||
return Arrays.asList(topics); | ||
} | ||
}); | ||
} | ||
|
||
@Override | ||
public ShareKafkaMessageListenerContainer<K, V> createContainer(Pattern topicPattern) { | ||
throw new UnsupportedOperationException("ShareConsumer does not support topic patterns"); | ||
} | ||
|
||
/** | ||
* Create a container instance for the provided endpoint. | ||
* @param endpoint the endpoint | ||
* @return the container instance | ||
*/ | ||
protected ShareKafkaMessageListenerContainer<K, V> createContainerInstance(KafkaListenerEndpoint endpoint) { | ||
Collection<String> topics = endpoint.getTopics(); | ||
Assert.state(topics != null, "'topics' must not be null"); | ||
return new ShareKafkaMessageListenerContainer<>(this.shareConsumerFactory, | ||
new ContainerProperties(topics.toArray(new String[0]))); | ||
} | ||
|
||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.