Polish "Provide control over how a Cassandra Cluster is created"

See gh-16702
This commit is contained in:
Andy Wilkinson 2019-05-09 12:27:21 +01:00
parent 5daf310bf4
commit ece87cc4cc
4 changed files with 43 additions and 49 deletions

View File

@ -48,17 +48,11 @@ import org.springframework.util.StringUtils;
@EnableConfigurationProperties(CassandraProperties.class) @EnableConfigurationProperties(CassandraProperties.class)
public class CassandraAutoConfiguration { public class CassandraAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public CassandraClusterFactory cassandraClusterFactory() {
return new CassandraClusterFactoryImpl();
}
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public Cluster cassandraCluster(CassandraProperties properties, public Cluster cassandraCluster(CassandraProperties properties,
ObjectProvider<ClusterBuilderCustomizer> builderCustomizers, ObjectProvider<ClusterBuilderCustomizer> builderCustomizers,
CassandraClusterFactory clusterFactory) { ObjectProvider<ClusterFactory> clusterFactory) {
PropertyMapper map = PropertyMapper.get(); PropertyMapper map = PropertyMapper.get();
Cluster.Builder builder = Cluster.builder() Cluster.Builder builder = Cluster.builder()
.withClusterName(properties.getClusterName()) .withClusterName(properties.getClusterName())
@ -79,8 +73,7 @@ public class CassandraAutoConfiguration {
.toCall(builder::withoutJMXReporting); .toCall(builder::withoutJMXReporting);
builderCustomizers.orderedStream() builderCustomizers.orderedStream()
.forEach((customizer) -> customizer.customize(builder)); .forEach((customizer) -> customizer.customize(builder));
return clusterFactory.getIfAvailable(() -> Cluster::buildFrom).create(builder);
return clusterFactory.build(builder);
} }
private QueryOptions getQueryOptions(CassandraProperties properties) { private QueryOptions getQueryOptions(CassandraProperties properties) {

View File

@ -1,35 +0,0 @@
/*
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.boot.autoconfigure.cassandra;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Cluster.Initializer;
/**
* Default Cassandra Cluster Factory Implementation.
*
* @auther Steffen F. Qvistgaard
* @since 2.2.0
*/
public class CassandraClusterFactoryImpl implements CassandraClusterFactory {
@Override
public Cluster build(final Initializer initializer) {
return Cluster.buildFrom(initializer);
}
}

View File

@ -20,15 +20,20 @@ import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Cluster.Initializer; import com.datastax.driver.core.Cluster.Initializer;
/** /**
* Cassandra Cluster Factory Interface. * {@code CassandraClusterFactory} provides control over the creation of a {@Cluster} from
* * an {@link Initializer}.
* This interface allows the default cassandra cluster builder to be overwritten
* *
* @auther Steffen F. Qvistgaard * @auther Steffen F. Qvistgaard
* @since 2.2.0 * @since 2.2.0
*/ */
public interface CassandraClusterFactory { @FunctionalInterface
public interface ClusterFactory {
Cluster build(Initializer initializer); /**
* Creates a {@link Cluster} from the given {@link Initializer}.
* @param initializer the {@Code Initializer}
* @return the {@code Cluster}
*/
Cluster create(Initializer initializer);
} }

View File

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.cassandra; package org.springframework.boot.autoconfigure.cassandra;
import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Cluster.Initializer;
import com.datastax.driver.core.PoolingOptions; import com.datastax.driver.core.PoolingOptions;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -115,6 +116,14 @@ public class CassandraAutoConfigurationTests {
}); });
} }
@Test
public void clusterFactoryIsCalledToCreateCluster() {
this.contextRunner.withUserConfiguration(ClusterFactoryConfig.class)
.run((context) -> assertThat(
context.getBean(TestClusterFactory.class).initializer)
.isNotNull());
}
@Configuration(proxyBeanMethods = false) @Configuration(proxyBeanMethods = false)
static class MockCustomizerConfig { static class MockCustomizerConfig {
@ -135,4 +144,26 @@ public class CassandraAutoConfigurationTests {
} }
@Configuration(proxyBeanMethods = false)
static class ClusterFactoryConfig {
@Bean
public TestClusterFactory clusterFactory() {
return new TestClusterFactory();
}
}
static class TestClusterFactory implements ClusterFactory {
private Initializer initializer = null;
@Override
public Cluster create(Initializer initializer) {
this.initializer = initializer;
return Cluster.buildFrom(initializer);
}
}
} }