Search in sources :

Example 1 with RetryPolicy

use of com.datastax.driver.core.policies.RetryPolicy 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 RetryPolicy

use of com.datastax.driver.core.policies.RetryPolicy in project tutorials by eugenp.

the class CassandraConfiguration method cluster.

@Bean
public Cluster cluster(CassandraProperties properties) {
    Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withProtocolVersion(protocolVersion).withPort(getPort(properties));
    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(properties));
    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(properties));
    if (properties.isSsl()) {
        builder.withSSL();
    }
    String points = properties.getContactPoints();
    builder.addContactPoints(StringUtils.commaDelimitedListToStringArray(points));
    Cluster cluster = builder.build();
    cluster.getConfiguration().getCodecRegistry().register(LocalDateCodec.instance).register(CustomZonedDateTimeCodec.instance);
    if (metricRegistry != null) {
        cluster.init();
        metricRegistry.registerAll(cluster.getMetrics().getRegistry());
    }
    return cluster;
}
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) Bean(org.springframework.context.annotation.Bean)

Example 3 with RetryPolicy

use of com.datastax.driver.core.policies.RetryPolicy in project eventapis by kloiasoft.

the class CassandraSession method cluster.

/*    private Cluster cluster() {
            Cluster.Builder builder = Cluster.builder();
            Arrays.stream(eventStoreConfig.getContactPoints().split(";")).forEach(s -> {
                String[] hostPort = s.split(":");
                builder.addContactPointsWithPorts(new InetSocketAddress(hostPort[0], Integer.parseInt(hostPort[1])));
            });
            builder.withPoolingOptions(eventStoreConfig.getPoolingOptions());

            Cluster cluster = builder.build();
            cluster.register(QueryLogger.builder().build());
            return cluster.init();
        }*/
private Cluster cluster() {
    EventStoreConfig properties = eventStoreConfig;
    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.addContactPointsWithPorts(Arrays.stream(StringUtils.split(points, ",")).map(s -> {
        String[] split = s.split(":");
        String host = split[0];
        int port = properties.getPort();
        try {
            port = Integer.parseInt(split[1]);
        } catch (Exception e) {
            log.trace(e.getMessage());
        }
        return new InetSocketAddress(host, port);
    }).collect(Collectors.toList()));
    return builder.build();
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) InetSocketAddress(java.net.InetSocketAddress) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy)

Example 4 with RetryPolicy

use of com.datastax.driver.core.policies.RetryPolicy in project cas by apereo.

the class DefaultCassandraSessionFactory method initializeCassandraCluster.

private static Cluster initializeCassandraCluster(final BaseCassandraProperties cassandra) {
    final Cluster cluster;
    final PoolingOptions poolingOptions = new PoolingOptions().setMaxRequestsPerConnection(HostDistance.LOCAL, cassandra.getMaxRequestsPerConnection()).setConnectionsPerHost(HostDistance.LOCAL, cassandra.getCoreConnections(), cassandra.getMaxConnections());
    final DCAwareRoundRobinPolicy.Builder dcPolicyBuilder = DCAwareRoundRobinPolicy.builder();
    if (StringUtils.isNotBlank(cassandra.getLocalDc())) {
        dcPolicyBuilder.withLocalDc(cassandra.getLocalDc());
    }
    final TokenAwarePolicy loadBalancingPolicy = new TokenAwarePolicy(dcPolicyBuilder.build(), cassandra.isShuffleReplicas());
    final SocketOptions socketOptions = new SocketOptions().setConnectTimeoutMillis(cassandra.getConnectTimeoutMillis()).setReadTimeoutMillis(cassandra.getReadTimeoutMillis());
    final QueryOptions queryOptions = new QueryOptions().setConsistencyLevel(ConsistencyLevel.valueOf(cassandra.getConsistencyLevel())).setSerialConsistencyLevel(ConsistencyLevel.valueOf(cassandra.getSerialConsistencyLevel()));
    final RetryPolicy retryPolicy = RetryPolicyType.valueOf(cassandra.getRetryPolicy()).getRetryPolicy();
    final Cluster.Builder builder = Cluster.builder().withCredentials(cassandra.getUsername(), cassandra.getPassword()).withPoolingOptions(poolingOptions).withProtocolVersion(ProtocolVersion.valueOf(cassandra.getProtocolVersion())).withLoadBalancingPolicy(loadBalancingPolicy).withSocketOptions(socketOptions).withRetryPolicy(new LoggingRetryPolicy(retryPolicy)).withCompression(ProtocolOptions.Compression.valueOf(cassandra.getCompression())).withPort(cassandra.getPort()).withQueryOptions(queryOptions);
    Arrays.stream(StringUtils.split(cassandra.getContactPoints(), ',')).forEach(contactPoint -> builder.addContactPoint(StringUtils.trim(contactPoint)));
    cluster = builder.build();
    if (LOGGER.isDebugEnabled()) {
        cluster.getMetadata().getAllHosts().forEach(clusterHost -> LOGGER.debug("Host [{}]:\n\n\tDC: [{}]\n\tRack: [{}]\n\tVersion: [{}]\n\tDistance: [{}]\n\tUp?: [{}]\n", clusterHost.getAddress(), clusterHost.getDatacenter(), clusterHost.getRack(), clusterHost.getCassandraVersion(), loadBalancingPolicy.distance(clusterHost), clusterHost.isUp()));
    }
    return cluster;
}
Also used : DCAwareRoundRobinPolicy(com.datastax.driver.core.policies.DCAwareRoundRobinPolicy) SocketOptions(com.datastax.driver.core.SocketOptions) PoolingOptions(com.datastax.driver.core.PoolingOptions) Cluster(com.datastax.driver.core.Cluster) TokenAwarePolicy(com.datastax.driver.core.policies.TokenAwarePolicy) QueryOptions(com.datastax.driver.core.QueryOptions) LoggingRetryPolicy(com.datastax.driver.core.policies.LoggingRetryPolicy) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) LoggingRetryPolicy(com.datastax.driver.core.policies.LoggingRetryPolicy)

