public static boolean isClusterActive() { try { Builder builder = Cluster.builder() .withQueryOptions( new QueryOptions() .setConsistencyLevel(ConsistencyLevel.QUORUM) .setSerialConsistencyLevel(ConsistencyLevel.LOCAL_SERIAL)); cluster = builder.addContactPoint("127.0.0.1").build(); session = cluster.connect(); return true; } catch (Exception e) { return false; } }
public static Cluster createCluster(CassandraConfiguration config) throws DataSourceException { boolean debugEnabled = false; if (log.isDebugEnabled()) { debugEnabled = true; } String userName = config.getUsername(); String password = config.getPassword(); List<String> connections = config.getHosts(); String clusterName = config.getClusterName(); int port = config.getPort(); boolean jmxDisabled = config.getJmxDisabled(); if (userName == null || password == null) { throw new DataSourceException("Can't create cluster with empty userName or Password"); } if (clusterName == null) { throw new DataSourceException("Can't create cluster with empty cluster name"); } if (connections == null || connections.isEmpty()) { throw new DataSourceException("Can't create cluster with empty connection string"); } int maxConnections = config.getMaxConnections(); int concurrency = config.getConcurrency(); boolean async = config.getAsync(); String compression = config.getCompression(); StringBuilder configProps = new StringBuilder(); configProps .append(" concurrency: " + concurrency) .append("\n mode: " + (async ? "asynchronous" : "blocking")) .append("\n per-host connections: " + maxConnections) .append("\n compression: " + compression); if (debugEnabled) { log.debug(configProps.toString()); } Cluster cluster = null; try { PoolingOptions poolOptions = CassandraDatasourceUtils.createPoolingOptions(config); SocketOptions socketOptions = CassandraDatasourceUtils.createSocketOptions(config); // Create cluster Builder builder = Cluster.builder(); for (String con : connections) { builder.addContactPoints(con); } builder .withPoolingOptions(poolOptions) .withSocketOptions(socketOptions) .withPort(port) .withCredentials(userName, password); CassandraDatasourceUtils.createRetryPolicy(config, builder); CassandraDatasourceUtils.createReconnectPolicy(config, builder); CassandraDatasourceUtils.createLoadBalancingPolicy(config, builder); if (jmxDisabled) { builder.withoutMetrics(); builder.withoutJMXReporting(); } if (ProtocolOptions.Compression.SNAPPY.name().equalsIgnoreCase(compression)) { builder.withCompression(ProtocolOptions.Compression.SNAPPY); } else { builder.withCompression(ProtocolOptions.Compression.NONE); } cluster = builder.build(); // validate cluster information after connect StringBuilder metaDataAfterConnect = new StringBuilder(); Set<Host> allHosts = cluster.getMetadata().getAllHosts(); for (Host h : allHosts) { metaDataAfterConnect.append("["); metaDataAfterConnect.append(h.getDatacenter()); metaDataAfterConnect.append("-"); metaDataAfterConnect.append(h.getRack()); metaDataAfterConnect.append("-"); metaDataAfterConnect.append(h.getAddress()); metaDataAfterConnect.append("]\n"); } if (debugEnabled) { log.debug("Cassandra Cluster: " + metaDataAfterConnect.toString()); } } catch (NoHostAvailableException ex) { throw new DataSourceException(" No Host available to access ", ex); } catch (Exception ex) { throw new DataSourceException(" Can not create cluster ", ex); } return cluster; }