From 994576af9eec8abbf6b9905364d0eb3ad3c877a6 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Thu, 1 Jul 2021 11:14:15 -0400 Subject: [PATCH] Remove @EnableAsync and @EnableScheduling to avoid conflicts. (#817) * Remove @EnableAsync and @EnableScheduling to avoid conflicts. Fixes #780 --- spring-cloud-kubernetes-config/pom.xml | 6 ++-- .../reload/ConfigReloadAutoConfiguration.java | 24 +++++++++++---- .../PollingConfigurationChangeDetector.java | 30 +++++++++++++++++-- 3 files changed, 49 insertions(+), 11 deletions(-) diff --git a/spring-cloud-kubernetes-config/pom.xml b/spring-cloud-kubernetes-config/pom.xml index a3e27d03..89ae9c6b 100644 --- a/spring-cloud-kubernetes-config/pom.xml +++ b/spring-cloud-kubernetes-config/pom.xml @@ -51,12 +51,14 @@ org.springframework.boot spring-boot-autoconfigure - org.springframework.cloud spring-cloud-context - + + org.springframework.cloud + spring-cloud-commons + org.springframework.security spring-security-rsa diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java index 0dae6bc7..6e3be241 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/ConfigReloadAutoConfiguration.java @@ -32,6 +32,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.cloud.autoconfigure.RefreshAutoConfiguration; import org.springframework.cloud.autoconfigure.RefreshEndpointAutoConfiguration; +import org.springframework.cloud.commons.util.TaskSchedulerWrapper; import org.springframework.cloud.context.refresh.ContextRefresher; import org.springframework.cloud.context.restart.RestartEndpoint; import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator; @@ -41,8 +42,7 @@ import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Conditional; import org.springframework.context.annotation.Configuration; import org.springframework.core.env.AbstractEnvironment; -import org.springframework.scheduling.annotation.EnableAsync; -import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler; import org.springframework.util.Assert; /** @@ -65,8 +65,6 @@ public class ConfigReloadAutoConfiguration { */ @ConditionalOnProperty("spring.cloud.kubernetes.reload.enabled") @ConditionalOnClass({ RestartEndpoint.class, ContextRefresher.class }) - @EnableScheduling - @EnableAsync protected static class ConfigReloadAutoConfigurationBeans { @Autowired @@ -87,12 +85,14 @@ public class ConfigReloadAutoConfiguration { @Autowired( required = false) ConfigMapPropertySourceLocator configMapPropertySourceLocator, @Autowired( - required = false) SecretsPropertySourceLocator secretsPropertySourceLocator) { + required = false) SecretsPropertySourceLocator secretsPropertySourceLocator, + TaskSchedulerWrapper taskScheduler) { switch (properties.getMode()) { case POLLING: return new PollingConfigurationChangeDetector(this.environment, properties, this.kubernetesClient, strategy, - configMapPropertySourceLocator, secretsPropertySourceLocator); + configMapPropertySourceLocator, secretsPropertySourceLocator, + taskScheduler.getTaskScheduler(), properties); case EVENT: return new EventBasedConfigurationChangeDetector(this.environment, properties, this.kubernetesClient, strategy, @@ -102,6 +102,18 @@ public class ConfigReloadAutoConfiguration { "Unsupported configuration reload mode: " + properties.getMode()); } + @Bean("springCloudKubernetesTaskScheduler") + @ConditionalOnMissingBean + public TaskSchedulerWrapper taskScheduler() { + ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); + + threadPoolTaskScheduler.setThreadNamePrefix( + "spring-cloud-kubernetes-ThreadPoolTaskScheduler-"); + threadPoolTaskScheduler.setDaemon(true); + + return new TaskSchedulerWrapper(threadPoolTaskScheduler); + } + /** * @param properties config reload properties * @param ctx application context diff --git a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java index 5cef72a5..4ab18b8f 100644 --- a/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java +++ b/spring-cloud-kubernetes-config/src/main/java/org/springframework/cloud/kubernetes/config/reload/PollingConfigurationChangeDetector.java @@ -16,6 +16,7 @@ package org.springframework.cloud.kubernetes.config.reload; +import java.time.Duration; import java.util.List; import javax.annotation.PostConstruct; @@ -24,13 +25,15 @@ import io.fabric8.kubernetes.client.KubernetesClient; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import org.springframework.boot.task.TaskSchedulerBuilder; import org.springframework.cloud.kubernetes.config.ConfigMapPropertySource; import org.springframework.cloud.kubernetes.config.ConfigMapPropertySourceLocator; import org.springframework.cloud.kubernetes.config.SecretsPropertySource; import org.springframework.cloud.kubernetes.config.SecretsPropertySourceLocator; import org.springframework.core.env.AbstractEnvironment; import org.springframework.core.env.MapPropertySource; -import org.springframework.scheduling.annotation.Scheduled; +import org.springframework.scheduling.TaskScheduler; +import org.springframework.scheduling.support.PeriodicTrigger; /** * A change detector that periodically retrieves secrets and configmaps and fire a reload @@ -47,6 +50,11 @@ public class PollingConfigurationChangeDetector extends ConfigurationChangeDetec private SecretsPropertySourceLocator secretsPropertySourceLocator; + private TaskScheduler taskExecutor; + + private Duration period = Duration.ofMillis(1500); + + @Deprecated public PollingConfigurationChangeDetector(AbstractEnvironment environment, ConfigReloadProperties properties, KubernetesClient kubernetesClient, ConfigurationUpdateStrategy strategy, @@ -56,15 +64,31 @@ public class PollingConfigurationChangeDetector extends ConfigurationChangeDetec this.configMapPropertySourceLocator = configMapPropertySourceLocator; this.secretsPropertySourceLocator = secretsPropertySourceLocator; + this.taskExecutor = new TaskSchedulerBuilder().build(); + } + + public PollingConfigurationChangeDetector(AbstractEnvironment environment, + ConfigReloadProperties properties, KubernetesClient kubernetesClient, + ConfigurationUpdateStrategy strategy, + ConfigMapPropertySourceLocator configMapPropertySourceLocator, + SecretsPropertySourceLocator secretsPropertySourceLocator, + TaskScheduler taskExecutor, ConfigReloadProperties configReloadProperties) { + super(environment, properties, kubernetesClient, strategy); + + this.configMapPropertySourceLocator = configMapPropertySourceLocator; + this.secretsPropertySourceLocator = secretsPropertySourceLocator; + this.taskExecutor = taskExecutor; + this.period = configReloadProperties.getPeriod(); } @PostConstruct public void init() { this.log.info("Kubernetes polling configuration change detector activated"); + PeriodicTrigger trigger = new PeriodicTrigger(period.toMillis()); + trigger.setInitialDelay(period.toMillis()); + taskExecutor.schedule(this::executeCycle, trigger); } - @Scheduled(initialDelayString = "${spring.cloud.kubernetes.reload.period:15000}", - fixedDelayString = "${spring.cloud.kubernetes.reload.period:15000}") public void executeCycle() { boolean changedConfigMap = false;