org.zodiac.autoconfigure.zookeeper.ZookeeperAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.zookeeper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.zodiac.autoconfigure.zookeeper.condition.ConditionalOnZookeeperEnabled;
@Configuration
@ConditionalOnZookeeperEnabled
@ConditionalOnClass(value = {org.apache.curator.framework.CuratorFramework.class})
public class ZookeeperAutoConfiguration {
private static final Logger log = LoggerFactory.getLogger(ZookeeperAutoConfiguration.class);
@Bean
@ConditionalOnMissingBean
@ConfigurationProperties(prefix = org.zodiac.zookeeper.constants.ZookeeperSystemPropertiesConstants.SPRING_ZOOKEEPER_PREFIX, ignoreInvalidFields = true)
protected ZookeeperProperties zookeeperProperties() {
return new ZookeeperProperties();
}
@Bean
@ConditionalOnMissingBean
protected org.apache.curator.RetryPolicy exponentialBackoffRetry(ZookeeperProperties properties) {
return new org.apache.curator.retry.ExponentialBackoffRetry(properties.getBaseSleepTimeMs(), properties.getMaxRetries(),
properties.getMaxSleepMs());
}
@Bean(destroyMethod = "close")
@ConditionalOnMissingBean
protected org.apache.curator.framework.CuratorFramework curatorFramework(org.apache.curator.RetryPolicy retryPolicy, ZookeeperProperties properties,
ObjectProvider ensembleProviderProvider) throws Exception {
org.apache.curator.framework.CuratorFrameworkFactory.Builder builder = org.apache.curator.framework.CuratorFrameworkFactory.builder();
org.apache.curator.ensemble.EnsembleProvider ensembleProvider = ensembleProviderProvider.getIfAvailable();
if (null != ensembleProvider) {
builder.ensembleProvider(ensembleProvider);
} else {
builder.connectString(properties.getConnectString());
}
org.apache.curator.framework.CuratorFramework curator = builder.retryPolicy(retryPolicy).build();
curator.start();
log.trace("Blocking until connected to zookeeper for {}{} .", properties.getBlockUntilConnectedWait(),
properties.getBlockUntilConnectedUnit());
curator.blockUntilConnected(properties.getBlockUntilConnectedWait(), properties.getBlockUntilConnectedUnit());
log.trace("connected to zookeeper");
return curator;
}
@Bean
@ConditionalOnMissingBean(value = {org.zodiac.zookeeper.support.ZookeeperCuratorOperations.class})
protected org.zodiac.zookeeper.support.ZookeeperCuratorTemplate zookeeperCuratorTemplate(org.apache.curator.framework.CuratorFramework curatorFramework,
ZookeeperProperties zookeeperProperties) {
return new org.zodiac.zookeeper.support.ZookeeperCuratorTemplate(curatorFramework, zookeeperProperties.getCharset());
}
}