Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class CassUtil {
- private static final Logger LOGGER = Logger.getInstance(CassUtil.class);
- private Session session;
- private Cluster cluster;
- private static class Holder {
- private static final CassUtil INSTANCE = new CassUtil();
- }
- public static CassUtil getInstance() {
- return Holder.INSTANCE;
- }
- private CassUtil() {
- List<String> servers = TestUtils.HOSTNAMES;
- String username =
- TestUtils.loadCredentialFile().getProperty(TestUtils.USERNAME);
- String password =
- TestUtils.loadCredentialFile().getProperty(TestUtils.PASSWORD);
- PoolingOptions opts = new PoolingOptions();
- opts.setCoreConnectionsPerHost(HostDistance.LOCAL,
- opts.getCoreConnectionsPerHost(HostDistance.LOCAL));
- Builder builder = Cluster.builder();
- cluster =
- builder
- .addContactPoints(servers.toArray(new String[servers.size()]))
- .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
- .withPoolingOptions(opts)
- .withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
- .withLoadBalancingPolicy(
- DCAwareRoundRobinPolicy
- .builder()
- .withLocalDc(
- !TestUtils.isProduction() ? "DC2" : TestUtils.getCurrentLocation()
- .get().name().toLowerCase()).build())
- .withCredentials(username, password).build();
- try {
- session = cluster.connect("testkeyspace");
- StringBuilder sb = new StringBuilder();
- Set<Host> allHosts = cluster.getMetadata().getAllHosts();
- for (Host host : allHosts) {
- sb.append("[");
- sb.append(host.getDatacenter());
- sb.append(host.getRack());
- sb.append(host.getAddress());
- sb.append("]");
- }
- LOGGER.logInfo("CONNECTED SUCCESSFULLY TO CASSANDRA CLUSTER: " + sb.toString());
- } catch (NoHostAvailableException ex) {
- LOGGER.logError("error= ", ExceptionUtils.getStackTrace(ex));
- } catch (Exception ex) {
- LOGGER.logError("error= " + ExceptionUtils.getStackTrace(ex));
- }
- }
- public void shutdown() {
- LOGGER.logInfo("Shutting down the whole cassandra cluster");
- if (null != session) {
- session.close();
- }
- if (null != cluster) {
- cluster.close();
- }
- }
- public Session getSession() {
- if (session == null) {
- throw new IllegalStateException("No connection initialized");
- }
- return session;
- }
- public Cluster getCluster() {
- return cluster;
- }
- public List<ProcessMetadata> getProcessMetadata() {
- return processMetadata;
- }
- public List<String> getTopicMetadata() {
- return topicMetadata;
- }
- public List<ProcMetadata> getProcMetadata() {
- return procMetadata;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement