|
| 1 | +/* |
| 2 | + * Copyright 2025-2025 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.kafka.listener; |
| 18 | + |
| 19 | +import java.util.concurrent.locks.ReentrantLock; |
| 20 | +import java.util.regex.Pattern; |
| 21 | + |
| 22 | +import org.apache.commons.logging.LogFactory; |
| 23 | +import org.apache.kafka.clients.consumer.ConsumerConfig; |
| 24 | +import org.jspecify.annotations.Nullable; |
| 25 | + |
| 26 | +import org.springframework.beans.BeanUtils; |
| 27 | +import org.springframework.beans.BeansException; |
| 28 | +import org.springframework.beans.factory.BeanNameAware; |
| 29 | +import org.springframework.context.ApplicationContext; |
| 30 | +import org.springframework.context.ApplicationContextAware; |
| 31 | +import org.springframework.context.ApplicationEventPublisher; |
| 32 | +import org.springframework.context.ApplicationEventPublisherAware; |
| 33 | +import org.springframework.core.log.LogAccessor; |
| 34 | +import org.springframework.kafka.core.ShareConsumerFactory; |
| 35 | +import org.springframework.kafka.support.TopicPartitionOffset; |
| 36 | +import org.springframework.util.Assert; |
| 37 | + |
| 38 | +/** |
| 39 | + * Abstract base class for share consumer message listener containers. |
| 40 | + * <p> |
| 41 | + * Handles common lifecycle, configuration, and event publishing for containers using a |
| 42 | + * {@link org.springframework.kafka.core.ShareConsumerFactory}. |
| 43 | + * <p> |
| 44 | + * Subclasses are responsible for implementing the actual consumer loop and message dispatch logic. |
| 45 | + * |
| 46 | + * @param <K> the key type |
| 47 | + * @param <V> the value type |
| 48 | + * |
| 49 | + * @author Soby Chacko |
| 50 | + * @since 4.0 |
| 51 | + */ |
| 52 | +public abstract class AbstractShareKafkaMessageListenerContainer<K, V> |
| 53 | + implements GenericMessageListenerContainer<K, V>, BeanNameAware, ApplicationEventPublisherAware, |
| 54 | + ApplicationContextAware { |
| 55 | + |
| 56 | + /** |
| 57 | + * The default {@link org.springframework.context.SmartLifecycle} phase for listener containers. |
| 58 | + */ |
| 59 | + public static final int DEFAULT_PHASE = Integer.MAX_VALUE - 100; |
| 60 | + |
| 61 | + protected final ShareConsumerFactory<K, V> shareConsumerFactory; |
| 62 | + |
| 63 | + protected final LogAccessor logger = new LogAccessor(LogFactory.getLog(this.getClass())); |
| 64 | + |
| 65 | + private final ContainerProperties containerProperties; |
| 66 | + |
| 67 | + protected final ReentrantLock lifecycleLock = new ReentrantLock(); |
| 68 | + |
| 69 | + private String beanName = "noBeanNameSet"; |
| 70 | + |
| 71 | + @Nullable |
| 72 | + private ApplicationEventPublisher applicationEventPublisher; |
| 73 | + |
| 74 | + private boolean autoStartup = true; |
| 75 | + |
| 76 | + private int phase = DEFAULT_PHASE; |
| 77 | + |
| 78 | + @Nullable |
| 79 | + private ApplicationContext applicationContext; |
| 80 | + |
| 81 | + private volatile boolean running = false; |
| 82 | + |
| 83 | + /** |
| 84 | + * Construct an instance with the provided factory and properties. |
| 85 | + * @param shareConsumerFactory the factory. |
| 86 | + * @param containerProperties the properties. |
| 87 | + */ |
| 88 | + @SuppressWarnings("unchecked") |
| 89 | + protected AbstractShareKafkaMessageListenerContainer(@Nullable ShareConsumerFactory<? super K, ? super V> shareConsumerFactory, |
| 90 | + ContainerProperties containerProperties) { |
| 91 | + Assert.notNull(containerProperties, "'containerProperties' cannot be null"); |
| 92 | + Assert.notNull(shareConsumerFactory, "'shareConsumerFactory' cannot be null"); |
| 93 | + this.shareConsumerFactory = (ShareConsumerFactory<K, V>) shareConsumerFactory; |
| 94 | + @Nullable String @Nullable [] topics = containerProperties.getTopics(); |
| 95 | + if (topics != null) { |
| 96 | + this.containerProperties = new ContainerProperties(topics); |
| 97 | + } |
| 98 | + else { |
| 99 | + Pattern topicPattern = containerProperties.getTopicPattern(); |
| 100 | + if (topicPattern != null) { |
| 101 | + this.containerProperties = new ContainerProperties(topicPattern); |
| 102 | + } |
| 103 | + else { |
| 104 | + @Nullable TopicPartitionOffset @Nullable [] topicPartitions = containerProperties.getTopicPartitions(); |
| 105 | + if (topicPartitions != null) { |
| 106 | + this.containerProperties = new ContainerProperties(topicPartitions); |
| 107 | + } |
| 108 | + else { |
| 109 | + throw new IllegalStateException("topics, topicPattern, or topicPartitions must be provided"); |
| 110 | + } |
| 111 | + } |
| 112 | + } |
| 113 | + BeanUtils.copyProperties(containerProperties, this.containerProperties, |
| 114 | + "topics", "topicPartitions", "topicPattern"); |
| 115 | + } |
| 116 | + |
| 117 | + @Override |
| 118 | + public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { |
| 119 | + this.applicationContext = applicationContext; |
| 120 | + } |
| 121 | + |
| 122 | + @Nullable |
| 123 | + public ApplicationContext getApplicationContext() { |
| 124 | + return this.applicationContext; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public void setBeanName(String name) { |
| 129 | + this.beanName = name; |
| 130 | + } |
| 131 | + |
| 132 | + /** |
| 133 | + * Return the bean name. |
| 134 | + * @return the bean name |
| 135 | + */ |
| 136 | + public String getBeanName() { |
| 137 | + return this.beanName; |
| 138 | + } |
| 139 | + |
| 140 | + @Override |
| 141 | + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { |
| 142 | + this.applicationEventPublisher = applicationEventPublisher; |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * Get the event publisher. |
| 147 | + * @return the publisher |
| 148 | + */ |
| 149 | + @Nullable |
| 150 | + public ApplicationEventPublisher getApplicationEventPublisher() { |
| 151 | + return this.applicationEventPublisher; |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean isAutoStartup() { |
| 156 | + return this.autoStartup; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public void setAutoStartup(boolean autoStartup) { |
| 161 | + this.autoStartup = autoStartup; |
| 162 | + } |
| 163 | + |
| 164 | + @Override |
| 165 | + public int getPhase() { |
| 166 | + return this.phase; |
| 167 | + } |
| 168 | + |
| 169 | + public void setPhase(int phase) { |
| 170 | + this.phase = phase; |
| 171 | + } |
| 172 | + |
| 173 | + @Override |
| 174 | + public void stop(Runnable callback) { |
| 175 | + stop(); |
| 176 | + callback.run(); |
| 177 | + } |
| 178 | + |
| 179 | + @Override |
| 180 | + public void start() { |
| 181 | + this.lifecycleLock.lock(); |
| 182 | + try { |
| 183 | + if (!isRunning()) { |
| 184 | + Assert.state(this.containerProperties.getMessageListener() instanceof GenericMessageListener, |
| 185 | + () -> "A " + GenericMessageListener.class.getName() + " implementation must be provided"); |
| 186 | + doStart(); |
| 187 | + } |
| 188 | + } |
| 189 | + finally { |
| 190 | + this.lifecycleLock.unlock(); |
| 191 | + } |
| 192 | + } |
| 193 | + |
| 194 | + @Override |
| 195 | + public void stop() { |
| 196 | + this.lifecycleLock.lock(); |
| 197 | + try { |
| 198 | + if (isRunning()) { |
| 199 | + doStop(); |
| 200 | + } |
| 201 | + } |
| 202 | + finally { |
| 203 | + this.lifecycleLock.unlock(); |
| 204 | + } |
| 205 | + } |
| 206 | + |
| 207 | + @Override |
| 208 | + public boolean isRunning() { |
| 209 | + return this.running; |
| 210 | + } |
| 211 | + |
| 212 | + protected void setRunning(boolean running) { |
| 213 | + this.running = running; |
| 214 | + } |
| 215 | + |
| 216 | + @Override |
| 217 | + public ContainerProperties getContainerProperties() { |
| 218 | + return this.containerProperties; |
| 219 | + } |
| 220 | + |
| 221 | + @Override |
| 222 | + @Nullable |
| 223 | + public String getGroupId() { |
| 224 | + return this.containerProperties.getGroupId() == null |
| 225 | + ? (String) this.shareConsumerFactory.getConfigurationProperties().get(ConsumerConfig.GROUP_ID_CONFIG) |
| 226 | + : this.containerProperties.getGroupId(); |
| 227 | + } |
| 228 | + |
| 229 | + @Override |
| 230 | + public String getListenerId() { |
| 231 | + return this.beanName; // the container factory sets the bean name to the id attribute |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public void setupMessageListener(Object messageListener) { |
| 236 | + this.containerProperties.setMessageListener(messageListener); |
| 237 | + } |
| 238 | + |
| 239 | + protected abstract void doStart(); |
| 240 | + |
| 241 | + protected abstract void doStop(); |
| 242 | + |
| 243 | + @Override |
| 244 | + public void destroy() { |
| 245 | + stop(); |
| 246 | + } |
| 247 | +} |
0 commit comments