Skip to content

Commit ece87cc

Browse files
committed
Polish "Provide control over how a Cassandra Cluster is created"
See gh-16702
1 parent 5daf310 commit ece87cc

File tree

4 files changed

+43
-49
lines changed

4 files changed

+43
-49
lines changed

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

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,11 @@
4848
@EnableConfigurationProperties(CassandraProperties.class)
4949
public class CassandraAutoConfiguration {
5050

51-
@Bean
52-
@ConditionalOnMissingBean
53-
public CassandraClusterFactory cassandraClusterFactory() {
54-
return new CassandraClusterFactoryImpl();
55-
}
56-
5751
@Bean
5852
@ConditionalOnMissingBean
5953
public Cluster cassandraCluster(CassandraProperties properties,
6054
ObjectProvider<ClusterBuilderCustomizer> builderCustomizers,
61-
CassandraClusterFactory clusterFactory) {
55+
ObjectProvider<ClusterFactory> clusterFactory) {
6256
PropertyMapper map = PropertyMapper.get();
6357
Cluster.Builder builder = Cluster.builder()
6458
.withClusterName(properties.getClusterName())
@@ -79,8 +73,7 @@ public Cluster cassandraCluster(CassandraProperties properties,
7973
.toCall(builder::withoutJMXReporting);
8074
builderCustomizers.orderedStream()
8175
.forEach((customizer) -> customizer.customize(builder));
82-
83-
return clusterFactory.build(builder);
76+
return clusterFactory.getIfAvailable(() -> Cluster::buildFrom).create(builder);
8477
}
8578

8679
private QueryOptions getQueryOptions(CassandraProperties properties) {

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

Lines changed: 0 additions & 35 deletions
This file was deleted.
Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,15 +20,20 @@
2020
import com.datastax.driver.core.Cluster.Initializer;
2121

2222
/**
23-
* Cassandra Cluster Factory Interface.
24-
*
25-
* This interface allows the default cassandra cluster builder to be overwritten
23+
* {@code CassandraClusterFactory} provides control over the creation of a {@Cluster} from
24+
* an {@link Initializer}.
2625
*
2726
* @auther Steffen F. Qvistgaard
2827
* @since 2.2.0
2928
*/
30-
public interface CassandraClusterFactory {
29+
@FunctionalInterface
30+
public interface ClusterFactory {
3131

32-
Cluster build(Initializer initializer);
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);
3338

3439
}

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)