Skip to content
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

Spring Cloud Azure 4.0 auto configuration apply builder factory customizer #25532

Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,12 @@
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty;
import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties;
import com.azure.spring.core.AzureSpringIdentifier;
import com.azure.spring.core.connectionstring.ConnectionStringProvider;
import com.azure.spring.core.connectionstring.StaticConnectionStringProvider;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.core.service.AzureServiceType;
import com.azure.spring.service.appconfiguration.ConfigurationClientBuilderFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -51,13 +56,28 @@ public ConfigurationAsyncClient azureConfigurationAsyncClient(ConfigurationClien
@Bean
@ConditionalOnMissingBean
public ConfigurationClientBuilder configurationClientBuilder(ConfigurationClientBuilderFactory factory) {
factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_APP_CONFIG);
return factory.build();
}

@Bean
@ConditionalOnMissingBean
public ConfigurationClientBuilderFactory configurationClientBuilderFactory(AzureAppConfigurationProperties properties) {
return new ConfigurationClientBuilderFactory(properties);
public ConfigurationClientBuilderFactory configurationClientBuilderFactory(
AzureAppConfigurationProperties properties,
ObjectProvider<ConnectionStringProvider<AzureServiceType.AppConfiguration>> connectionStringProviders,
ObjectProvider<AzureServiceClientBuilderCustomizer<ConfigurationClientBuilder>> customizers) {
ConfigurationClientBuilderFactory factory = new ConfigurationClientBuilderFactory(properties);

factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_APP_CONFIG);
connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider);
customizers.orderedStream().forEach(factory::addBuilderCustomizer);
return factory;
}

