|  | 
|  | 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.jspecify.annotations.Nullable; | 
|  | 24 | + | 
|  | 25 | +import org.springframework.context.ApplicationContext; | 
|  | 26 | +import org.springframework.context.ApplicationContextAware; | 
|  | 27 | +import org.springframework.context.ApplicationEventPublisher; | 
|  | 28 | +import org.springframework.context.ApplicationEventPublisherAware; | 
|  | 29 | +import org.springframework.kafka.core.ShareConsumerFactory; | 
|  | 30 | +import org.springframework.kafka.listener.ContainerProperties; | 
|  | 31 | +import org.springframework.kafka.listener.ShareKafkaMessageListenerContainer; | 
|  | 32 | +import org.springframework.kafka.support.JavaUtils; | 
|  | 33 | +import org.springframework.kafka.support.TopicPartitionOffset; | 
|  | 34 | +import org.springframework.util.Assert; | 
|  | 35 | + | 
|  | 36 | +/** | 
|  | 37 | + * A {@link KafkaListenerContainerFactory} implementation to create {@link ShareKafkaMessageListenerContainer} | 
|  | 38 | + * instances for Kafka's share consumer model. | 
|  | 39 | + * <p> | 
|  | 40 | + * This 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 <K> the key type | 
|  | 49 | + * @param <V> the value type | 
|  | 50 | + * | 
|  | 51 | + * @author Soby Chacko | 
|  | 52 | + * @since 4.0 | 
|  | 53 | + */ | 
|  | 54 | +public class ShareKafkaListenerContainerFactory<K, V> | 
|  | 55 | +		implements KafkaListenerContainerFactory<ShareKafkaMessageListenerContainer<K, V>>, ApplicationEventPublisherAware, ApplicationContextAware { | 
|  | 56 | + | 
|  | 57 | +	private final ShareConsumerFactory<? super K, ? super V> shareConsumerFactory; | 
|  | 58 | + | 
|  | 59 | +	private @Nullable Boolean autoStartup; | 
|  | 60 | + | 
|  | 61 | +	private @Nullable Integer phase; | 
|  | 62 | + | 
|  | 63 | +	private @Nullable ApplicationEventPublisher applicationEventPublisher; | 
|  | 64 | + | 
|  | 65 | +	private @Nullable ApplicationContext applicationContext; | 
|  | 66 | + | 
|  | 67 | +	/** | 
|  | 68 | +	 * Construct an instance with the provided consumer factory. | 
|  | 69 | +	 * @param shareConsumerFactory the share consumer factory | 
|  | 70 | +	 */ | 
|  | 71 | +	public ShareKafkaListenerContainerFactory(ShareConsumerFactory<K, V> shareConsumerFactory) { | 
|  | 72 | +		this.shareConsumerFactory = shareConsumerFactory; | 
|  | 73 | +	} | 
|  | 74 | + | 
|  | 75 | +	@Override | 
|  | 76 | +	public void setApplicationContext(ApplicationContext applicationContext) { | 
|  | 77 | +		this.applicationContext = applicationContext; | 
|  | 78 | +	} | 
|  | 79 | + | 
|  | 80 | +	/** | 
|  | 81 | +	 * Set whether containers created by this factory should auto-start. | 
|  | 82 | +	 * @param autoStartup true to auto-start | 
|  | 83 | +	 */ | 
|  | 84 | +	public void setAutoStartup(Boolean autoStartup) { | 
|  | 85 | +		this.autoStartup = autoStartup; | 
|  | 86 | +	} | 
|  | 87 | + | 
|  | 88 | +	/** | 
|  | 89 | +	 * Set the phase in which containers created by this factory should start and stop. | 
|  | 90 | +	 * @param phase the phase | 
|  | 91 | +	 */ | 
|  | 92 | +	public void setPhase(Integer phase) { | 
|  | 93 | +		this.phase = phase; | 
|  | 94 | +	} | 
|  | 95 | + | 
|  | 96 | +	@Override | 
|  | 97 | +	public void setApplicationEventPublisher(ApplicationEventPublisher applicationEventPublisher) { | 
|  | 98 | +		this.applicationEventPublisher = applicationEventPublisher; | 
|  | 99 | +	} | 
|  | 100 | + | 
|  | 101 | +	@Override | 
|  | 102 | +	@SuppressWarnings({"unchecked", "rawtypes"}) | 
|  | 103 | +	public ShareKafkaMessageListenerContainer<K, V> createListenerContainer(KafkaListenerEndpoint endpoint) { | 
|  | 104 | +		ShareKafkaMessageListenerContainer<K, V> instance = createContainerInstance(endpoint); | 
|  | 105 | +		JavaUtils.INSTANCE | 
|  | 106 | +				.acceptIfNotNull(endpoint.getId(), instance::setBeanName); | 
|  | 107 | +		if (endpoint instanceof AbstractKafkaListenerEndpoint abstractKafkaListenerEndpoint) { | 
|  | 108 | +			configureEndpoint(abstractKafkaListenerEndpoint); | 
|  | 109 | +		} | 
|  | 110 | +		// TODO: No message converter for queue at the moment | 
|  | 111 | +		endpoint.setupListenerContainer(instance, null); | 
|  | 112 | +		initializeContainer(instance, endpoint); | 
|  | 113 | +		return instance; | 
|  | 114 | +	} | 
|  | 115 | + | 
|  | 116 | +	private void configureEndpoint(AbstractKafkaListenerEndpoint<K, V> endpoint) { | 
|  | 117 | +		// Minimal configuration; can add more properties later | 
|  | 118 | +	} | 
|  | 119 | + | 
|  | 120 | +	/** | 
|  | 121 | +	 * Initialize the provided container with common configuration properties. | 
|  | 122 | +	 * @param instance the container instance | 
|  | 123 | +	 * @param endpoint the endpoint | 
|  | 124 | +	 */ | 
|  | 125 | +	protected void initializeContainer(ShareKafkaMessageListenerContainer<K, V> instance, KafkaListenerEndpoint endpoint) { | 
|  | 126 | +		ContainerProperties properties = instance.getContainerProperties(); | 
|  | 127 | +		Boolean effectiveAutoStartup = endpoint.getAutoStartup() != null ? endpoint.getAutoStartup() : this.autoStartup; | 
|  | 128 | +		JavaUtils.INSTANCE | 
|  | 129 | +				.acceptIfNotNull(effectiveAutoStartup, instance::setAutoStartup) | 
|  | 130 | +				.acceptIfNotNull(this.phase, instance::setPhase) | 
|  | 131 | +				.acceptIfNotNull(this.applicationContext, instance::setApplicationContext) | 
|  | 132 | +				.acceptIfNotNull(this.applicationEventPublisher, instance::setApplicationEventPublisher) | 
|  | 133 | +				.acceptIfNotNull(endpoint.getGroupId(), properties::setGroupId) | 
|  | 134 | +				.acceptIfNotNull(endpoint.getClientIdPrefix(), properties::setClientId) | 
|  | 135 | +				.acceptIfNotNull(endpoint.getConsumerProperties(), properties::setKafkaConsumerProperties); | 
|  | 136 | +	} | 
|  | 137 | + | 
|  | 138 | +	@Override | 
|  | 139 | +	public ShareKafkaMessageListenerContainer<K, V> createContainer(TopicPartitionOffset... topicPartitions) { | 
|  | 140 | +		throw new UnsupportedOperationException("ShareConsumer does not support explicit partition assignment"); | 
|  | 141 | +	} | 
|  | 142 | + | 
|  | 143 | +	@Override | 
|  | 144 | +	public ShareKafkaMessageListenerContainer<K, V> createContainer(String... topics) { | 
|  | 145 | +		return createContainerInstance(new KafkaListenerEndpointAdapter() { | 
|  | 146 | + | 
|  | 147 | +			@Override | 
|  | 148 | +			public Collection<String> getTopics() { | 
|  | 149 | +				return Arrays.asList(topics); | 
|  | 150 | +			} | 
|  | 151 | +		}); | 
|  | 152 | +	} | 
|  | 153 | + | 
|  | 154 | +	@Override | 
|  | 155 | +	public ShareKafkaMessageListenerContainer<K, V> createContainer(Pattern topicPattern) { | 
|  | 156 | +		throw new UnsupportedOperationException("ShareConsumer does not support topic patterns"); | 
|  | 157 | +	} | 
|  | 158 | + | 
|  | 159 | +	/** | 
|  | 160 | +	 * Create a container instance for the provided endpoint. | 
|  | 161 | +	 * @param endpoint the endpoint | 
|  | 162 | +	 * @return the container instance | 
|  | 163 | +	 */ | 
|  | 164 | +	protected ShareKafkaMessageListenerContainer<K, V> createContainerInstance(KafkaListenerEndpoint endpoint) { | 
|  | 165 | +		Collection<String> topics = endpoint.getTopics(); | 
|  | 166 | +		Assert.state(topics != null, "'topics' must not be null"); | 
|  | 167 | +		return new ShareKafkaMessageListenerContainer<>(this.shareConsumerFactory, | 
|  | 168 | +				new ContainerProperties(topics.toArray(new String[0]))); | 
|  | 169 | +	} | 
|  | 170 | + | 
|  | 171 | +} | 
0 commit comments