DB/NoSQL · Java EE

Making Cassandra to play with a Wildfly

Introduction

I have used in the past MongoDB but after running some metrics and based on the case study of my needs, I switched to Apache Cassandra and never looked back. If you don’t know what what Apache Cassandra is or DataStax(the guys are leading the Apache Cassandra movement) have a look here. The second need would be configure the driver to be used as a module so that is able to be injected in several modules or other war/ears.

Motivation behind this small/mini howto was the fact that there is no where documented clearly which are the dependencies of the java driver. So I decided to discover them and wildfly modular system offered a very nice candidate to quickly track down deps.

Prerequisites

In order to run the code bellow you need the following software:

  1. WildflyAS application server. You can find it here.
  2. Apache Cassandra NoSQL store. I firmly suggest getting the bundles provided by datastax. You can find it here.
  3. Oracle JDK 1.7 at least. You can find it here

Doing the tricks

In order to make it work grab the driver from Cassandra planet and in your WildFly installation director create a directory named com/datastax/cassandra/main/ in Linux issue the following commands:

cd $WILDFLY_HOME/modules/system/layers/base/com

mkdir -p datastax/cassandra/main/

cd $WILDFLY_HOME/modules/system/layers/base/com/datastax/cassandra/main/

touch module.xml

After you finished with that you will need to install the following deps as modules also:

  • Netty version 3.9.*
  • Metrics

Then you need to have the module.xml file of your cassandra_driver as the following

<module xmlns=”urn:jboss:module:1.1″ name=”com.datastax.cassandra”>

<resources>
<resource-root path=”cassandra-driver-core-2.0.2.jar”/>
<!– Insert resources here –>
</resources>
<dependencies>
<module name=”javax.api”/>
<module name=”javax.transaction.api”/>
<module name=”org.slf4j” />
<module name=”com.google.guava” />
<module name=”org.jboss.netty” />
<module name=”com.codahale.metrics” />
</dependencies>
</module>

To install metrics which is a library for obtaining metrics in production and is used by the driver you need to do the same things as you did for the cassandra driver and then define the module.xml file of the metrics module to be like the following one:

<module xmlns=”urn:jboss:module:1.1″ name=”com.codahale.metrics”>

<resources>
<resource-root path=”metrics-core-3.0.2.jar”/>
<!– Insert resources here –>
</resources>
<dependencies>
<module name=”javax.api”/>
<module name=”javax.transaction.api”/>
<module name=”sun.jdk” />
<module name=”org.slf4j” />
</dependencies>
</module>

 

I will not delve into the details of configuring Netty also as a module because is really straightforward and has really no deps to external jars. Grab the jar from netty.io site and define a simple Wildfly module. The reference that module in your apache cassandra driver module.xml file.

In order to test you can write a simple jar that has a MDB and writes something in the database. I used the following code deployed in the Wildfly I have locally.

@Override
public void onMessage(Message arg0) {
TextMessage msg = (TextMessage) arg0;
try {
PreparedStatement stmt = session.prepare(“INSERT INTO mo(msisdn,campaign_id,scenario_id,script_id,play_id,answer)”
+”VALUES(?,?,?,?,?,?)”);
BoundStatement boundStatement = new BoundStatement(stmt);
String message = msg.getText();
String msisdn = null;
matcher = pattern.matcher(message);
while(matcher.find()){
msisdn = matcher.group();
}
int campaign_id = (random.nextInt(50 – 1) + 1);
int scenario_id = (random.nextInt(100 – 1) + 1);
int script_id = (random.nextInt(200 – 1) + 1);
int play_id = (random.nextInt(300 – 1) + 1);
boundStatement.bind(msisdn,campaign_id,scenario_id,script_id,play_id,”Hello from :”+msisdn);
session.execute(boundStatement);
} catch (JMSException jmse) {
LOGGER.error(“Failed to receive message”, jmse);
}finally{
sisyphusCluster.close();
}

Firing up the Wildfly instance would make the magic to work. You also need a client to inject messages in the queue you have defined the MDB is listening from. That part of the code is out of the scope of this mini howto.

Conclusion

This small howto is targeted to enthusiasts of these two technologies, which I am a great fun and follower, and sometimes are having hard times dealing with the configuration. I am a strong believer that in order to motivate the community and people to use tools you should offer them solution to configuration problems. I know many people don’t agree with that since they believe this hard path leads to inner knowledge. But maybe also drives away new users. I hope you find this mini howto helpful!

regards

\n\b

Advertisements

2 thoughts on “Making Cassandra to play with a Wildfly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s