|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +package com.azure.spring.messaging.servicebus.core; |
| 5 | + |
| 6 | +import com.azure.core.credential.TokenCredential; |
| 7 | +import com.azure.messaging.servicebus.ServiceBusClientBuilder; |
| 8 | +import com.azure.messaging.servicebus.ServiceBusSessionReceiverClient; |
| 9 | +import com.azure.messaging.servicebus.models.ServiceBusReceiveMode; |
| 10 | +import com.azure.spring.cloud.core.credential.AzureCredentialResolver; |
| 11 | +import com.azure.spring.cloud.core.customizer.AzureServiceClientBuilderCustomizer; |
| 12 | +import com.azure.spring.cloud.core.implementation.util.AzureSpringIdentifier; |
| 13 | +import com.azure.spring.cloud.service.implementation.servicebus.factory.ServiceBusSessionReceiverClientBuilderFactory; |
| 14 | +import com.azure.spring.cloud.service.servicebus.properties.ServiceBusEntityType; |
| 15 | +import com.azure.spring.messaging.ConsumerIdentifier; |
| 16 | +import com.azure.spring.messaging.PropertiesSupplier; |
| 17 | +import com.azure.spring.messaging.servicebus.core.properties.ConsumerProperties; |
| 18 | +import com.azure.spring.messaging.servicebus.core.properties.NamespaceProperties; |
| 19 | +import com.azure.spring.messaging.servicebus.implementation.properties.merger.ConsumerPropertiesParentMerger; |
| 20 | +import org.slf4j.Logger; |
| 21 | +import org.slf4j.LoggerFactory; |
| 22 | +import org.springframework.beans.factory.DisposableBean; |
| 23 | +import org.springframework.lang.Nullable; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.HashMap; |
| 27 | +import java.util.List; |
| 28 | +import java.util.Map; |
| 29 | +import java.util.concurrent.ConcurrentHashMap; |
| 30 | + |
| 31 | +/** |
| 32 | + * The {@link ServiceBusConsumerFactory} implementation to produce new {@link ServiceBusSessionReceiverClient} instances |
| 33 | + * for provided {@link NamespaceProperties} and optional producer {@link PropertiesSupplier} on each |
| 34 | + * {@link #createReceiver} invocation. |
| 35 | + * <p> |
| 36 | + * {@link ServiceBusSessionReceiverClient} produced by this factory will share the same namespace level configuration, but |
| 37 | + * if a configuration entry is provided at both producer and namespace level, the producer level configuration will |
| 38 | + * take advantage. |
| 39 | + * </p> |
| 40 | + * @since 5.22.0 |
| 41 | + */ |
| 42 | +public final class DefaultServiceBusNamespaceConsumerFactory implements ServiceBusConsumerFactory, DisposableBean { |
| 43 | + private static final Logger LOGGER = LoggerFactory.getLogger(DefaultServiceBusNamespaceConsumerFactory.class); |
| 44 | + private final List<Listener> listeners = new ArrayList<>(); |
| 45 | + private final NamespaceProperties namespaceProperties; |
| 46 | + private final PropertiesSupplier<ConsumerIdentifier, ConsumerProperties> propertiesSupplier; |
| 47 | + private final Map<String, ServiceBusSessionReceiverClient> clients = new ConcurrentHashMap<>(); |
| 48 | + private final ConsumerPropertiesParentMerger parentMerger = new ConsumerPropertiesParentMerger(); |
| 49 | + private final List<AzureServiceClientBuilderCustomizer<ServiceBusClientBuilder>> customizers = new ArrayList<>(); |
| 50 | + private final List<AzureServiceClientBuilderCustomizer<ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder>> sessionReceiverCustomizers = new ArrayList<>(); |
| 51 | + private final Map<String, List<AzureServiceClientBuilderCustomizer<ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder>>> dedicatedSessionReceiverCustomizers = new HashMap<>(); |
| 52 | + private AzureCredentialResolver<TokenCredential> tokenCredentialResolver = null; |
| 53 | + private TokenCredential defaultCredential = null; |
| 54 | + |
| 55 | + /** |
| 56 | + * Construct a factory with the provided namespace level configuration. |
| 57 | + * @param namespaceProperties the namespace properties |
| 58 | + */ |
| 59 | + public DefaultServiceBusNamespaceConsumerFactory(NamespaceProperties namespaceProperties) { |
| 60 | + this(namespaceProperties, key -> null); |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * Construct a factory with the provided namespace level configuration and producer {@link PropertiesSupplier}. |
| 65 | + * @param namespaceProperties the namespace properties. |
| 66 | + * @param supplier the {@link PropertiesSupplier} to supply {@link ConsumerProperties} for each queue/topic entity. |
| 67 | + */ |
| 68 | + public DefaultServiceBusNamespaceConsumerFactory(NamespaceProperties namespaceProperties, |
| 69 | + PropertiesSupplier<ConsumerIdentifier, ConsumerProperties> supplier) { |
| 70 | + this.namespaceProperties = namespaceProperties; |
| 71 | + this.propertiesSupplier = supplier == null ? key -> null : supplier; |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public ServiceBusSessionReceiverClient createReceiver(String name) { |
| 76 | + return doCreateReceiver(name, null); |
| 77 | + } |
| 78 | + |
| 79 | + @Override |
| 80 | + public ServiceBusSessionReceiverClient createReceiver(String name, ServiceBusEntityType entityType) { |
| 81 | + ConsumerProperties consumerProperties = this.propertiesSupplier.getProperties(new ConsumerIdentifier(name)) != null |
| 82 | + ? this.propertiesSupplier.getProperties(new ConsumerIdentifier(name)) : new ConsumerProperties(); |
| 83 | + if (entityType != null) { |
| 84 | + consumerProperties.setEntityType(entityType); |
| 85 | + } |
| 86 | + return doCreateReceiver(name, consumerProperties); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public void addListener(Listener listener) { |
| 91 | + this.listeners.add(listener); |
| 92 | + } |
| 93 | + |
| 94 | + @Override |
| 95 | + public boolean removeListener(Listener listener) { |
| 96 | + return this.listeners.remove(listener); |
| 97 | + } |
| 98 | + |
| 99 | + @Override |
| 100 | + public void destroy() { |
| 101 | + clients.forEach((name, receiver) -> { |
| 102 | + listeners.forEach(l -> l.receiverRemoved(name, receiver)); |
| 103 | + receiver.close(); |
| 104 | + }); |
| 105 | + this.clients.clear(); |
| 106 | + this.listeners.clear(); |
| 107 | + } |
| 108 | + |
| 109 | + private ServiceBusSessionReceiverClient doCreateReceiver(String name, @Nullable ConsumerProperties properties) { |
| 110 | + return clients.computeIfAbsent(name, entityName -> { |
| 111 | + ConsumerProperties consumerProperties = parentMerger.merge(properties, this.namespaceProperties); |
| 112 | + consumerProperties.setEntityName(entityName); |
| 113 | + |
| 114 | + ServiceBusSessionReceiverClient receiverClient; |
| 115 | + if (Boolean.TRUE.equals(consumerProperties.getSessionEnabled())) { |
| 116 | + ServiceBusSessionReceiverClientBuilderFactory factory = |
| 117 | + new ServiceBusSessionReceiverClientBuilderFactory(consumerProperties, this.customizers); |
| 118 | + |
| 119 | + factory.setDefaultTokenCredential(this.defaultCredential); |
| 120 | + factory.setTokenCredentialResolver(this.tokenCredentialResolver); |
| 121 | + factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_INTEGRATION_SERVICE_BUS); |
| 122 | + |
| 123 | + ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder builder = factory.build(); |
| 124 | + |
| 125 | + customizeBuilder(name, builder); |
| 126 | + |
| 127 | + builder.receiveMode(ServiceBusReceiveMode.RECEIVE_AND_DELETE); |
| 128 | + builder.disableAutoComplete(); |
| 129 | + LOGGER.debug("Set RECEIVE_AND_DELETE mode for request-reply-pattern receiver client, " |
| 130 | + + "'enableAutoComplete' is not needed in for RECEIVE_AND_DELETE mode."); |
| 131 | + |
| 132 | + receiverClient = builder.buildClient(); |
| 133 | + |
| 134 | + this.listeners.forEach(l -> l.receiverAdded(entityName, receiverClient)); |
| 135 | + } else { |
| 136 | + receiverClient = null; |
| 137 | + LOGGER.warn("Receiver client is null. Define a bean PropertiesSupplier<ConsumerIdentifier, ConsumerProperties> to enable consumer 'session-enabled'."); |
| 138 | + } |
| 139 | + return receiverClient; |
| 140 | + }); |
| 141 | + } |
| 142 | + |
| 143 | + /** |
| 144 | + * Set the token credential resolver. |
| 145 | + * |
| 146 | + * @param tokenCredentialResolver The token credential resolver. |
| 147 | + */ |
| 148 | + public void setTokenCredentialResolver(AzureCredentialResolver<TokenCredential> tokenCredentialResolver) { |
| 149 | + this.tokenCredentialResolver = tokenCredentialResolver; |
| 150 | + } |
| 151 | + |
| 152 | + /** |
| 153 | + * Set the default credential for all clients generated from this factory. |
| 154 | + * |
| 155 | + * @param defaultCredential The default credential. |
| 156 | + */ |
| 157 | + public void setDefaultCredential(TokenCredential defaultCredential) { |
| 158 | + this.defaultCredential = defaultCredential; |
| 159 | + } |
| 160 | + |
| 161 | + /** |
| 162 | + * Add a {@link ServiceBusClientBuilder} |
| 163 | + * customizer to customize the shared client builder created in this factory, it's used to build other sender clients. |
| 164 | + * |
| 165 | + * @param customizer the provided builder customizer. |
| 166 | + */ |
| 167 | + public void addCustomizer(AzureServiceClientBuilderCustomizer<ServiceBusClientBuilder> customizer) { |
| 168 | + if (customizer == null) { |
| 169 | + LOGGER.debug("The provided '{}' customizer is null, will ignore it.", ServiceBusClientBuilder.class.getName()); |
| 170 | + } else { |
| 171 | + this.customizers.add(customizer); |
| 172 | + } |
| 173 | + } |
| 174 | + |
| 175 | + /** |
| 176 | + * Add a {@link ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder} |
| 177 | + * customizer to customize all the session clients created from this factory. |
| 178 | + * @param customizer the provided builder customizer. |
| 179 | + */ |
| 180 | + public void addSessionReceiverCustomizer(AzureServiceClientBuilderCustomizer<ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder> customizer) { |
| 181 | + if (customizer == null) { |
| 182 | + LOGGER.debug("The provided '{}' customizer is null, will ignore it.", |
| 183 | + ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder.class.getName()); |
| 184 | + return; |
| 185 | + } |
| 186 | + this.sessionReceiverCustomizers.add(customizer); |
| 187 | + } |
| 188 | + |
| 189 | + /** |
| 190 | + * Add a session receiver client builder customizer to customize the clients created from this factory with Service Bus |
| 191 | + * entity name of value {@code entityName}. |
| 192 | + * |
| 193 | + * @param entityName the entity name of the client. |
| 194 | + * @param customizer the provided customizer. |
| 195 | + */ |
| 196 | + public void addDedicatedSessionReceiverCustomizer(String entityName, |
| 197 | + AzureServiceClientBuilderCustomizer<ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder> customizer) { |
| 198 | + if (customizer == null) { |
| 199 | + LOGGER.debug("The provided '{}' dedicated customizer is null, will ignore it.", |
| 200 | + ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder.class.getName()); |
| 201 | + } else { |
| 202 | + this.dedicatedSessionReceiverCustomizers |
| 203 | + .computeIfAbsent(entityName, key -> new ArrayList<>()) |
| 204 | + .add(customizer); |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + private void customizeBuilder(String entityName, ServiceBusClientBuilder.ServiceBusSessionReceiverClientBuilder builder) { |
| 209 | + this.sessionReceiverCustomizers.forEach(customizer -> customizer.customize(builder)); |
| 210 | + this.dedicatedSessionReceiverCustomizers.getOrDefault(entityName, new ArrayList<>()) |
| 211 | + .forEach(customizer -> customizer.customize(builder)); |
| 212 | + } |
| 213 | + |
| 214 | +} |
0 commit comments