Achilles exposes a simple builder to start an embedded Cassandra server and optionally bootstrap the framework.
Such feature is already available for JUnit testing using the AchillesTestResourceBuilder 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.
To use the embedded Cassandra server, you must pull the following dependency:
<dependency>
<groupId>info.archinnov</groupId>
<artifactId>achilles-embedded</artifactId>
<version>${achilles.version}</version>
</dependency>
The builder API:
CassandraShutdownHook shutdownHook = new CassandraShutdownHook();
CassandraEmbeddedServerBuilder
.builder()
.withClusterName("Test Cluster")
.withListenAdress("127.0.0.1")
.withRpcAdress("127.0.0.1")
.withBroadcastAdress("127.0.0.1")
.withBroadcastRPCAdress("127.0.0.1")
.withCommitLogFolder("/home/user/cassandra/commitlog")
.withConcurrentReads(16)
.withConcurrentWrites(16)
.withCQLPort(9042)
.withDataFolder("/home/user/cassandra/data")
.withDurableWrite(true)
.withHintsFolder("/home/user/cassandra/hints")
.withKeyspaceName("achilles_test")
.withParams(new TypedMap(...))
.withSavedCachesFolder("/home/user/cassandra/saved_caches")
.withScript("src/test/resources/startup_script.cql")
.withScriptTemplate("src/test/resources/startup_script_template.cql", values)
.withStoragePort(7990)
.withStorageSSLPort(7999)
.withThriftPort(9160)
.withShutdownHook(shutdownHook)
.cleanDataFilesAtStartup(true)
.buildNativeSession() || buildNativeCluster() || buildServer();
...
// Control the shutdown of the embedded Cassandra instance
shutdownHook.shutdownNow();
Most of the parameters are optional, below are the default values:
withScript() multiple timeswithScriptTemplate() multiple timesThe builder can return:
com.datastax.driver.core.Cluster objectcom.datastax.driver.core.Session objectCassandraEmbeddedServerBuilder objectThere will be at most one embedded Cassandra server started per JVM (more precisely, per classloader)
event if many CassandraEmbeddedServer are built. But because the keyspace name is provided each time,
the builder will create as many keyspaces as specified.
You can inject a shutdown hook to control when to shutdown the embedded Cassandra process.
CassandraShutDownHook shutdownHook = new CassandraShutDownHook();
Session session = CassandraEmbeddedServerBuilder.builder()
.withShutdownHook(shutdownHook)
...
.buildNativeSession();
...
shutdownHook.shutdownNow();
Please note that upon call on shutdownNow(), Achilles will trigger the shutdown of: