Search in sources :

Example 1 with Cluster

use of com.datastax.driver.core.Cluster in project spring-boot by spring-projects.

the class CassandraAutoConfiguration method cluster.

@Bean
@ConditionalOnMissingBean
public Cluster cluster() {
    CassandraProperties properties = this.properties;
    Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withPort(properties.getPort());
    if (properties.getUsername() != null) {
        builder.withCredentials(properties.getUsername(), properties.getPassword());
    }
    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));
    customize(builder);
    return builder.build();
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) ConditionalOnMissingBean(org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean) Bean(org.springframework.context.annotation.Bean)

Example 2 with Cluster

use of com.datastax.driver.core.Cluster in project spring-boot by spring-projects.

the class CassandraAutoConfigurationTests method createClusterWithOverrides.

@Test
public void createClusterWithOverrides() {
    load("spring.data.cassandra.cluster-name=testcluster");
    assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
    Cluster cluster = this.context.getBean(Cluster.class);
    assertThat(cluster.getClusterName()).isEqualTo("testcluster");
}
Also used : Cluster(com.datastax.driver.core.Cluster) Test(org.junit.Test)

Example 3 with Cluster

use of com.datastax.driver.core.Cluster in project spring-boot by spring-projects.

the class CassandraAutoConfigurationTests method createClusterWithDefault.

@Test
public void createClusterWithDefault() {
    load();
    assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
    Cluster cluster = this.context.getBean(Cluster.class);
    assertThat(cluster.getClusterName()).startsWith("cluster");
}
Also used : Cluster(com.datastax.driver.core.Cluster) Test(org.junit.Test)

Example 4 with Cluster

use of com.datastax.driver.core.Cluster in project zipkin by openzipkin.

the class DefaultSessionFactory method buildCluster.

// Visible for testing
static Cluster buildCluster(Cassandra3Storage cassandra) {
    Cluster.Builder builder = Cluster.builder();
    List<InetSocketAddress> contactPoints = parseContactPoints(cassandra);
    int defaultPort = findConnectPort(contactPoints);
    builder.addContactPointsWithPorts(contactPoints);
    // This ends up protocolOptions.port
    builder.withPort(defaultPort);
    if (cassandra.username != null && cassandra.password != null) {
        builder.withCredentials(cassandra.username, cassandra.password);
    }
    builder.withRetryPolicy(ZipkinRetryPolicy.INSTANCE);
    builder.withLoadBalancingPolicy(new TokenAwarePolicy(new LatencyAwarePolicy.Builder(cassandra.localDc != null ? DCAwareRoundRobinPolicy.builder().withLocalDc(cassandra.localDc).build() : new RoundRobinPolicy()).build()));
    builder.withPoolingOptions(new PoolingOptions().setMaxConnectionsPerHost(HostDistance.LOCAL, cassandra.maxConnections));
    if (cassandra.useSsl) {
        builder = builder.withSSL();
    }
    return builder.build();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) LatencyAwarePolicy(com.datastax.driver.core.policies.LatencyAwarePolicy) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)

Example 5 with Cluster

use of com.datastax.driver.core.Cluster in project zipkin by openzipkin.

the class DefaultSessionFactory method create.

/**
   * Creates a session and ensures schema if configured. Closes the cluster and session if any
   * exception occurred.
   */
@Override
public Session create(Cassandra3Storage cassandra) {
    Closer closer = Closer.create();
    try {
        Cluster cluster = closer.register(buildCluster(cassandra));
        cluster.register(new QueryLogger.Builder().build());
        Session session;
        if (cassandra.ensureSchema) {
            session = closer.register(cluster.connect());
            Schema.ensureExists(cassandra.keyspace, session);
            session.execute("USE " + cassandra.keyspace);
        } else {
            session = cluster.connect(cassandra.keyspace);
        }
        initializeUDTs(session);
        return session;
    } catch (RuntimeException e) {
        try {
            closer.close();
        } catch (IOException ignored) {
        }
        throw e;
    }
}
Also used : Closer(com.google.common.io.Closer) Cluster(com.datastax.driver.core.Cluster) IOException(java.io.IOException) Session(com.datastax.driver.core.Session)

Aggregations

Cluster (com.datastax.driver.core.Cluster)123 Session (com.datastax.driver.core.Session)74 ResultSet (com.datastax.driver.core.ResultSet)41 Row (com.datastax.driver.core.Row)37 Test (org.junit.Test)33 ConstantReconnectionPolicy (com.datastax.driver.core.policies.ConstantReconnectionPolicy)30 Host (com.datastax.driver.core.Host)10 Metadata (com.datastax.driver.core.Metadata)9 PreparedStatement (com.datastax.driver.core.PreparedStatement)9 ArrayList (java.util.ArrayList)9 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)8 IOException (java.io.IOException)8 PoolingOptions (com.datastax.driver.core.PoolingOptions)7 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)7 BeforeClass (org.junit.BeforeClass)7 KeyspaceMetadata (com.datastax.driver.core.KeyspaceMetadata)6 QueryOptions (com.datastax.driver.core.QueryOptions)6 InetSocketAddress (java.net.InetSocketAddress)6 BoundStatement (com.datastax.driver.core.BoundStatement)5 SocketOptions (com.datastax.driver.core.SocketOptions)5