Code Monkey home page Code Monkey logo

Comments (5)

nosan avatar nosan commented on May 23, 2024

Hi @saksham-kakkar,

public class EmbeddedCassandraInitializer implements InitializingBean {

	private final CqlSession session;

	public EmbeddedCassandraInitializer(CqlSession session) {
		this.session = session;
	}

	@Override
	public void afterPropertiesSet() throws Exception {
		// initialize cassandra
		CqlScript.ofClassPath("schema.cql").forEachStatement(this.session::execute);
	}

}
public class EmbeddedCassandraInitializerPostProcessor implements BeanPostProcessor, Ordered, BeanFactoryAware {

	private BeanFactory beanFactory;

	@Override
	public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
		this.beanFactory = beanFactory;
	}

	@Override
	public int getOrder() {
		return Ordered.HIGHEST_PRECEDENCE + 1;
	}

	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		return bean;
	}

	@Override
	public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
		if (bean instanceof Cassandra) {
			this.beanFactory.getBean(EmbeddedCassandraInitializer.class);
		}
		return bean;
	}

}
@Configuration(proxyBeanMethods = false)
@Import(EmbeddedCassandraInitializer.class)
public class CassandraConfiguration {

	@Bean
	public static EmbeddedCassandraInitializerPostProcessor embeddedCassandraInitializerPostProcessor() {
		return new EmbeddedCassandraInitializerPostProcessor();
	}

}

from embedded-cassandra-spring-boot-starter.

nosan avatar nosan commented on May 23, 2024

Another way, you can register:

@Configuration(proxyBeanMethods = false)
static class CqlSessionFactoryBeanConfiguration {

@Bean
CqlSessionFactoryBean cqlSessionFactoryBean() {
	CqlSessionFactoryBean cqlSessionFactoryBean = new CqlSessionFactoryBean();
	cqlSessionFactoryBean.setLocalDatacenter("datacenter1");
	cqlSessionFactoryBean.setKeyspaceSpecifications();
	cqlSessionFactoryBean.setKeyspaceStartupScripts();
	cqlSessionFactoryBean.setKeyspaceShutdownScripts();
	return cqlSessionFactoryBean;
}

}

but bear in mind, this will replace the auto-configured CqlSession.

from embedded-cassandra-spring-boot-starter.

saksham-kakkar avatar saksham-kakkar commented on May 23, 2024

Thanks for the quick response. I'll check these out.

from embedded-cassandra-spring-boot-starter.

mwojtaczka avatar mwojtaczka commented on May 23, 2024

Hi @nosan

Failed to instantiate [com.datastax.oss.driver.api.core.CqlSession]: Factory method 'cassandraSession' threw exception; nested exception is com.datastax.oss.driver.api.core.InvalidKeyspaceException: Invalid keyspace

How can I define keyspace before CqlSession init?

from embedded-cassandra-spring-boot-starter.

nosan avatar nosan commented on May 23, 2024

Hi @mwojtaczka
Sorry, I have not seen your message, looks like somehow I just missed it :(
Unfortunately, there is no easy way to do it.

You can either use CqlSessionFactoryBean which supports keyspace creation or use the separate CqlSession to create a keyspace.

from embedded-cassandra-spring-boot-starter.

Related Issues (8)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.