|
| 1 | +/* |
| 2 | + * Copyright 2025-present 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.config; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.Collection; |
| 21 | +import java.util.regex.Pattern; |
| 22 | + |
| 23 | +import org.apache.commons.logging.LogFactory; |
| 24 | +import org.jspecify.annotations.Nullable; |
| 25 | + |
| 26 | +import org.springframework.context.ApplicationContext; |
| 27 | +import org.springframework.context.ApplicationContextAware; |
| 28 | +import org.springframework.context.ApplicationEventPublisher; |
| 29 | +import org.springframework.context.ApplicationEventPublisherAware; |
| 30 | +import org.springframework.core.log.LogAccessor; |
| 31 | +import org.springframework.kafka.core.ShareConsumerFactory; |
| 32 | +import org.springframework.kafka.listener.AbstractShareKafkaMessageListenerContainer; |
| 33 | +import org.springframework.kafka.listener.ContainerProperties; |
| 34 | +import org.springframework.kafka.support.JavaUtils; |
| 35 | +import org.springframework.kafka.support.TopicPartitionOffset; |
| 36 | + |
| 37 | +/** |
| 38 | + * Base {@link KafkaListenerContainerFactory} for creating containers that use Kafka's share consumer model. |
| 39 | + * <p> |
| 40 | + * This abstract factory provides common configuration and lifecycle management for share consumer containers. |
| 41 | + * It handles the creation of containers based on endpoints, topics, or patterns, and applies common |
| 42 | + * configuration properties to the created containers. |
| 43 | + * <p> |
| 44 | + * The share consumer model enables cooperative rebalancing, allowing consumers to maintain ownership of |
| 45 | + * some partitions while relinquishing others during rebalances, which can reduce disruption compared to |
| 46 | + * the classic consumer model. |
| 47 | + * |
| 48 | + * @param <C> the container type |
| 49 | + * @param <K> the key type |
| 50 | + * @param <V> the value type |
| 51 | + * |
| 52 | + * @author Soby Chacko |
| 53 | + * @since 4.0 |
| 54 | + */ |
| 55 | +public abstract class AbstractShareKafkaListenerContainerFactory<C extends AbstractShareKafkaMessageListenerContainer<K, V>, K, V> |
| 56 | + implements KafkaListenerContainerFactory<C>, ApplicationEventPublisherAware, ApplicationContextAware { |
| 57 | + |
| 58 | + protected final LogAccessor logger = new LogAccessor(LogFactory.getLog(getClass())); |
| 59 | + |
| 60 | + private final ContainerProperties containerProperties = new ContainerProperties((Pattern) null); |
| 61 | + |
| 62 | + private @Nullable ShareConsumerFactory<? super K, ? super V> shareConsumerFactory; |
| 63 | + |
| 64 | + private @Nullable Boolean autoStartup; |
| 65 | + |
| 66 | + private @Nullable Integer phase; |
| 67 | + |
| 68 | + private @Nullable ApplicationEventPublisher applicationEventPublisher; |
| 69 | + |
| 70 | + private @Nullable ApplicationContext applicationContext; |
| 71 | + |
| 72 | + @Override |
| 73 | + public void setApplicationContext(ApplicationContext applicationContext) { |
| 74 | + this.applicationContext = applicationContext; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Set the share consumer factory to use for creating containers. |
| 79 | + * @param shareConsumerFactory the share consumer factory |
| 80 | + */ |
| 81 | + public void setShareConsumerFactory(ShareConsumerFactory<? super K, ? super V> shareConsumerFactory) { |
| 82 | + this.shareConsumerFactory = shareConsumerFactory; |
| 83 | + } |
| 84 | + |
| 85 | + /** |
| 86 | + * Get the share consumer factory. |
| 87 | + * @return the share consumer factory |
| 88 | + */ |
| 89 | + public @Nullable ShareConsumerFactory<? super K, ? super V> getShareConsumerFactory() { |
| 90 | + return this.shareConsumerFactory; |
| 91 | + } |
| 92 | + |
| 93 | + /** |
| 94 | + * Set whether containers created by this factory should auto-start. |
| 95 | + * @param autoStartup true to auto-start |
| 96 | + */ |
| 97 | + public void setAutoStartup(Boolean autoStartup) { |
| 98 | + this.autoStartup = autoStartup; |
| 99 | + } |
| 100 | + |
| 101 | + /** |
| 102 | + * Set the phase in which containers created by this factory should start and stop. |
| 103 | + * @param phase the phase |
| 104 | + */ |
| 105 | + public void setPhase(Integer phase) { |
| 106 | + this.phase = phase; |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { |
| 111 | + this.applicationEventPublisher = applicationEventPublisher; |
| 112 | + } |
| 113 | + |
| 114 | + /** |
| 115 | + * Get the container properties. |
| 116 | + * @return the container properties |
| 117 | + */ |
| 118 | + public ContainerProperties getContainerProperties() { |
| 119 | + return this.containerProperties; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public C createListenerContainer(KafkaListenerEndpoint endpoint) { |
| 124 | + C instance = createContainerInstance(endpoint); |
| 125 | + JavaUtils.INSTANCE |
| 126 | + .acceptIfNotNull(endpoint.getId(), instance::setBeanName); |
| 127 | + if (endpoint instanceof AbstractKafkaListenerEndpoint) { |
| 128 | + configureEndpoint((AbstractKafkaListenerEndpoint<K, V>) endpoint); |
| 129 | + } |
| 130 | + endpoint.setupListenerContainer(instance, null); // No message converter for MVP |
| 131 | + initializeContainer(instance, endpoint); |
| 132 | + return instance; |
| 133 | + } |
| 134 | + |
| 135 | + private void configureEndpoint(AbstractKafkaListenerEndpoint<K, V> endpoint) { |
| 136 | + // Minimal configuration; can add more properties later |
| 137 | + } |
| 138 | + |
| 139 | + /** |
| 140 | + * Initialize the provided container with common configuration properties. |
| 141 | + * @param instance the container instance |
| 142 | + * @param endpoint the endpoint |
| 143 | + */ |
| 144 | + protected void initializeContainer(C instance, KafkaListenerEndpoint endpoint) { |
| 145 | + ContainerProperties properties = instance.getContainerProperties(); |
| 146 | + if (this.containerProperties.getAckCount() > 0) { |
| 147 | + properties.setAckCount(this.containerProperties.getAckCount()); |
| 148 | + } |
| 149 | + if (this.containerProperties.getAckTime() > 0) { |
| 150 | + properties.setAckTime(this.containerProperties.getAckTime()); |
| 151 | + } |
| 152 | + if (endpoint.getAutoStartup() != null) { |
| 153 | + instance.setAutoStartup(endpoint.getAutoStartup()); |
| 154 | + } |
| 155 | + else if (this.autoStartup != null) { |
| 156 | + instance.setAutoStartup(this.autoStartup); |
| 157 | + } |
| 158 | + if (this.phase != null) { |
| 159 | + instance.setPhase(this.phase); |
| 160 | + } |
| 161 | + if (this.applicationContext != null) { |
| 162 | + instance.setApplicationContext(this.applicationContext); |
| 163 | + } |
| 164 | + if (this.applicationEventPublisher != null) { |
| 165 | + instance.setApplicationEventPublisher(this.applicationEventPublisher); |
| 166 | + } |
| 167 | + if (endpoint.getGroupId() != null) { |
| 168 | + instance.getContainerProperties().setGroupId(endpoint.getGroupId()); |
| 169 | + } |
| 170 | + if (endpoint.getClientIdPrefix() != null) { |
| 171 | + instance.getContainerProperties().setClientId(endpoint.getClientIdPrefix()); |
| 172 | + } |
| 173 | + if (endpoint.getConsumerProperties() != null) { |
| 174 | + instance.getContainerProperties().setKafkaConsumerProperties(endpoint.getConsumerProperties()); |
| 175 | + } |
| 176 | + } |
| 177 | + |
| 178 | + @Override |
| 179 | + public C createContainer(TopicPartitionOffset... topicPartitions) { |
| 180 | + return createContainerInstance(new KafkaListenerEndpointAdapter() { |
| 181 | + @Override |
| 182 | + public TopicPartitionOffset[] getTopicPartitionsToAssign() { |
| 183 | + return Arrays.copyOf(topicPartitions, topicPartitions.length); |
| 184 | + } |
| 185 | + }); |
| 186 | + } |
| 187 | + |
| 188 | + @Override |
| 189 | + public C createContainer(String... topics) { |
| 190 | + return createContainerInstance(new KafkaListenerEndpointAdapter() { |
| 191 | + @Override |
| 192 | + public Collection<String> getTopics() { |
| 193 | + return Arrays.asList(topics); |
| 194 | + } |
| 195 | + }); |
| 196 | + } |
| 197 | + |
| 198 | + @Override |
| 199 | + public C createContainer(Pattern topicPattern) { |
| 200 | + return createContainerInstance(new KafkaListenerEndpointAdapter() { |
| 201 | + @Override |
| 202 | + public Pattern getTopicPattern() { |
| 203 | + return topicPattern; |
| 204 | + } |
| 205 | + }); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Create a container instance for the provided endpoint. |
| 210 | + * @param endpoint the endpoint |
| 211 | + * @return the container instance |
| 212 | + */ |
| 213 | + protected abstract C createContainerInstance(KafkaListenerEndpoint endpoint); |
| 214 | +} |
0 commit comments