Advertisement
Guest User

Untitled

a guest
Nov 29th, 2016
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. public class CassUtil {
  2. private static final Logger LOGGER = Logger.getInstance(CassUtil.class);
  3.  
  4. private Session session;
  5. private Cluster cluster;
  6.  
  7. private static class Holder {
  8. private static final CassUtil INSTANCE = new CassUtil();
  9. }
  10.  
  11. public static CassUtil getInstance() {
  12. return Holder.INSTANCE;
  13. }
  14.  
  15. private CassUtil() {
  16. List<String> servers = TestUtils.HOSTNAMES;
  17. String username =
  18. TestUtils.loadCredentialFile().getProperty(TestUtils.USERNAME);
  19. String password =
  20. TestUtils.loadCredentialFile().getProperty(TestUtils.PASSWORD);
  21.  
  22. PoolingOptions opts = new PoolingOptions();
  23. opts.setCoreConnectionsPerHost(HostDistance.LOCAL,
  24. opts.getCoreConnectionsPerHost(HostDistance.LOCAL));
  25.  
  26. Builder builder = Cluster.builder();
  27. cluster =
  28. builder
  29. .addContactPoints(servers.toArray(new String[servers.size()]))
  30. .withRetryPolicy(DowngradingConsistencyRetryPolicy.INSTANCE)
  31. .withPoolingOptions(opts)
  32. .withReconnectionPolicy(new ConstantReconnectionPolicy(100L))
  33. .withLoadBalancingPolicy(
  34. DCAwareRoundRobinPolicy
  35. .builder()
  36. .withLocalDc(
  37. !TestUtils.isProduction() ? "DC2" : TestUtils.getCurrentLocation()
  38. .get().name().toLowerCase()).build())
  39. .withCredentials(username, password).build();
  40.  
  41. try {
  42. session = cluster.connect("testkeyspace");
  43. StringBuilder sb = new StringBuilder();
  44. Set<Host> allHosts = cluster.getMetadata().getAllHosts();
  45. for (Host host : allHosts) {
  46. sb.append("[");
  47. sb.append(host.getDatacenter());
  48. sb.append(host.getRack());
  49. sb.append(host.getAddress());
  50. sb.append("]");
  51. }
  52. LOGGER.logInfo("CONNECTED SUCCESSFULLY TO CASSANDRA CLUSTER: " + sb.toString());
  53. } catch (NoHostAvailableException ex) {
  54. LOGGER.logError("error= ", ExceptionUtils.getStackTrace(ex));
  55. } catch (Exception ex) {
  56. LOGGER.logError("error= " + ExceptionUtils.getStackTrace(ex));
  57. }
  58. }
  59.  
  60.  
  61. public void shutdown() {
  62. LOGGER.logInfo("Shutting down the whole cassandra cluster");
  63. if (null != session) {
  64. session.close();
  65. }
  66. if (null != cluster) {
  67. cluster.close();
  68. }
  69. }
  70.  
  71. public Session getSession() {
  72. if (session == null) {
  73. throw new IllegalStateException("No connection initialized");
  74. }
  75. return session;
  76. }
  77.  
  78. public Cluster getCluster() {
  79. return cluster;
  80. }
  81.  
  82. public List<ProcessMetadata> getProcessMetadata() {
  83. return processMetadata;
  84. }
  85.  
  86. public List<String> getTopicMetadata() {
  87. return topicMetadata;
  88. }
  89.  
  90. public List<ProcMetadata> getProcMetadata() {
  91. return procMetadata;
  92. }
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement