Skip to content

Instantly share code, notes, and snippets.

@zznate
Created October 14, 2015 23:01
Show Gist options
  • Save zznate/e3db151242ce1d4c20bf to your computer and use it in GitHub Desktop.
Save zznate/e3db151242ce1d4c20bf to your computer and use it in GitHub Desktop.
Example of a "Component" or "Resource" controlling Cassandra access
public class MyClusterManager {
private Session session;
private Cluster cluster;
@javax.annotation.PostConstruct
public void init() throws IOException
{
cluster = createCluster(); // private method that builds a cluster
session = createSession(); // private method that builds a session
}
@javax.annotation.PreDestroy
public void shutdown() throws IOException
{
session.close();
cluster.close();
}
// Expose block-and-tackle operations thusly:
@Override
public ResultSet execute(final Statement boundStatement)
{
return session.execute(boundStatement);
}
@Override
public ResultSetFuture executeAsync(final Statement boundStatement)
{
return session.executeAsync(boundStatement);
}
@Override
public PreparedStatement prepare(final String statement)
{
return session.prepare(statement);
}
@Override
public ResultSet execute(final String statement)
{
return session.execute(statement);
}
@Override
public Metadata getMetadata()
{
return cluster.getMetadata();
}
//...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment