Evident ClearStone 5.0 uses Cassandra for its persistent storage of application performance monitoring data, as written about elsewhere in our blog. There are some cases where you might want to embed Cassandra within your Java application or run it as a webapp within your application server for testing purposes. Ching-Cheng Chen, longtime stalwart of the company, sat with me recently to help me with this blog post, furnishing all the detail. Let me start by sharing his strong suggestion that embedding Cassandra within Tomcat is not a good idea for production.
Here are some notes about embedding Cassandra within Tomcat:
Take a look at the Cassandra (for 0.7.0 release) start up script; it will give you clues on how to start up/shut down Cassandra within your own Java class.
To start up a Cassandra node, instantiate an org.apache.cassandra.thrift.CassandraDaemon object and invoke its activate() method in a separate thread.
To shut down the Cassandra node, invoke the deactivate() method on the CassandraDaemon object you created during start up.
The default Cassandra thread pool worked fine for us in our testing mode. Everyone should modify their Cassandra thread pool configuration according to their environment, though.
But what about the configuration? Cassandra will load the configuration file cassandra.yaml if found in the classpath by default. If there is a need to use different configuration base on environment, you can always generate a configuration file on the fly. To generate Cassandra configuration file dynamically, instantiate an org.apache.cassandra.config.Config object and populate it with your preferred configuration, then write the configuration using the snakeyaml API included in Cassandra distribution.
import org.apache.cassandra.config.Config;
import org.apache.cassandra.utils.SkipNullRepresenter;
import org.yaml.snakeyaml.Dumper;
import org.yaml.snakeyaml.DumperOptions;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.nodes.Tag;
…
// create configuation object
Config config = new Config();
config.cluster_name = “ClusterName”;
config.rpc_port = 9160;
…
// write configuration file
FileWriter fw = new FileWriter(new File(classPathDir+”/cassandra.yaml”));
DumperOptions options =DumperOptions();
options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
SkipNullRepresenter representer = newSkipNullRepresenter();
representer.addClassTag(Config.class, Tag.MAP);
Dumper dumper = new Dumper(representer, options);
Yaml yaml = new Yaml(dumper);
yaml.dump(config, fw);
fw.close();
I close with this reminder: Do NOT use this for production; your application(s) will compete for resources with Cassandra; that is certainly not a good thing. For the simpler testing environment, though, it can be useful.
Read more about our solution for Cassandra NoSQL Performance Monitoring
