001/*
002 * Copyright 2012-2018 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.boot.autoconfigure.cassandra;
018
019import java.time.Duration;
020import java.time.temporal.ChronoUnit;
021import java.util.ArrayList;
022import java.util.Collections;
023import java.util.List;
024
025import com.datastax.driver.core.ConsistencyLevel;
026import com.datastax.driver.core.ProtocolOptions;
027import com.datastax.driver.core.ProtocolOptions.Compression;
028import com.datastax.driver.core.QueryOptions;
029import com.datastax.driver.core.policies.LoadBalancingPolicy;
030import com.datastax.driver.core.policies.ReconnectionPolicy;
031import com.datastax.driver.core.policies.RetryPolicy;
032
033import org.springframework.boot.context.properties.ConfigurationProperties;
034import org.springframework.boot.context.properties.DeprecatedConfigurationProperty;
035import org.springframework.boot.convert.DurationUnit;
036
037/**
038 * Configuration properties for Cassandra.
039 *
040 * @author Julien Dubois
041 * @author Phillip Webb
042 * @author Mark Paluch
043 * @author Stephane Nicoll
044 * @since 1.3.0
045 */
046@ConfigurationProperties(prefix = "spring.data.cassandra")
047public class CassandraProperties {
048
049        /**
050         * Keyspace name to use.
051         */
052        private String keyspaceName;
053
054        /**
055         * Name of the Cassandra cluster.
056         */
057        private String clusterName;
058
059        /**
060         * Cluster node addresses.
061         */
062        private final List<String> contactPoints = new ArrayList<>(
063                        Collections.singleton("localhost"));
064
065        /**
066         * Port of the Cassandra server.
067         */
068        private int port = ProtocolOptions.DEFAULT_PORT;
069
070        /**
071         * Login user of the server.
072         */
073        private String username;
074
075        /**
076         * Login password of the server.
077         */
078        private String password;
079
080        /**
081         * Compression supported by the Cassandra binary protocol.
082         */
083        private Compression compression = Compression.NONE;
084
085        /**
086         * Class name of the load balancing policy. The class must have a default constructor.
087         */
088        private Class<? extends LoadBalancingPolicy> loadBalancingPolicy;
089
090        /**
091         * Queries consistency level.
092         */
093        private ConsistencyLevel consistencyLevel;
094
095        /**
096         * Queries serial consistency level.
097         */
098        private ConsistencyLevel serialConsistencyLevel;
099
100        /**
101         * Queries default fetch size.
102         */
103        private int fetchSize = QueryOptions.DEFAULT_FETCH_SIZE;
104
105        /**
106         * Class name of the reconnection policy. The class must have a default constructor.
107         */
108        private Class<? extends ReconnectionPolicy> reconnectionPolicy;
109
110        /**
111         * Class name of the retry policy. The class must have a default constructor.
112         */
113        private Class<? extends RetryPolicy> retryPolicy;
114
115        /**
116         * Socket option: connection time out.
117         */
118        private Duration connectTimeout;
119
120        /**
121         * Socket option: read time out.
122         */
123        private Duration readTimeout;
124
125        /**
126         * Schema action to take at startup.
127         */
128        private String schemaAction = "none";
129
130        /**
131         * Enable SSL support.
132         */
133        private boolean ssl = false;
134
135        /**
136         * Whether to enable JMX reporting. Default to false as Cassandra JMX reporting is not
137         * compatible with Dropwizard Metrics.
138         */
139        private boolean jmxEnabled;
140
141        /**
142         * Pool configuration.
143         */
144        private final Pool pool = new Pool();
145
146        public String getKeyspaceName() {
147                return this.keyspaceName;
148        }
149
150        public void setKeyspaceName(String keyspaceName) {
151                this.keyspaceName = keyspaceName;
152        }
153
154        public String getClusterName() {
155                return this.clusterName;
156        }
157
158        public void setClusterName(String clusterName) {
159                this.clusterName = clusterName;
160        }
161
162        public List<String> getContactPoints() {
163                return this.contactPoints;
164        }
165
166        public int getPort() {
167                return this.port;
168        }
169
170        public void setPort(int port) {
171                this.port = port;
172        }
173
174        public String getUsername() {
175                return this.username;
176        }
177
178        public void setUsername(String username) {
179                this.username = username;
180        }
181
182        public String getPassword() {
183                return this.password;
184        }
185
186        public void setPassword(String password) {
187                this.password = password;
188        }
189
190        public Compression getCompression() {
191                return this.compression;
192        }
193
194        public void setCompression(Compression compression) {
195                this.compression = compression;
196        }
197
198        @DeprecatedConfigurationProperty(reason = "Implement a ClusterBuilderCustomizer bean instead.")
199        @Deprecated
200        public Class<? extends LoadBalancingPolicy> getLoadBalancingPolicy() {
201                return this.loadBalancingPolicy;
202        }
203
204        @Deprecated
205        public void setLoadBalancingPolicy(
206                        Class<? extends LoadBalancingPolicy> loadBalancingPolicy) {
207                this.loadBalancingPolicy = loadBalancingPolicy;
208        }
209
210        public ConsistencyLevel getConsistencyLevel() {
211                return this.consistencyLevel;
212        }
213
214        public void setConsistencyLevel(ConsistencyLevel consistency) {
215                this.consistencyLevel = consistency;
216        }
217
218        public ConsistencyLevel getSerialConsistencyLevel() {
219                return this.serialConsistencyLevel;
220        }
221
222        public void setSerialConsistencyLevel(ConsistencyLevel serialConsistency) {
223                this.serialConsistencyLevel = serialConsistency;
224        }
225
226        public int getFetchSize() {
227                return this.fetchSize;
228        }
229
230        public void setFetchSize(int fetchSize) {
231                this.fetchSize = fetchSize;
232        }
233
234        @DeprecatedConfigurationProperty(reason = "Implement a ClusterBuilderCustomizer bean instead.")
235        @Deprecated
236        public Class<? extends ReconnectionPolicy> getReconnectionPolicy() {
237                return this.reconnectionPolicy;
238        }
239
240        @Deprecated
241        public void setReconnectionPolicy(
242                        Class<? extends ReconnectionPolicy> reconnectionPolicy) {
243                this.reconnectionPolicy = reconnectionPolicy;
244        }
245
246        @DeprecatedConfigurationProperty(reason = "Implement a ClusterBuilderCustomizer bean instead.")
247        @Deprecated
248        public Class<? extends RetryPolicy> getRetryPolicy() {
249                return this.retryPolicy;
250        }
251
252        @Deprecated
253        public void setRetryPolicy(Class<? extends RetryPolicy> retryPolicy) {
254                this.retryPolicy = retryPolicy;
255        }
256
257        public Duration getConnectTimeout() {
258                return this.connectTimeout;
259        }
260
261        public void setConnectTimeout(Duration connectTimeout) {
262                this.connectTimeout = connectTimeout;
263        }
264
265        public Duration getReadTimeout() {
266                return this.readTimeout;
267        }
268
269        public void setReadTimeout(Duration readTimeout) {
270                this.readTimeout = readTimeout;
271        }
272
273        public boolean isSsl() {
274                return this.ssl;
275        }
276
277        public void setSsl(boolean ssl) {
278                this.ssl = ssl;
279        }
280
281        public boolean isJmxEnabled() {
282                return this.jmxEnabled;
283        }
284
285        public void setJmxEnabled(boolean jmxEnabled) {
286                this.jmxEnabled = jmxEnabled;
287        }
288
289        public String getSchemaAction() {
290                return this.schemaAction;
291        }
292
293        public void setSchemaAction(String schemaAction) {
294                this.schemaAction = schemaAction;
295        }
296
297        public Pool getPool() {
298                return this.pool;
299        }
300
301        /**
302         * Pool properties.
303         */
304        public static class Pool {
305
306                /**
307                 * Idle timeout before an idle connection is removed. If a duration suffix is not
308                 * specified, seconds will be used.
309                 */
310                @DurationUnit(ChronoUnit.SECONDS)
311                private Duration idleTimeout = Duration.ofSeconds(120);
312
313                /**
314                 * Pool timeout when trying to acquire a connection from a host's pool.
315                 */
316                private Duration poolTimeout = Duration.ofMillis(5000);
317
318                /**
319                 * Heartbeat interval after which a message is sent on an idle connection to make
320                 * sure it's still alive. If a duration suffix is not specified, seconds will be
321                 * used.
322                 */
323                @DurationUnit(ChronoUnit.SECONDS)
324                private Duration heartbeatInterval = Duration.ofSeconds(30);
325
326                /**
327                 * Maximum number of requests that get queued if no connection is available.
328                 */
329                private int maxQueueSize = 256;
330
331                public Duration getIdleTimeout() {
332                        return this.idleTimeout;
333                }
334
335                public void setIdleTimeout(Duration idleTimeout) {
336                        this.idleTimeout = idleTimeout;
337                }
338
339                public Duration getPoolTimeout() {
340                        return this.poolTimeout;
341                }
342
343                public void setPoolTimeout(Duration poolTimeout) {
344                        this.poolTimeout = poolTimeout;
345                }
346
347                public Duration getHeartbeatInterval() {
348                        return this.heartbeatInterval;
349                }
350
351                public void setHeartbeatInterval(Duration heartbeatInterval) {
352                        this.heartbeatInterval = heartbeatInterval;
353                }
354
355                public int getMaxQueueSize() {
356                        return this.maxQueueSize;
357                }
358
359                public void setMaxQueueSize(int maxQueueSize) {
360                        this.maxQueueSize = maxQueueSize;
361                }
362
363        }
364
365}