Merge pull request #16702 from Steffen Folman Qvistgaard

* gh-16702:
  Polish "Provide control over how a Cassandra Cluster is created"
  Provide control over how a Cassandra Cluster is created

Closes gh-16702
pull/16815/head
Andy Wilkinson 6 years ago
commit 40e1c19bee

@ -40,6 +40,7 @@ import org.springframework.util.StringUtils;
* @author Phillip Webb
* @author Eddú Meléndez
* @author Stephane Nicoll
* @auther Steffen F. Qvistgaard
* @since 1.3.0
*/
@Configuration(proxyBeanMethods = false)
@ -50,7 +51,8 @@ public class CassandraAutoConfiguration {
@Bean
@ConditionalOnMissingBean
public Cluster cassandraCluster(CassandraProperties properties,
ObjectProvider<ClusterBuilderCustomizer> builderCustomizers) {
ObjectProvider<ClusterBuilderCustomizer> builderCustomizers,
ObjectProvider<ClusterFactory> clusterFactory) {
PropertyMapper map = PropertyMapper.get();
Cluster.Builder builder = Cluster.builder()
.withClusterName(properties.getClusterName())
@ -71,7 +73,7 @@ public class CassandraAutoConfiguration {
.toCall(builder::withoutJMXReporting);
builderCustomizers.orderedStream()
.forEach((customizer) -> customizer.customize(builder));
return builder.build();
return clusterFactory.getIfAvailable(() -> Cluster::buildFrom).create(builder);
}
private QueryOptions getQueryOptions(CassandraProperties properties) {

@ -0,0 +1,39 @@
/*
* 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;
/**
* {@code CassandraClusterFactory} provides control over the creation of a {@Cluster} from
* an {@link Initializer}.
*
* @auther Steffen F. Qvistgaard
* @since 2.2.0
*/
@FunctionalInterface
public interface ClusterFactory {
/**
* Creates a {@link Cluster} from the given {@link Initializer}.
* @param initializer the {@Code Initializer}
* @return the {@code Cluster}
*/
Cluster create(Initializer initializer);
}

@ -17,6 +17,7 @@
package org.springframework.boot.autoconfigure.cassandra;
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Cluster.Initializer;
import com.datastax.driver.core.PoolingOptions;
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)
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);
}
}
}

Loading…
Cancel
Save