forked from spring-projects/spring-boot
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request spring-projects#2214 from jdubois/master
* spring-projectsgh-2214: Add Cassandra support
- Loading branch information
Showing
32 changed files
with
1,417 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
...uator/src/main/java/org/springframework/boot/actuate/health/CassandraHealthIndicator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2012-2015 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.actuate.health; | ||
|
||
import org.springframework.data.cassandra.core.CassandraAdminOperations; | ||
import org.springframework.util.Assert; | ||
|
||
import com.datastax.driver.core.ResultSet; | ||
import com.datastax.driver.core.querybuilder.QueryBuilder; | ||
import com.datastax.driver.core.querybuilder.Select; | ||
|
||
/** | ||
* Simple implementation of a {@link HealthIndicator} returning status information for | ||
* Cassandra data stores. | ||
* | ||
* @author Julien Dubois | ||
* @since 1.3.0 | ||
*/ | ||
public class CassandraHealthIndicator extends AbstractHealthIndicator { | ||
|
||
private CassandraAdminOperations cassandraAdminOperations; | ||
|
||
/** | ||
* Create a new {@link CassandraHealthIndicator} instance. | ||
* @param cassandraAdminOperations the Cassandra admin operations | ||
*/ | ||
public CassandraHealthIndicator(CassandraAdminOperations cassandraAdminOperations) { | ||
Assert.notNull(cassandraAdminOperations, | ||
"CassandraAdminOperations must not be null"); | ||
this.cassandraAdminOperations = cassandraAdminOperations; | ||
} | ||
|
||
@Override | ||
protected void doHealthCheck(Health.Builder builder) throws Exception { | ||
try { | ||
Select select = QueryBuilder.select("release_version") | ||
.from("system", "local"); | ||
ResultSet results = this.cassandraAdminOperations.query(select); | ||
if (results.isExhausted()) { | ||
builder.up(); | ||
return; | ||
} | ||
String version = results.one().getString(0); | ||
builder.up().withDetail("version", version); | ||
} | ||
catch (Exception ex) { | ||
builder.down(ex); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...ain/java/org/springframework/boot/autoconfigure/cassandra/CassandraAutoConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* Copyright 2012-2015 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. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package org.springframework.boot.autoconfigure.cassandra; | ||
|
||
import org.springframework.beans.BeanUtils; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; | ||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; | ||
import org.springframework.boot.context.properties.EnableConfigurationProperties; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.util.StringUtils; | ||
|
||
import com.datastax.driver.core.Cluster; | ||
import com.datastax.driver.core.QueryOptions; | ||
import com.datastax.driver.core.SocketOptions; | ||
import com.datastax.driver.core.policies.LoadBalancingPolicy; | ||
import com.datastax.driver.core.policies.ReconnectionPolicy; | ||
import com.datastax.driver.core.policies.RetryPolicy; | ||
|
||
/** | ||
* {@link EnableAutoConfiguration Auto-configuration} for Cassandra. | ||
* | ||
* @author Julien Dubois | ||
* @author Phillip Webb | ||
* @since 1.3.0 | ||
*/ | ||
@Configuration | ||
@ConditionalOnClass({ Cluster.class }) | ||
@EnableConfigurationProperties(CassandraProperties.class) | ||
public class CassandraAutoConfiguration { | ||
|
||
@Autowired | ||
private CassandraProperties properties; | ||
|
||
@Bean | ||
@ConditionalOnMissingBean | ||
public Cluster cluster() { | ||
CassandraProperties properties = this.properties; | ||
Cluster.Builder builder = Cluster.builder() | ||
.withClusterName(properties.getClusterName()) | ||
.withPort(properties.getPort()); | ||
if (properties.getCompression() != null) { | ||
builder.withCompression(properties.getCompression()); | ||
} | ||
if (properties.getLoadBalancingPolicy() != null) { | ||
LoadBalancingPolicy policy = instantiate(properties.getLoadBalancingPolicy()); | ||
builder.withLoadBalancingPolicy(policy); | ||
} | ||
builder.withQueryOptions(getQueryOptions()); | ||
if (properties.getReconnectionPolicy() != null) { | ||
ReconnectionPolicy policy = instantiate(properties.getReconnectionPolicy()); | ||
builder.withReconnectionPolicy(policy); | ||
} | ||
if (properties.getRetryPolicy() != null) { | ||
RetryPolicy policy = instantiate(properties.getRetryPolicy()); | ||
builder.withRetryPolicy(policy); | ||
} | ||
builder.withSocketOptions(getSocketOptions()); | ||
if (properties.isSsl()) { | ||
builder.withSSL(); | ||
} | ||
String points = properties.getContactPoints(); | ||
builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points)); | ||
return builder.build(); | ||
} | ||
|
||
public static <T> T instantiate(Class<T> type) { | ||
return BeanUtils.instantiate(type); | ||
} | ||
|
||
private QueryOptions getQueryOptions() { | ||
QueryOptions options = new QueryOptions(); | ||
CassandraProperties properties = this.properties; | ||
if (properties.getConsistencyLevel() != null) { | ||
options.setConsistencyLevel(properties.getConsistencyLevel()); | ||
} | ||
if (properties.getSerialConsistencyLevel() != null) { | ||
options.setSerialConsistencyLevel(properties.getSerialConsistencyLevel()); | ||
} | ||
options.setFetchSize(properties.getFetchSize()); | ||
return options; | ||
} | ||
|
||
private SocketOptions getSocketOptions() { | ||
SocketOptions options = new SocketOptions(); | ||
options.setConnectTimeoutMillis(this.properties.getConnectTimeoutMillis()); | ||
options.setReadTimeoutMillis(this.properties.getReadTimeoutMillis()); | ||
return options; | ||
} | ||
|
||
} |
Oops, something went wrong.