@Bean
@ConditionalOnProperty("spring.cloud.azure.appconfiguration.connection-string")
public StaticConnectionStringProvider<AzureServiceType.AppConfiguration> staticAppConfigurationConnectionStringProvider(
AzureAppConfigurationProperties properties) {

return new StaticConnectionStringProvider<>(AzureServiceType.APP_CONFIGURATION, properties.getConnectionString());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.azure.core.credential.TokenCredential;
import com.azure.identity.DefaultAzureCredentialBuilder;
import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.core.factory.AbstractAzureServiceClientBuilderFactory;
import com.azure.spring.core.factory.credential.AbstractAzureCredentialBuilderFactory;
import com.azure.spring.core.factory.credential.DefaultAzureCredentialBuilderFactory;
Expand All @@ -20,8 +21,6 @@
import org.springframework.core.annotation.Order;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;

import static com.azure.spring.cloud.autoconfigure.context.AzureContextUtils.DEFAULT_TOKEN_CREDENTIAL_BEAN_NAME;

/**
Expand All @@ -35,6 +34,7 @@ public class AzureDefaultTokenCredentialAutoConfiguration {
public AzureDefaultTokenCredentialAutoConfiguration(AzureGlobalProperties azureGlobalProperties) {
this.azureGlobalProperties = azureGlobalProperties;
}

@ConditionalOnMissingBean(name = DEFAULT_TOKEN_CREDENTIAL_BEAN_NAME)
@Bean(name = DEFAULT_TOKEN_CREDENTIAL_BEAN_NAME)
@Order
Expand All @@ -45,12 +45,14 @@ public TokenCredential tokenCredential(AbstractAzureCredentialBuilderFactory<Def
@Bean
@ConditionalOnMissingBean
public AbstractAzureCredentialBuilderFactory<DefaultAzureCredentialBuilder> azureCredentialBuilderFactory(
ObjectProvider<AzureServiceClientBuilderCustomizer<DefaultAzureCredentialBuilder>> customizers,
ObjectProvider<ThreadPoolTaskExecutor> threadPoolTaskExecutors) {
ThreadPoolExecutor threadPoolExecutor = null;
if (threadPoolTaskExecutors.getIfAvailable() != null) {
threadPoolExecutor = threadPoolTaskExecutors.getObject().getThreadPoolExecutor();
}
return new DefaultAzureCredentialBuilderFactory(azureGlobalProperties, threadPoolExecutor);
DefaultAzureCredentialBuilderFactory factory = new DefaultAzureCredentialBuilderFactory(azureGlobalProperties);

threadPoolTaskExecutors.ifAvailable(tpe -> factory.setExecutorService(tpe.getThreadPoolExecutor()));
customizers.orderedStream().forEach(factory::addBuilderCustomizer);

return factory;
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty;
import com.azure.spring.cloud.autoconfigure.cosmos.properties.AzureCosmosProperties;
import com.azure.spring.cloud.autoconfigure.properties.AzureGlobalProperties;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.service.cosmos.CosmosClientBuilderFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
Expand Down Expand Up @@ -56,8 +58,12 @@ public CosmosClientBuilder cosmosClientBuilder(CosmosClientBuilderFactory factor

@Bean
@ConditionalOnMissingBean
public CosmosClientBuilderFactory cosmosClientBuilderFactory(AzureCosmosProperties properties) {
return new CosmosClientBuilderFactory(properties);
public CosmosClientBuilderFactory cosmosClientBuilderFactory(AzureCosmosProperties properties,
ObjectProvider<AzureServiceClientBuilderCustomizer<CosmosClientBuilder>> customizers) {
CosmosClientBuilderFactory factory = new CosmosClientBuilderFactory(properties);

customizers.orderedStream().forEach(factory::addBuilderCustomizer);
return factory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@
import com.azure.messaging.eventhubs.EventHubClientBuilder;
import com.azure.messaging.eventhubs.checkpointstore.blob.BlobCheckpointStore;
import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties;
import com.azure.spring.core.AzureSpringIdentifier;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.service.storage.blob.BlobServiceClientBuilderFactory;
import com.azure.storage.blob.BlobContainerAsyncClient;
import com.azure.storage.blob.BlobServiceClientBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
Expand Down Expand Up @@ -46,7 +49,7 @@ public BlobCheckpointStore blobCheckpointStore(
.buildAsyncClient()
.getBlobContainerAsyncClient(eventHubsProperties.getProcessor().getCheckpointStore().getContainerName());

initializers.ifAvailable(initializer -> initializer.init(blobContainerAsyncClient));
initializers.orderedStream().forEach(i -> i.init(blobContainerAsyncClient));

return new BlobCheckpointStore(blobContainerAsyncClient);
}
Expand All @@ -66,8 +69,14 @@ public BlobCheckpointStoreContainerInitializer blobCheckpointStoreContainerIniti
@Bean(EVENT_HUB_PROCESSOR_CHECKPOINT_STORE_STORAGE_CLIENT_BUILDER_FACTORY_BEAN_NAME)
@ConditionalOnMissingBean(name = EVENT_HUB_PROCESSOR_CHECKPOINT_STORE_STORAGE_CLIENT_BUILDER_FACTORY_BEAN_NAME)
public BlobServiceClientBuilderFactory eventHubProcessorBlobServiceClientBuilderFactory(
AzureEventHubsProperties eventHubsProperties) {
return new BlobServiceClientBuilderFactory(getCheckpointStoreProperties(eventHubsProperties));
AzureEventHubsProperties eventHubsProperties,
ObjectProvider<AzureServiceClientBuilderCustomizer<BlobServiceClientBuilder>> customizers) {
BlobServiceClientBuilderFactory factory =
new BlobServiceClientBuilderFactory(getCheckpointStoreProperties(eventHubsProperties));

factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
customizers.orderedStream().forEach(factory::addBuilderCustomizer);
return factory;
}

private AzureEventHubsProperties.Processor.BlobCheckpointStore getCheckpointStoreProperties(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties;
import com.azure.spring.core.AzureSpringIdentifier;
import com.azure.spring.core.connectionstring.ConnectionStringProvider;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.core.service.AzureServiceType;
import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory;
import org.springframework.beans.factory.ObjectProvider;
Expand All @@ -34,12 +35,14 @@ public EventHubClientBuilder eventHubClientBuilder(EventHubClientBuilderFactory
@Bean
@ConditionalOnMissingBean
public EventHubClientBuilderFactory eventHubClientBuilderFactory(AzureEventHubsProperties properties,
ObjectProvider<ConnectionStringProvider<AzureServiceType.EventHubs>> connectionStringProviders) {
final EventHubClientBuilderFactory builderFactory = new EventHubClientBuilderFactory(properties);
ObjectProvider<ConnectionStringProvider<AzureServiceType.EventHubs>> connectionStringProviders,
ObjectProvider<AzureServiceClientBuilderCustomizer<EventHubClientBuilder>> customizers) {
final EventHubClientBuilderFactory factory = new EventHubClientBuilderFactory(properties);

builderFactory.setConnectionStringProvider(connectionStringProviders.getIfAvailable());
builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
return builderFactory;
factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider);
customizers.orderedStream().forEach(factory::addBuilderCustomizer);
return factory;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnMissingProperty;
import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties;
import com.azure.spring.core.AzureSpringIdentifier;
import com.azure.spring.core.connectionstring.StaticConnectionStringProvider;
import com.azure.spring.core.connectionstring.ConnectionStringProvider;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.core.properties.util.PropertyMapper;
import com.azure.spring.core.service.AzureServiceType;
import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -79,14 +81,16 @@ static class DedicatedConsumerConnectionConfiguration {

@Bean(EVENT_HUB_CONSUMER_CLIENT_BUILDER_FACTORY_BEAN_NAME)
@ConditionalOnMissingBean(name = EVENT_HUB_CONSUMER_CLIENT_BUILDER_FACTORY_BEAN_NAME)
public EventHubClientBuilderFactory eventHubClientBuilderFactoryForConsumer() {
public EventHubClientBuilderFactory eventHubClientBuilderFactoryForConsumer(
ObjectProvider<ConnectionStringProvider<AzureServiceType.EventHubs>> connectionStringProviders,
ObjectProvider<AzureServiceClientBuilderCustomizer<EventHubClientBuilder>> customizers) {

final EventHubClientBuilderFactory builderFactory = new EventHubClientBuilderFactory(this.consumerProperties);
final EventHubClientBuilderFactory factory = new EventHubClientBuilderFactory(this.consumerProperties);

builderFactory.setConnectionStringProvider(new StaticConnectionStringProvider<>(AzureServiceType.EVENT_HUBS,
this.consumerProperties.getConnectionString()));
builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
return builderFactory;
factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider);
customizers.orderedStream().forEach(factory::addBuilderCustomizer);
return factory;
}

@Bean(EVENT_HUB_CONSUMER_CLIENT_BUILDER_BEAN_NAME)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
import com.azure.messaging.eventhubs.EventProcessorClientBuilder;
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnAnyProperty;
import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties;
import com.azure.spring.cloud.resourcemanager.connectionstring.AbstractArmConnectionStringProvider;
import com.azure.spring.core.connectionstring.StaticConnectionStringProvider;
import com.azure.spring.core.AzureSpringIdentifier;
import com.azure.spring.core.connectionstring.ConnectionStringProvider;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.core.service.AzureServiceType;
import com.azure.spring.service.eventhubs.factory.EventProcessorClientBuilderFactory;
import com.azure.spring.service.eventhubs.processor.EventProcessingListener;
Expand All @@ -22,9 +23,6 @@
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;

import static com.azure.spring.core.service.AzureServiceType.EVENT_HUBS;

/**
* Configures a {@link EventProcessorClient}.
Expand Down Expand Up @@ -52,16 +50,14 @@ public EventProcessorClient eventProcessorClient(EventProcessorClientBuilder bui
@ConditionalOnMissingBean
public EventProcessorClientBuilderFactory eventProcessorClientBuilderFactory(
CheckpointStore checkpointStore, EventProcessingListener listener,
ObjectProvider<AbstractArmConnectionStringProvider<AzureServiceType.EventHubs>> connectionStringProviders) {
ObjectProvider<ConnectionStringProvider<AzureServiceType.EventHubs>> connectionStringProviders,
ObjectProvider<AzureServiceClientBuilderCustomizer<EventProcessorClientBuilder>> customizers) {
final EventProcessorClientBuilderFactory factory =
new EventProcessorClientBuilderFactory(this.processorProperties, checkpointStore, listener);

if (StringUtils.hasText(this.processorProperties.getConnectionString())) {
factory.setConnectionStringProvider(
new StaticConnectionStringProvider<>(EVENT_HUBS, this.processorProperties.getConnectionString()));
} else {
factory.setConnectionStringProvider(connectionStringProviders.getIfAvailable());
}
factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider);
customizers.orderedStream().forEach(factory::addBuilderCustomizer);
return factory;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,11 @@
import com.azure.spring.cloud.autoconfigure.condition.ConditionalOnMissingProperty;
import com.azure.spring.cloud.autoconfigure.eventhubs.properties.AzureEventHubsProperties;
import com.azure.spring.core.AzureSpringIdentifier;
import com.azure.spring.core.connectionstring.StaticConnectionStringProvider;
import com.azure.spring.core.connectionstring.ConnectionStringProvider;
import com.azure.spring.core.customizer.AzureServiceClientBuilderCustomizer;
import com.azure.spring.core.service.AzureServiceType;
import com.azure.spring.service.eventhubs.factory.EventHubClientBuilderFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
Expand Down Expand Up @@ -61,14 +63,17 @@ static class DedicatedProducerConnectionConfiguration {

@Bean(EVENT_HUB_PRODUCER_CLIENT_BUILDER_FACTORY_BEAN_NAME)
@ConditionalOnMissingBean(name = EVENT_HUB_PRODUCER_CLIENT_BUILDER_FACTORY_BEAN_NAME)
public EventHubClientBuilderFactory eventHubClientBuilderFactoryForProducer() {
public EventHubClientBuilderFactory eventHubClientBuilderFactoryForProducer(
ObjectProvider<ConnectionStringProvider<AzureServiceType.EventHubs>> connectionStringProviders,
ObjectProvider<AzureServiceClientBuilderCustomizer<EventHubClientBuilder>> customizers) {

final EventHubClientBuilderFactory builderFactory = new EventHubClientBuilderFactory(this.producerProperties);
final EventHubClientBuilderFactory factory = new EventHubClientBuilderFactory(this.producerProperties);

builderFactory.setConnectionStringProvider(new StaticConnectionStringProvider<>(AzureServiceType.EVENT_HUBS,
this.producerProperties.getConnectionString()));
builderFactory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
return builderFactory;
factory.setSpringIdentifier(AzureSpringIdentifier.AZURE_SPRING_EVENT_HUBS);
connectionStringProviders.ifAvailable(factory::setConnectionStringProvider);
connectionStringProviders.orderedStream().findFirst().ifPresent(factory::setConnectionStringProvider);
customizers.orderedStream().forEach(factory::addBuilderCustomizer);
return factory;
}

@Bean(EVENT_HUB_PRODUCER_CLIENT_BUILDER_BEAN_NAME)
Expand Down
Loading