Skip to content

Commit 40e1c19

Browse files
committed
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
2 parents 81af0f2 + ece87cc commit 40e1c19

File tree

3 files changed

+74
-2
lines changed

3 files changed

+74
-2
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
* @author Phillip Webb
4141
* @author Eddú Meléndez
4242
* @author Stephane Nicoll
43+
* @auther Steffen F. Qvistgaard
4344
* @since 1.3.0
4445
*/
4546
@Configuration(proxyBeanMethods = false)
@@ -50,7 +51,8 @@ public class CassandraAutoConfiguration {
5051
@Bean
5152
@ConditionalOnMissingBean
5253
public Cluster cassandraCluster(CassandraProperties properties,
53-
ObjectProvider<ClusterBuilderCustomizer> builderCustomizers) {
54+
ObjectProvider<ClusterBuilderCustomizer> builderCustomizers,
55+
ObjectProvider<ClusterFactory> clusterFactory) {
5456
PropertyMapper map = PropertyMapper.get();
5557
Cluster.Builder builder = Cluster.builder()
5658
.withClusterName(properties.getClusterName())
@@ -71,7 +73,7 @@ public Cluster cassandraCluster(CassandraProperties properties,
7173
.toCall(builder::withoutJMXReporting);
7274
builderCustomizers.orderedStream()
7375
.forEach((customizer) -> customizer.customize(builder));
74-
return builder.build();
76+
return clusterFactory.getIfAvailable(() -> Cluster::buildFrom).create(builder);
7577
}
7678

7779
private QueryOptions getQueryOptions(CassandraProperties properties) {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.cassandra;
18+
19+
import com.datastax.driver.core.Cluster;
20+
import com.datastax.driver.core.Cluster.Initializer;
21+
22+
/**
23+
* {@code CassandraClusterFactory} provides control over the creation of a {@Cluster} from
24+
* an {@link Initializer}.
25+
*
26+
* @auther Steffen F. Qvistgaard
27+
* @since 2.2.0
28+
*/
29+
@FunctionalInterface
30+
public interface ClusterFactory {
31+
32+
/**
33+
* Creates a {@link Cluster} from the given {@link Initializer}.
34+
* @param initializer the {@Code Initializer}
35+
* @return the {@code Cluster}
36+
*/
37+
Cluster create(Initializer initializer);
38+
39+
}

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfigurationTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.boot.autoconfigure.cassandra;
1818

1919
import com.datastax.driver.core.Cluster;
20+
import com.datastax.driver.core.Cluster.Initializer;
2021
import com.datastax.driver.core.PoolingOptions;
2122
import org.junit.jupiter.api.Test;
2223

@@ -115,6 +116,14 @@ public void customizePoolOptions() {
115116
});
116117
}
117118

119+
@Test
120+
public void clusterFactoryIsCalledToCreateCluster() {
121+
this.contextRunner.withUserConfiguration(ClusterFactoryConfig.class)
122+
.run((context) -> assertThat(
123+
context.getBean(TestClusterFactory.class).initializer)
124+
.isNotNull());
125+
}
126+
118127
@Configuration(proxyBeanMethods = false)
119128
static class MockCustomizerConfig {
120129

@@ -135,4 +144,26 @@ public ClusterBuilderCustomizer customizer() {
135144

136145
}
137146

147+
@Configuration(proxyBeanMethods = false)
148+
static class ClusterFactoryConfig {
149+
150+
@Bean
151+
public TestClusterFactory clusterFactory() {
152+
return new TestClusterFactory();
153+
}
154+
155+
}
156+
157+
static class TestClusterFactory implements ClusterFactory {
158+
159+
private Initializer initializer = null;
160+
161+
@Override
162+
public Cluster create(Initializer initializer) {
163+
this.initializer = initializer;
164+
return Cluster.buildFrom(initializer);
165+
}
166+
167+
}
168+
138169
}

0 commit comments

Comments
 (0)