Example 5 with RetryPolicy

use of com.datastax.driver.core.policies.RetryPolicy in project jhipster-sample-app-cassandra by jhipster.

the class CassandraConfiguration method cluster.

@Bean
public Cluster cluster(CassandraProperties properties) {
    Cluster.Builder builder = Cluster.builder().withClusterName(properties.getClusterName()).withProtocolVersion(protocolVersion).withPort(getPort(properties));
    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(properties));
    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(properties));
    if (properties.isSsl()) {
        builder.withSSL();
    }
    builder.addContactPoints(StringUtils.toStringArray(properties.getContactPoints()));
    Cluster cluster = builder.build();
    TupleType tupleType = cluster.getMetadata().newTupleType(DataType.timestamp(), DataType.varchar());
    cluster.getConfiguration().getCodecRegistry().register(LocalDateCodec.instance).register(InstantCodec.instance).register(new ZonedDateTimeCodec(tupleType));
    if (metricRegistry != null) {
        cluster.init();
        metricRegistry.registerAll(cluster.getMetrics().getRegistry());
    }
    return cluster;
}
Also used : LoadBalancingPolicy(com.datastax.driver.core.policies.LoadBalancingPolicy) ReconnectionPolicy(com.datastax.driver.core.policies.ReconnectionPolicy) TupleType(com.datastax.driver.core.TupleType) Cluster(com.datastax.driver.core.Cluster) RetryPolicy(com.datastax.driver.core.policies.RetryPolicy) ZonedDateTimeCodec(com.datastax.driver.extras.codecs.jdk8.ZonedDateTimeCodec) Bean(org.springframework.context.annotation.Bean)

Aggregations

Cluster (com.datastax.driver.core.Cluster)5 RetryPolicy (com.datastax.driver.core.policies.RetryPolicy)5 LoadBalancingPolicy (com.datastax.driver.core.policies.LoadBalancingPolicy)4 ReconnectionPolicy (com.datastax.driver.core.policies.ReconnectionPolicy)4 Bean (org.springframework.context.annotation.Bean)3 PoolingOptions (com.datastax.driver.core.PoolingOptions)1 QueryOptions (com.datastax.driver.core.QueryOptions)1 SocketOptions (com.datastax.driver.core.SocketOptions)1 TupleType (com.datastax.driver.core.TupleType)1 DCAwareRoundRobinPolicy (com.datastax.driver.core.policies.DCAwareRoundRobinPolicy)1 LoggingRetryPolicy (com.datastax.driver.core.policies.LoggingRetryPolicy)1 TokenAwarePolicy (com.datastax.driver.core.policies.TokenAwarePolicy)1 ZonedDateTimeCodec (com.datastax.driver.extras.codecs.jdk8.ZonedDateTimeCodec)1 InetSocketAddress (java.net.InetSocketAddress)1 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)1