I am using Cassandra in my Spring Boot app. When I start the app, the keyspace is automatically created and everything is okay. The problem is that I want to create keyspace in runtime. For example from endpoint or from application.properties. I have tried only with @RefreshScope and after that with
spring.cloud.refresh.extra-refreshable=spring.data.cassandra.keyspace-name
and sent POST on actuator/refresh but both solutions does not work. The Class is simple Person class with @Table annotation.
So my question is: Is it possible to create keyspace in runtime after sending request to actuator/refresh???
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-cassandra</artifactId>
</dependency>
And this is what I have in application.properties
#spring.data.cassandra.keyspace-name=test1_keyspace
#spring.data.cassandra.schema-action=CREATE_IF_NOT_EXISTS
#spring.data.cassandra.contact-points=IP
You can achieve that with this:
@Component
class MySessionRefresh {
@Autowired
Environment env;
@Autowired
Session session;
// omitted constructors for brevity
@EventListener
@Order(Ordered.LOWEST_PRECEDENCE)
public void handle(RefreshScopeRefreshedEvent event) {
String keyspace = env.getProperty("spring.data.cassandra.keyspace-name");
session.execute("CREATE KEYSPACE IF NOT EXISTS " + keyspace + " WITH REPLICATION = { 'class' : 'NetworkTopologyStrategy', 'datacenter1' : 3 };");
}
}
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments