org.zodiac.autoconfigure.bootstrap.config.client.AppConfigClientAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.bootstrap.config.client;
import javax.annotation.Resource;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.IntervalTask;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.zodiac.autoconfigure.application.ApplicationInfoProperties;
import org.zodiac.autoconfigure.bootstrap.condition.ConditionalOnAppConfigClientEnabled;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.commons.util.lang.Strings;
import org.zodiac.core.bootstrap.config.client.AppConfigClientWatch;
@SpringBootConfiguration
@ConditionalOnAppConfigClientEnabled
public class AppConfigClientAutoConfiguration {
public AppConfigClientAutoConfiguration() {
}
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_CONFIG_CLIENT_PREFIX, ignoreInvalidFields = true)
protected HttpAppConfigClientProperties httpConfigClientProperties(Environment environment, ApplicationContext context,
ApplicationInfoProperties applicationInfoProperties) {
if (context.getParent() != null && BeanFactoryUtils.beanNamesForTypeIncludingAncestors(context.getParent(),
HttpAppConfigClientProperties.class).length > 0) {
return BeanFactoryUtils.beanOfTypeIncludingAncestors(context.getParent(), HttpAppConfigClientProperties.class);
}
HttpAppConfigClientProperties httpConfigClientProperties = new HttpAppConfigClientProperties(environment);
String clientName = Strings.trimToNull(environment.getProperty(SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_CONFIG_CLIENT_NAME));
if (null == clientName)
httpConfigClientProperties.setName(applicationInfoProperties.getName());
return httpConfigClientProperties;
}
@EnableScheduling
@SpringBootConfiguration
@ConditionalOnAppConfigClientEnabled
@ConditionalOnClass(value = {org.zodiac.core.context.refresh.AppContextRefresher.class})
@ConditionalOnBean(value = {org.zodiac.core.context.refresh.AppContextRefresher.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_CONFIG_CLIENT_WATCH_ENABLED, havingValue = "true", matchIfMissing = true)
protected static class AppConfigClientWatchConfiguration implements SchedulingConfigurer {
private HttpAppConfigClientProperties configClientProperties;
@Resource
private AppConfigClientWatch configClientWatch;
public AppConfigClientWatchConfiguration(HttpAppConfigClientProperties configClientProperties) {
this.configClientProperties = configClientProperties;
}
@Bean
protected AppConfigClientWatch configClientWatch(org.zodiac.core.context.refresh.AppContextRefresher contextRefresher) {
return new AppConfigClientWatch(contextRefresher);
}
@Override
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) {
//AppConfigClientWatch configClientWatch = configClientWatch(contextRefresher);
AppConfigClientWatchTask task = new AppConfigClientWatchTask(() -> {
configClientWatch.watchRemoteServer();
}, configClientProperties.getWatch().getDelay(), configClientProperties.getWatch().getInitialDelay());
taskRegistrar.addFixedDelayTask(task);
}
}
private static class AppConfigClientWatchTask extends IntervalTask {
public AppConfigClientWatchTask(Runnable runnable, long interval, long initialDelay) {
super(runnable, interval, initialDelay);
}
public AppConfigClientWatchTask(Runnable runnable, long interval) {
super(runnable, interval);
}
}
}