Provide control over how a Cassandra Cluster is created

See gh-16702
This commit is contained in:
Steffen Folman Qvistgaard 2019-05-02 09:08:50 +02:00 committed by Andy Wilkinson
parent 81af0f2fa6
commit 5daf310bf4
3 changed files with 80 additions and 2 deletions

View File

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

View File

@ -0,0 +1,34 @@
/*
* 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;
/**
* Cassandra Cluster Factory Interface.
*
* This interface allows the default cassandra cluster builder to be overwritten
*
* @auther Steffen F. Qvistgaard
* @since 2.2.0
*/
public interface CassandraClusterFactory {
Cluster build(Initializer initializer);
}

View File

@ -0,0 +1,35 @@
/*
* 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);
}
}