-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add Kafka shared consumer container support #3945
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 3 commits into
spring-projects:main
from
sobychacko:shared-consumer-iteration-3
Jun 6, 2025
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
247 changes: 247 additions & 0 deletions
247
...n/java/org/springframework/kafka/listener/AbstractShareKafkaMessageListenerContainer.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,247 @@ | ||
/* | ||
* Copyright 2025-2025 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.listener; | ||
|
||
import java.util.concurrent.locks.ReentrantLock; | ||
import java.util.regex.Pattern; | ||
|
||
import org.apache.commons.logging.LogFactory; | ||
import org.apache.kafka.clients.consumer.ConsumerConfig; | ||
import org.jspecify.annotations.Nullable; | ||
|
||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.beans.BeansException; | ||
import org.springframework.beans.factory.BeanNameAware; | ||
import org.springframework.context.ApplicationContext; | ||
import org.springframework.context.ApplicationContextAware; | ||
import org.springframework.context.ApplicationEventPublisher; | ||
import org.springframework.context.ApplicationEventPublisherAware; | ||
import org.springframework.core.log.LogAccessor; | ||
import org.springframework.kafka.core.ShareConsumerFactory; | ||
import org.springframework.kafka.support.TopicPartitionOffset; | ||
import org.springframework.util.Assert; | ||
|
||
/** | ||
* Abstract base class for share consumer message listener containers. | ||
* <p> | ||
* Handles common lifecycle, configuration, and event publishing for containers using a | ||
* {@link org.springframework.kafka.core.ShareConsumerFactory}. | ||
* <p> | ||
* Subclasses are responsible for implementing the actual consumer loop and message dispatch logic. | ||
* | ||
* @param <K> the key type | ||
* @param <V> the value type | ||
* | ||
* @author Soby Chacko | ||
* @since 4.0 | ||
*/ | ||
public abstract class AbstractShareKafkaMessageListenerContainer<K, V> | ||
implements GenericMessageListenerContainer<K, V>, BeanNameAware, ApplicationEventPublisherAware, | ||
ApplicationContextAware { | ||
|
||
/** | ||
* The default {@link org.springframework.context.SmartLifecycle} phase for listener containers. | ||
*/ | ||
public static final int DEFAULT_PHASE = Integer.MAX_VALUE - 100; | ||
|
||
protected final ShareConsumerFactory<K, V> shareConsumerFactory; | ||
|
||
protected final LogAccessor logger = new LogAccessor(LogFactory.getLog(this.getClass())); | ||
|
||
private final ContainerProperties containerProperties; | ||
|
||
protected final ReentrantLock lifecycleLock = new ReentrantLock(); | ||
|
||
private String beanName = "noBeanNameSet"; | ||
|
||
@Nullable | ||
private ApplicationEventPublisher applicationEventPublisher; | ||
|
||
private boolean autoStartup = true; | ||
|
||
private int phase = DEFAULT_PHASE; | ||
|
||
@Nullable | ||
private ApplicationContext applicationContext; | ||
|
||
private volatile boolean running = false; | ||
|
||
/** | ||
* Construct an instance with the provided factory and properties. | ||
* @param shareConsumerFactory the factory. | ||
* @param containerProperties the properties. | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
protected AbstractShareKafkaMessageListenerContainer(@Nullable ShareConsumerFactory<? super K, ? super V> shareConsumerFactory, | ||
ContainerProperties containerProperties) { | ||
Assert.notNull(containerProperties, "'containerProperties' cannot be null"); | ||
Assert.notNull(shareConsumerFactory, "'shareConsumerFactory' cannot be null"); | ||
this.shareConsumerFactory = (ShareConsumerFactory<K, V>) shareConsumerFactory; | ||
@Nullable String @Nullable [] topics = containerProperties.getTopics(); | ||
if (topics != null) { | ||
this.containerProperties = new ContainerProperties(topics); | ||
} | ||
else { | ||
Pattern topicPattern = containerProperties.getTopicPattern(); | ||
if (topicPattern != null) { | ||
this.containerProperties = new ContainerProperties(topicPattern); | ||
} | ||
else { | ||
@Nullable TopicPartitionOffset @Nullable [] topicPartitions = containerProperties.getTopicPartitions(); | ||
if (topicPartitions != null) { | ||
this.containerProperties = new ContainerProperties(topicPartitions); | ||
} | ||
else { | ||
throw new IllegalStateException("topics, topicPattern, or topicPartitions must be provided"); | ||
} | ||
} | ||
} | ||
BeanUtils.copyProperties(containerProperties, this.containerProperties, | ||
"topics", "topicPartitions", "topicPattern"); | ||
} | ||
|
||
@Override | ||
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | ||
this.applicationContext = applicationContext; | ||
} | ||
|
||
@Nullable | ||
public ApplicationContext getApplicationContext() { | ||
return this.applicationContext; | ||
} | ||
|
||
@Override | ||
public void setBeanName(String name) { | ||
this.beanName = name; | ||
} | ||
|
||
/** | ||
* Return the bean name. | ||
* @return the bean name | ||
*/ | ||
public String getBeanName() { | ||
return this.beanName; | ||
} | ||
|
||
@Override | ||
public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | ||
this.applicationEventPublisher = applicationEventPublisher; | ||
} | ||
|
||
/** | ||
* Get the event publisher. | ||
* @return the publisher | ||
*/ | ||
@Nullable | ||
public ApplicationEventPublisher getApplicationEventPublisher() { | ||
return this.applicationEventPublisher; | ||
} | ||
|
||
@Override | ||
public boolean isAutoStartup() { | ||
return this.autoStartup; | ||
} | ||
|
||
@Override | ||
public void setAutoStartup(boolean autoStartup) { | ||
this.autoStartup = autoStartup; | ||
} | ||
|
||
@Override | ||
public int getPhase() { | ||
return this.phase; | ||
} | ||
|
||
public void setPhase(int phase) { | ||
this.phase = phase; | ||
} | ||
|
||
@Override | ||
public void stop(Runnable callback) { | ||
stop(); | ||
callback.run(); | ||
} | ||
|
||
@Override | ||
public void start() { | ||
this.lifecycleLock.lock(); | ||
try { | ||
if (!isRunning()) { | ||
Assert.state(this.containerProperties.getMessageListener() instanceof GenericMessageListener, | ||
() -> "A " + GenericMessageListener.class.getName() + " implementation must be provided"); | ||
doStart(); | ||
} | ||
} | ||
finally { | ||
this.lifecycleLock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public void stop() { | ||
this.lifecycleLock.lock(); | ||
try { | ||
if (isRunning()) { | ||
doStop(); | ||
} | ||
} | ||
finally { | ||
this.lifecycleLock.unlock(); | ||
} | ||
} | ||
|
||
@Override | ||
public boolean isRunning() { | ||
return this.running; | ||
} | ||
|
||
protected void setRunning(boolean running) { | ||
this.running = running; | ||
} | ||
|
||
@Override | ||
public ContainerProperties getContainerProperties() { | ||
return this.containerProperties; | ||
} | ||
|
||
@Override | ||
@Nullable | ||
public String getGroupId() { | ||
return this.containerProperties.getGroupId() == null | ||
? (String) this.shareConsumerFactory.getConfigurationProperties().get(ConsumerConfig.GROUP_ID_CONFIG) | ||
: this.containerProperties.getGroupId(); | ||
} | ||
|
||
@Override | ||
public String getListenerId() { | ||
return this.beanName; // the container factory sets the bean name to the id attribute | ||
} | ||
|
||
@Override | ||
public void setupMessageListener(Object messageListener) { | ||
this.containerProperties.setMessageListener(messageListener); | ||
} | ||
|
||
protected abstract void doStart(); | ||
|
||
protected abstract void doStop(); | ||
|
||
@Override | ||
public void destroy() { | ||
stop(); | ||
} | ||
} |
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.