Skip to content

Commit dba6bcc

Browse files
committed
Merge pull request #7946 from tsachev:gh-7945
* pr/7946: Polish "Add PoolingOptions to CasandraProperties" Add PoolingOptions to CasandraProperties
2 parents 605dee4 + ba1bc45 commit dba6bcc

File tree

4 files changed

+123
-1
lines changed

4 files changed

+123
-1
lines changed

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
import java.util.List;
2020

2121
import com.datastax.driver.core.Cluster;
22+
import com.datastax.driver.core.PoolingOptions;
2223
import com.datastax.driver.core.QueryOptions;
2324
import com.datastax.driver.core.SocketOptions;
2425
import com.datastax.driver.core.policies.LoadBalancingPolicy;
@@ -89,6 +90,7 @@ public Cluster cassandraCluster() {
8990
if (properties.isSsl()) {
9091
builder.withSSL();
9192
}
93+
builder.withPoolingOptions(getPoolingOptions());
9294
String points = properties.getContactPoints();
9395
builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
9496

@@ -128,4 +130,14 @@ private SocketOptions getSocketOptions() {
128130
return options;
129131
}
130132

133+
private PoolingOptions getPoolingOptions() {
134+
CassandraProperties.Pool pool = this.properties.getPool();
135+
PoolingOptions options = new PoolingOptions();
136+
options.setIdleTimeoutSeconds(pool.getIdleTimeout());
137+
options.setPoolTimeoutMillis(pool.getPoolTimeout());
138+
options.setHeartbeatIntervalSeconds(pool.getHeartbeatInterval());
139+
options.setMaxQueueSize(pool.getMaxQueueSize());
140+
return options;
141+
}
142+
131143
}

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

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2016 the original author or authors.
2+
* Copyright 2012-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,6 +33,7 @@
3333
* @author Julien Dubois
3434
* @author Phillip Webb
3535
* @author Mark Paluch
36+
* @author Stephane Nicoll
3637
* @since 1.3.0
3738
*/
3839
@ConfigurationProperties(prefix = "spring.data.cassandra")
@@ -123,6 +124,11 @@ public class CassandraProperties {
123124
*/
124125
private boolean ssl = false;
125126

127+
/**
128+
* Pool configuration.
129+
*/
130+
private final Pool pool = new Pool();
131+
126132
public String getKeyspaceName() {
127133
return this.keyspaceName;
128134
}
@@ -261,4 +267,69 @@ public void setSchemaAction(String schemaAction) {
261267
this.schemaAction = schemaAction;
262268
}
263269

270+
public Pool getPool() {
271+
return this.pool;
272+
}
273+
274+
/**
275+
* Pool properties.
276+
*/
277+
public static class Pool {
278+
279+
/**
280+
* Idle timeout (in seconds) before an idle connection is removed.
281+
*/
282+
private int idleTimeout = 120;
283+
284+
/**
285+
* Pool timeout (in milliseconds) when trying to acquire a connection from a
286+
* host's pool.
287+
*/
288+
private int poolTimeout = 5000;
289+
290+
/**
291+
* Heartbeat interval (in seconds) after which a message is sent on an idle
292+
* connection to make sure it's still alive.
293+
*/
294+
private int heartbeatInterval = 30;
295+
296+
/**
297+
* Maximum number of requests that get enqueued if no connection is available.
298+
*/
299+
private int maxQueueSize = 256;
300+
301+
public int getIdleTimeout() {
302+
return this.idleTimeout;
303+
}
304+
305+
public void setIdleTimeout(int idleTimeout) {
306+
this.idleTimeout = idleTimeout;
307+
}
308+
309+
public int getPoolTimeout() {
310+
return this.poolTimeout;
311+
}
312+
313+
public void setPoolTimeout(int poolTimeout) {
314+
this.poolTimeout = poolTimeout;
315+
}
316+
317+
public int getHeartbeatInterval() {
318+
return this.heartbeatInterval;
319+
}
320+
321+
public void setHeartbeatInterval(int heartbeatInterval) {
322+
this.heartbeatInterval = heartbeatInterval;
323+
}
324+
325+
public int getMaxQueueSize() {
326+
return this.maxQueueSize;
327+
}
328+
329+
public void setMaxQueueSize(int maxQueueSize) {
330+
this.maxQueueSize = maxQueueSize;
331+
}
332+
333+
}
334+
264335
}

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

Lines changed: 32 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.PoolingOptions;
2021
import org.junit.After;
2122
import org.junit.Test;
2223

@@ -80,6 +81,37 @@ public void customizerOverridesAutoConfig() {
8081
assertThat(cluster.getClusterName()).isEqualTo("overridden-name");
8182
}
8283

84+
@Test
85+
public void defaultPoolOptions() {
86+
load();
87+
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
88+
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
89+
.getConfiguration().getPoolingOptions();
90+
assertThat(poolingOptions.getIdleTimeoutSeconds())
91+
.isEqualTo(PoolingOptions.DEFAULT_IDLE_TIMEOUT_SECONDS);
92+
assertThat(poolingOptions.getPoolTimeoutMillis())
93+
.isEqualTo(PoolingOptions.DEFAULT_POOL_TIMEOUT_MILLIS);
94+
assertThat(poolingOptions.getHeartbeatIntervalSeconds())
95+
.isEqualTo(PoolingOptions.DEFAULT_HEARTBEAT_INTERVAL_SECONDS);
96+
assertThat(poolingOptions.getMaxQueueSize())
97+
.isEqualTo(PoolingOptions.DEFAULT_MAX_QUEUE_SIZE);
98+
}
99+
100+
@Test
101+
public void customizePoolOptions() {
102+
load("spring.data.cassandra.pool.idle-timeout=42",
103+
"spring.data.cassandra.pool.pool-timeout=52",
104+
"spring.data.cassandra.pool.heartbeat-interval=62",
105+
"spring.data.cassandra.pool.max-queue-size=72");
106+
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
107+
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
108+
.getConfiguration().getPoolingOptions();
109+
assertThat(poolingOptions.getIdleTimeoutSeconds()).isEqualTo(42);
110+
assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(52);
111+
assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(62);
112+
assertThat(poolingOptions.getMaxQueueSize()).isEqualTo(72);
113+
}
114+
83115
private void load(String... environment) {
84116
load(null, environment);
85117
}

spring-boot-docs/src/main/asciidoc/appendix-application-properties.adoc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,10 +588,17 @@ content into your application; rather pick only the properties that you need.
588588
spring.data.cassandra.consistency-level= # Queries consistency level.
589589
spring.data.cassandra.contact-points=localhost # Comma-separated list of cluster node addresses.
590590
spring.data.cassandra.fetch-size= # Queries default fetch size.
591+
spring.data.cassandra.heartbeat-interval-seconds= # Pooling option: heartbeat interval.
591592
spring.data.cassandra.keyspace-name= # Keyspace name to use.
592593
spring.data.cassandra.load-balancing-policy= # Class name of the load balancing policy.
594+
spring.data.cassandra.max-requests-per-connection.*= # Pooling option: max requests per connection.
595+
spring.data.cassandra.max-queue-size= # Pooling option: max queue size.
593596
spring.data.cassandra.port= # Port of the Cassandra server.
594597
spring.data.cassandra.password= # Login password of the server.
598+
spring.data.cassandra.pool.heartbeat-interval=30 # Heartbeat interval (in seconds) after which a message is sent on an idle connection to make sure it's still alive.
599+
spring.data.cassandra.pool.idle-timeout=120 # Idle timeout (in seconds) before an idle connection is removed.
600+
spring.data.cassandra.pool.max-queue-size=256 # Maximum number of requests that get enqueued if no connection is available.
601+
spring.data.cassandra.pool.pool-timeout=5000 # Pool timeout (in milliseconds) when trying to acquire a connection from a host's pool.
595602
spring.data.cassandra.reactive-repositories.enabled=true # Enable Cassandra reactive repositories.
596603
spring.data.cassandra.read-timeout-millis= # Socket option: read time out.
597604
spring.data.cassandra.reconnection-policy= # Reconnection policy class.

0 commit comments

Comments
 (0)