Search in sources :

Example 6 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project cassandra by apache.

the class CqlConfigHelper method getCluster.

public static Cluster getCluster(String[] hosts, Configuration conf, int port) {
    Optional<AuthProvider> authProvider = getAuthProvider(conf);
    Optional<SSLOptions> sslOptions = getSSLOptions(conf);
    Optional<Integer> protocolVersion = getProtocolVersion(conf);
    LoadBalancingPolicy loadBalancingPolicy = getReadLoadBalancingPolicy(hosts);
    SocketOptions socketOptions = getReadSocketOptions(conf);
    QueryOptions queryOptions = getReadQueryOptions(conf);
    PoolingOptions poolingOptions = getReadPoolingOptions(conf);
    Cluster.Builder builder = Cluster.builder().addContactPoints(hosts).withPort(port).withCompression(ProtocolOptions.Compression.NONE);
    if (authProvider.isPresent())
        builder.withAuthProvider(authProvider.get());
    if (sslOptions.isPresent())
        builder.withSSL(sslOptions.get());
    if (protocolVersion.isPresent()) {
        builder.withProtocolVersion(ProtocolVersion.fromInt(protocolVersion.get()));
    }
    builder.withLoadBalancingPolicy(loadBalancingPolicy).withSocketOptions(socketOptions).withQueryOptions(queryOptions).withPoolingOptions(poolingOptions);
    return builder.build();
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) SocketOptions(com.datastax.driver.core.SocketOptions) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) AuthProvider(com.datastax.driver.core.AuthProvider) PlainTextAuthProvider(com.datastax.driver.core.PlainTextAuthProvider) JdkSSLOptions(com.datastax.driver.core.JdkSSLOptions) SSLOptions(com.datastax.driver.core.SSLOptions) QueryOptions(com.datastax.driver.core.QueryOptions)

Example 7 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project beam by apache.

the class CassandraIO method getCluster.

/**
 * Get a Cassandra cluster using hosts and port.
 */
static Cluster getCluster(ValueProvider<List<String>> hosts, ValueProvider<Integer> port, ValueProvider<String> username, ValueProvider<String> password, ValueProvider<String> localDc, ValueProvider<String> consistencyLevel, ValueProvider<Integer> connectTimeout, ValueProvider<Integer> readTimeout) {
    Cluster.Builder builder = Cluster.builder().addContactPoints(hosts.get().toArray(new String[0])).withPort(port.get());
    if (username != null) {
        builder.withAuthProvider(new PlainTextAuthProvider(username.get(), password.get()));
    }
    DCAwareRoundRobinPolicy.Builder dcAwarePolicyBuilder = new DCAwareRoundRobinPolicy.Builder();
    if (localDc != null) {
        dcAwarePolicyBuilder.withLocalDc(localDc.get());
    }
    builder.withLoadBalancingPolicy(new TokenAwarePolicy(dcAwarePolicyBuilder.build()));
    if (consistencyLevel != null) {
        builder.withQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(consistencyLevel.get())));
    }
    SocketOptions socketOptions = new SocketOptions();
    builder.withSocketOptions(socketOptions);
    if (connectTimeout != null) {
        socketOptions.setConnectTimeoutMillis(connectTimeout.get());
    }
    if (readTimeout != null) {
        socketOptions.setReadTimeoutMillis(readTimeout.get());
    }
    return builder.build();
}
Also used : DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) SocketOptions(com.datastax.driver.core.SocketOptions) Cluster(com.datastax.driver.core.Cluster) PlainTextAuthProvider(com.datastax.driver.core.PlainTextAuthProvider) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) QueryOptions(com.datastax.driver.core.QueryOptions)

Example 8 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project indy by Commonjava.

the class CassandraClient method init.

@PostConstruct
private void init() {
    if (!config.isEnabled()) {
        logger.info("Cassandra client not enabled");
        return;
    }
    host = config.getCassandraHost();
    port = config.getCassandraPort();
    SocketOptions socketOptions = new SocketOptions();
    socketOptions.setConnectTimeoutMillis(config.getConnectTimeoutMillis());
    socketOptions.setReadTimeoutMillis(config.getReadTimeoutMillis());
    Cluster.Builder builder = Cluster.builder().withoutJMXReporting().withRetryPolicy(new ConfigurableRetryPolicy(config.getReadRetries(), config.getWriteRetries())).addContactPoint(host).withPort(port).withSocketOptions(socketOptions);
    username = config.getCassandraUser();
    String password = config.getCassandraPass();
    if (isNotBlank(username) && isNotBlank(password)) {
        logger.info("Build with credentials, user: {}, pass: ****", username);
        builder.withCredentials(username, password);
    }
    cluster = builder.build();
}
Also used : SocketOptions(com.datastax.driver.core.SocketOptions) Cluster(com.datastax.driver.core.Cluster) PostConstruct(javax.annotation.PostConstruct)

