Home > Net >  Spring Boot application should start even if Cassandra is down
Spring Boot application should start even if Cassandra is down

Time:09-29

I want to make sure that my App gets started even if Cassandra is down. What is the best way to handle that?

I am using AbstractReactiveCassandraConfiguration and multiple keyspace implementation.

    @Bean
    @NonNull
    @Override
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
        cqlSessionFactoryBean.setContactPoints(contactPoints);
        cqlSessionFactoryBean.setLocalDatacenter(localDataCenter);
        cqlSessionFactoryBean.setUsername(username);
        cqlSessionFactoryBean.setPassword(password);
        return cqlSessionFactoryBean;
    }

Keyspace is set at the individual keyspace config file.

Keyspace 1 and keyspace 2 with similar kinds of configurations.

@Configuration
@EnableReactiveCassandraRepositories(basePackages = "com.abc.repository.cassandra.xyz", reactiveCassandraTemplateRef = "keyspaceXYZTemplate")
public class XYZCassandraConfiguration extends CassandraConfig {

    @Override
    protected String getKeyspaceName() {
        return "KEYSPACE_NAME";
    }

    @Bean("keyspaceXYZTemplate")
    public ReactiveCassandraTemplate reactiveCassandraTemplate(@Qualifier("xyzBean") CqlSessionFactoryBean cqlSessionFactoryBean) {
        final ReactiveSession reactiveSession = new DefaultBridgedReactiveSession(cqlSessionFactoryBean.getObject());
        return new ReactiveCassandraTemplate(reactiveSession, cassandraConverter());
    }

    @NonNull
    @Override
    @Primary
    @Bean(name = "xyzBean")
    public CqlSessionFactoryBean cassandraSession() {
        final CqlSessionFactoryBean cqlSessionFactoryBean = super.cassandraSession();
        cqlSessionFactoryBean.setKeyspaceName(getKeyspaceName());
        return cqlSessionFactoryBean;
    }
}

Stack Trace: When the app starts and Cassandra is down.

Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean
Invocation of init method failed; nested exception is com.datastax.oss.driver.api.core.AllNodesFailedException: Could not reach any contact point, make sure you've provided valid addresses (showing first 1 nodes, use getAllErrors() for more): Node(endPoint=localhost:9042, hostId=null, hashCode=70c95ec9): [com.datastax.oss.driver.api.core.connection.ConnectionInitException: [s0|control|connecting...] Protocol initialization request, step 1 (OPTIONS): failed to send request (io.netty.channel.StacklessClosedChannelException)]
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:800) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:229) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.autowireConstructor(AbstractAutowireCapableBeanFactory.java:1354) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBeanInstance(AbstractAutowireCapableBeanFactory.java:1204) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:564) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:524) ~[spring-beans-5.3.8.jar:5.3.8]
    at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335) ~[spring-beans-5.3.8.jar:5.3.8]

CodePudding user response:

This isn't possible since your app has a dependency on being connected to the database before it can start.

The beans can't be instantiated unless the driver is connected to Cassandra. Cheers!

  • Related