Merge pull request #7946 from tsachev:gh-7945
* pr/7946: Polish "Add PoolingOptions to CasandraProperties" Add PoolingOptions to CasandraProperties
This commit is contained in:
commit
dba6bcc7e1
|
@ -19,6 +19,7 @@ package org.springframework.boot.autoconfigure.cassandra;
|
|||
import java.util.List;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.PoolingOptions;
|
||||
import com.datastax.driver.core.QueryOptions;
|
||||
import com.datastax.driver.core.SocketOptions;
|
||||
import com.datastax.driver.core.policies.LoadBalancingPolicy;
|
||||
|
@ -89,6 +90,7 @@ public class CassandraAutoConfiguration {
|
|||
if (properties.isSsl()) {
|
||||
builder.withSSL();
|
||||
}
|
||||
builder.withPoolingOptions(getPoolingOptions());
|
||||
String points = properties.getContactPoints();
|
||||
builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
|
||||
|
||||
|
@ -128,4 +130,14 @@ public class CassandraAutoConfiguration {
|
|||
return options;
|
||||
}
|
||||
|
||||
private PoolingOptions getPoolingOptions() {
|
||||
CassandraProperties.Pool pool = this.properties.getPool();
|
||||
PoolingOptions options = new PoolingOptions();
|
||||
options.setIdleTimeoutSeconds(pool.getIdleTimeout());
|
||||
options.setPoolTimeoutMillis(pool.getPoolTimeout());
|
||||
options.setHeartbeatIntervalSeconds(pool.getHeartbeatInterval());
|
||||
options.setMaxQueueSize(pool.getMaxQueueSize());
|
||||
return options;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
/*
|
||||
* Copyright 2012-2016 the original author or authors.
|
||||
* Copyright 2012-2017 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.
|
||||
|
@ -33,6 +33,7 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
|
|||
* @author Julien Dubois
|
||||
* @author Phillip Webb
|
||||
* @author Mark Paluch
|
||||
* @author Stephane Nicoll
|
||||
* @since 1.3.0
|
||||
*/
|
||||
@ConfigurationProperties(prefix = "spring.data.cassandra")
|
||||
|
@ -123,6 +124,11 @@ public class CassandraProperties {
|
|||
*/
|
||||
private boolean ssl = false;
|
||||
|
||||
/**
|
||||
* Pool configuration.
|
||||
*/
|
||||
private final Pool pool = new Pool();
|
||||
|
||||
public String getKeyspaceName() {
|
||||
return this.keyspaceName;
|
||||
}
|
||||
|
@ -261,4 +267,69 @@ public class CassandraProperties {
|
|||
this.schemaAction = schemaAction;
|
||||
}
|
||||
|
||||
public Pool getPool() {
|
||||
return this.pool;
|
||||
}
|
||||
|
||||
/**
|
||||
* Pool properties.
|
||||
*/
|
||||
public static class Pool {
|
||||
|
||||
/**
|
||||
* Idle timeout (in seconds) before an idle connection is removed.
|
||||
*/
|
||||
private int idleTimeout = 120;
|
||||
|
||||
/**
|
||||
* Pool timeout (in milliseconds) when trying to acquire a connection from a
|
||||
* host's pool.
|
||||
*/
|
||||
private int poolTimeout = 5000;
|
||||
|
||||
/**
|
||||
* Heartbeat interval (in seconds) after which a message is sent on an idle
|
||||
* connection to make sure it's still alive.
|
||||
*/
|
||||
private int heartbeatInterval = 30;
|
||||
|
||||
/**
|
||||
* Maximum number of requests that get enqueued if no connection is available.
|
||||
*/
|
||||
private int maxQueueSize = 256;
|
||||
|
||||
public int getIdleTimeout() {
|
||||
return this.idleTimeout;
|
||||
}
|
||||
|
||||
public void setIdleTimeout(int idleTimeout) {
|
||||
this.idleTimeout = idleTimeout;
|
||||
}
|
||||
|
||||
public int getPoolTimeout() {
|
||||
return this.poolTimeout;
|
||||
}
|
||||
|
||||
public void setPoolTimeout(int poolTimeout) {
|
||||
this.poolTimeout = poolTimeout;
|
||||
}
|
||||
|
||||
public int getHeartbeatInterval() {
|
||||
return this.heartbeatInterval;
|
||||
}
|
||||
|
||||
public void setHeartbeatInterval(int heartbeatInterval) {
|
||||
this.heartbeatInterval = heartbeatInterval;
|
||||
}
|
||||
|
||||
public int getMaxQueueSize() {
|
||||
return this.maxQueueSize;
|
||||
}
|
||||
|
||||
public void setMaxQueueSize(int maxQueueSize) {
|
||||
this.maxQueueSize = maxQueueSize;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -17,6 +17,7 @@
|
|||
package org.springframework.boot.autoconfigure.cassandra;
|
||||
|
||||
import com.datastax.driver.core.Cluster;
|
||||
import com.datastax.driver.core.PoolingOptions;
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
|
||||
|
@ -80,6 +81,37 @@ public class CassandraAutoConfigurationTests {
|
|||
assertThat(cluster.getClusterName()).isEqualTo("overridden-name");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void defaultPoolOptions() {
|
||||
load();
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
|
||||
.getConfiguration().getPoolingOptions();
|
||||
assertThat(poolingOptions.getIdleTimeoutSeconds())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_IDLE_TIMEOUT_SECONDS);
|
||||
assertThat(poolingOptions.getPoolTimeoutMillis())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_POOL_TIMEOUT_MILLIS);
|
||||
assertThat(poolingOptions.getHeartbeatIntervalSeconds())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_HEARTBEAT_INTERVAL_SECONDS);
|
||||
assertThat(poolingOptions.getMaxQueueSize())
|
||||
.isEqualTo(PoolingOptions.DEFAULT_MAX_QUEUE_SIZE);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void customizePoolOptions() {
|
||||
load("spring.data.cassandra.pool.idle-timeout=42",
|
||||
"spring.data.cassandra.pool.pool-timeout=52",
|
||||
"spring.data.cassandra.pool.heartbeat-interval=62",
|
||||
"spring.data.cassandra.pool.max-queue-size=72");
|
||||
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
|
||||
PoolingOptions poolingOptions = this.context.getBean(Cluster.class)
|
||||
.getConfiguration().getPoolingOptions();
|
||||
assertThat(poolingOptions.getIdleTimeoutSeconds()).isEqualTo(42);
|
||||
assertThat(poolingOptions.getPoolTimeoutMillis()).isEqualTo(52);
|
||||
assertThat(poolingOptions.getHeartbeatIntervalSeconds()).isEqualTo(62);
|
||||
assertThat(poolingOptions.getMaxQueueSize()).isEqualTo(72);
|
||||
}
|
||||
|
||||
private void load(String... environment) {
|
||||
load(null, environment);
|
||||
}
|
||||
|
|
|
@ -588,10 +588,17 @@ content into your application; rather pick only the properties that you need.
|
|||
spring.data.cassandra.consistency-level= # Queries consistency level.
|
||||
spring.data.cassandra.contact-points=localhost # Comma-separated list of cluster node addresses.
|
||||
spring.data.cassandra.fetch-size= # Queries default fetch size.
|
||||
spring.data.cassandra.heartbeat-interval-seconds= # Pooling option: heartbeat interval.
|
||||
spring.data.cassandra.keyspace-name= # Keyspace name to use.
|
||||
spring.data.cassandra.load-balancing-policy= # Class name of the load balancing policy.
|
||||
spring.data.cassandra.max-requests-per-connection.*= # Pooling option: max requests per connection.
|
||||
spring.data.cassandra.max-queue-size= # Pooling option: max queue size.
|
||||
spring.data.cassandra.port= # Port of the Cassandra server.
|
||||
spring.data.cassandra.password= # Login password of the server.
|
||||
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.
|
||||
spring.data.cassandra.pool.idle-timeout=120 # Idle timeout (in seconds) before an idle connection is removed.
|
||||
spring.data.cassandra.pool.max-queue-size=256 # Maximum number of requests that get enqueued if no connection is available.
|
||||
spring.data.cassandra.pool.pool-timeout=5000 # Pool timeout (in milliseconds) when trying to acquire a connection from a host's pool.
|
||||
spring.data.cassandra.reactive-repositories.enabled=true # Enable Cassandra reactive repositories.
|
||||
spring.data.cassandra.read-timeout-millis= # Socket option: read time out.
|
||||
spring.data.cassandra.reconnection-policy= # Reconnection policy class.
|
||||
|
|
Loading…
Reference in New Issue