Example 9 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project spring-cloud-connectors by spring-cloud.

the class CassandraClusterCreatorTest method shouldCreateClusterWithConfig.

@Test
public void shouldCreateClusterWithConfig() throws Exception {
    CassandraServiceInfo info = new CassandraServiceInfo("local", Collections.singletonList("127.0.0.1"), 9142);
    CassandraClusterConfig config = new CassandraClusterConfig();
    config.setCompression(ProtocolOptions.Compression.NONE);
    config.setPoolingOptions(new PoolingOptions().setPoolTimeoutMillis(1234));
    config.setQueryOptions(new QueryOptions());
    config.setProtocolVersion(ProtocolVersion.NEWEST_SUPPORTED);
    config.setLoadBalancingPolicy(new RoundRobinPolicy());
    config.setReconnectionPolicy(new ConstantReconnectionPolicy(1));
    config.setRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE);
    config.setSocketOptions(new SocketOptions());
    Cluster cluster = creator.create(info, config);
    Configuration configuration = cluster.getConfiguration();
    assertThat(configuration.getProtocolOptions().getCompression(), is(config.getCompression()));
    assertThat(configuration.getQueryOptions(), is(config.getQueryOptions()));
    assertThat(configuration.getSocketOptions(), is(config.getSocketOptions()));
    Policies policies = configuration.getPolicies();
    assertThat(policies.getLoadBalancingPolicy(), is(config.getLoadBalancingPolicy()));
    assertThat(policies.getReconnectionPolicy(), is(config.getReconnectionPolicy()));
    assertThat(policies.getRetryPolicy(), is(config.getRetryPolicy()));
}
Also used : Policies(com.datastax.driver.core.policies.Policies) Configuration(com.datastax.driver.core.Configuration) CassandraServiceInfo(org.springframework.cloud.service.common.CassandraServiceInfo) SocketOptions(com.datastax.driver.core.SocketOptions) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) QueryOptions(com.datastax.driver.core.QueryOptions) RoundRobinPolicy(com.datastax.driver.core.policies.RoundRobinPolicy) ConstantReconnectionPolicy(com.datastax.driver.core.policies.ConstantReconnectionPolicy) Test(org.junit.Test)

Example 10 with SocketOptions

use of com.datastax.driver.core.SocketOptions in project spring-cloud-connectors by spring-cloud.

the class CassandraClusterConfigWithServiceConfig method testClusterFactoryWithConfig.

@Bean(name = "my-service")
public Cluster testClusterFactoryWithConfig() {
    CassandraClusterConfig config = new CassandraClusterConfig();
    SocketOptions socketOptions = new SocketOptions();
    socketOptions.setSendBufferSize(12345);
    config.setSocketOptions(socketOptions);
    return connectionFactory().cluster("my-service", config);
}
Also used : CassandraClusterConfig(org.springframework.cloud.service.column.CassandraClusterConfig) SocketOptions(com.datastax.driver.core.SocketOptions) Bean(org.springframework.context.annotation.Bean)

Aggregations

SocketOptions (com.datastax.driver.core.SocketOptions)16 Cluster (com.datastax.driver.core.Cluster)7 QueryOptions (com.datastax.driver.core.QueryOptions)6 PoolingOptions (com.datastax.driver.core.PoolingOptions)4 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)4 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)3 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)3 RoundRobinPolicy (com.datastax.driver.core.policies.RoundRobinPolicy)3 PlainTextAuthProvider (com.datastax.driver.core.PlainTextAuthProvider)2 SSLOptions (com.datastax.driver.core.SSLOptions)2 WhiteListPolicy (com.datastax.driver.core.policies.WhiteListPolicy)2 InetSocketAddress (java.net.InetSocketAddress)2 PostConstruct (javax.annotation.PostConstruct)2 BeforeClass (org.junit.BeforeClass)2 AuthProvider (com.datastax.driver.core.AuthProvider)1 Configuration (com.datastax.driver.core.Configuration)1 Host (com.datastax.driver.core.Host)1 JdkSSLOptions (com.datastax.driver.core.JdkSSLOptions)1 Metadata (com.datastax.driver.core.Metadata)1 NoHostAvailableException (com.datastax.driver.core.exceptions.NoHostAvailableException)1