Advertisement
Guest User

Untitled

a guest
May 17th, 2017
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.35 KB | None | 0 0
  1. import javax.inject.Inject;
  2. @Import(value = {LocalDbAuthConfig.class,CassandraAvailabiltyConfig.class})
  3. @Configuration
  4. @EnableRetry
  5. @EnableAspectJAutoProxy
  6. @EnableDbAuthPropertySupport
  7. public class CassandraConfig extends AbstractCassandraConfiguration {
  8.  
  9. private Logger LOGGER= LogManager.getLogger();
  10.  
  11. @Value("${cassandra.contact.hosts}")
  12. private String cassandraContactPoints;
  13.  
  14. @Value("${cassandra.contact.port}")
  15. private Integer cassandraPort;
  16.  
  17. @Value("${cassandra.auth.alias}")
  18. private String cassandraDbAuthAlias;
  19.  
  20. private String cassandraUsername;
  21.  
  22. private String cassandraPassword;
  23.  
  24. @Value("${cassandra.schema.create}")
  25. private boolean createSchema;
  26.  
  27. @Inject
  28. private DbAuthBean dbAuthBean;
  29.  
  30. @Inject
  31. private RetryTemplate retryTemplate;
  32.  
  33. @Inject
  34. private ApplicationContext context;
  35.  
  36.  
  37. @Bean
  38. @DependsOn("assertCassandraAvailable")
  39. public CassandraClusterFactoryBean cluster() {
  40.  
  41. cassandraUsername = dbAuthBean.property(cassandraDbAuthAlias, "username");
  42. cassandraPassword = dbAuthBean.property(cassandraDbAuthAlias, "password");
  43.  
  44. MyCassandraClusterFactoryBean cluster = new MyCassandraClusterFactoryBean();
  45. cluster.setContactPoints(cassandraContactPoints);
  46. cluster.setPort(cassandraPort);
  47. cluster.setUsername(cassandraUsername);
  48. cluster.setPassword(cassandraPassword);
  49. cluster.setQueryOptions(new QueryOptions().setConsistencyLevel(ConsistencyLevel.ALL));
  50. cluster.setReconnectionPolicy(new ConstantReconnectionPolicy(100L));
  51.  
  52. return cluster;
  53. }
  54.  
  55. @Bean
  56. @DependsOn("assertCassandraAvailable")
  57. public CassandraSessionFactoryBean session() throws Exception {
  58.  
  59. MyCassandraSessionFactoryBean session = new MyCassandraSessionFactoryBean();
  60. session.setCluster(cluster().getObject());
  61. session.setConverter(cassandraConverter());
  62. session.setKeyspaceName(getKeyspaceName());
  63. session.setSchemaAction(getSchemaAction());
  64. session.setStartupScripts(getStartupScripts());
  65. session.setShutdownScripts(getShutdownScripts());
  66. return session;
  67. }
  68.  
  69. @Bean
  70. public RetryTemplate retryTemplate() {
  71. RetryTemplate retryTemplate = new RetryTemplate();
  72.  
  73. FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
  74. fixedBackOffPolicy.setBackOffPeriod(200l);
  75. retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
  76.  
  77. SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
  78. retryPolicy.setMaxAttempts(5);
  79. retryTemplate.setRetryPolicy(retryPolicy);
  80.  
  81. return retryTemplate;
  82. }
  83.  
  84. /**
  85. *Extend the Bean so that we can override it's Singleton behaviour.
  86. */
  87. public class MyCassandraClusterFactoryBean extends CassandraClusterFactoryBean
  88. {
  89. @Override public boolean isSingleton() {
  90. return false;
  91. }
  92.  
  93. @Override public Cluster getObject() {
  94. return super.getObject();
  95. }
  96.  
  97. @Override public void afterPropertiesSet() throws Exception {
  98. super.afterPropertiesSet();
  99. }
  100. }
  101. public class MyCassandraSessionFactoryBean extends CassandraSessionFactoryBean {
  102. public MyCassandraSessionFactoryBean() {
  103. super();
  104. }
  105.  
  106. @Override public void afterPropertiesSet() throws Exception {
  107.  
  108. retryTemplate.execute(new RetryCallback<Void, Exception>() {
  109. @Override public Void doWithRetry(RetryContext retryContext) throws Exception {
  110. try {
  111. MyCassandraSessionFactoryBean.super.afterPropertiesSet();
  112. }
  113. catch (Exception e) {
  114. cluster().afterPropertiesSet();
  115. Cluster cluster = cluster().getObject();
  116. setCluster(cluster);
  117. throw e;
  118. }
  119. return null;
  120. }
  121. });
  122.  
  123. }
  124. }
  125.  
  126. @Override
  127. public String[] getEntityBasePackages() {
  128. return new String[]{Component.class.getPackage().getName()};
  129. }
  130.  
  131. @Value("${cassandra.keyspace}")
  132. private String cassandraKeyspace;
  133.  
  134. @Override
  135. protected String getKeyspaceName() {
  136. return cassandraKeyspace;
  137. }
  138.  
  139. @Override
  140. public SchemaAction getSchemaAction() {
  141. if (createSchema) {
  142. return SchemaAction.CREATE_IF_NOT_EXISTS;
  143. }
  144. else {
  145. return SchemaAction.NONE;
  146. }
  147. }
  148.  
  149. @Bean
  150. public CassandraMappingContext mappingContext() {
  151. return new BasicCassandraMappingContext();
  152. }
  153.  
  154. @Bean
  155. public CassandraConverter converter() {
  156. return new MappingCassandraConverter(mappingContext());
  157. }
  158.  
  159. @Override
  160. public RetryPolicy getRetryPolicy() {
  161. return DefaultRetryPolicy.INSTANCE;
  162. }
  163. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement