Skip to content

CQL embedded cassandra server

DuyHai DOAN edited this page Nov 13, 2013 · 16 revisions

Achilles exposes a simple builder to start an embedded Cassandra server and bootstrap the framework.

Such feature is already available for JUnit testing using the AchillesCQLResourceBuilder discussed previously. However there is still a need to start an embedded Cassandra outside of testing context and more importantly, to connect to several keyspaces.

The builder API:

CQLEmbeddedServerBuilder
	.withEntityPackages("test") || noEntityPackages()
	.withClusterName("Test Cluster")
	.withDataFolder("/home/user/cassandra/data")
	.withCommitLogFolder("/home/user/cassandra/commitlog")
	.withSavedCachesFolder("/home/user/cassandra/saved_caches")
	.cleanDataFilesAtStartup(true)
	.withConfigYamlFile("/home/user/cassandra/cassandra.yaml")
	.withCQLPort(9042)
	.withThriftPort(9160)
	.withStoragePort(7990)
	.withStorageSSLPort(7999)
	.withKeyspaceName("achilles")
	.withDurableWrite(true)
	.buildNativeSessionOnly() || buildPersistenceManagerFactory() || buildPersistenceManager();

Most of the parameters are optional, below are the default values:

  • clusterName: Achilles Embedded Cassandra Cluster
  • dataFolder: target/cassandra_embedded/data
  • commitLogFolder: target/cassandra_embedded/commitlog
  • savedCachesFolder: target/cassandra_embedded/saved_caches
  • cleanDataFilesAtStartup: true
  • configYAMLFile: target/cassandra_embedded/cassandra.yaml
  • cqlPort: randomized at runtime
  • thriftPort: randomized at runtime
  • storagePort: randomized at runtime
  • storageSSLPort: randomized at runtime
  • keyspaceName: achilles_embedded
  • durableWrite: true

The builder can return:

  1. either an Achilles CQLPersistenceManagerFactory
  2. or an Achilles CQLPersistenceManager
  3. or just a native Session object from the driver

There will be at most one embedded Cassandra server started per JVM (more precisely, per classloader) event if many CQLEmbeddedServer are built. But because the keyspace name is provided each time, the builder will bootstrap as many Achilles instances or Session objects as distinct keyspaces.

Example is better than words:

CQLPersistenceManagerFactory factory1 = CQLEmbeddedServerBuilder
  .withEntityPackages("my.entity.package1")
  .withKeyspaceName("keyspace1")
  .buildPersistenceManagerFactory();

CQLPersistenceManagerFactory factory2 = CQLEmbeddedServerBuilder
  .withEntityPackages("my.entity.package2")
  .withKeyspaceName("keyspace2")
  .buildPersistenceManagerFactory();

CQLPersistenceManagerFactory factory3 = CQLEmbeddedServerBuilder
  .withEntityPackages("my.entity.package1")
  .withKeyspaceName("keyspace1")
  .buildPersistenceManagerFactory();

assertThat(factory1).isNotEqualTo(factory2);
assertThat(factory1).isEqualTo(factory3);

Home

Clone this wiki locally