Java Driver 1.0 for Apache Cassandra

Asynchronous I/O

You can execute statements on a session objects in two different ways. Calling execute blocks the calling thread until the statement finishes executing, but a session also allows for asynchronous and non-blocking I/O by calling the executeAsync method.

Modify the functionality of the SimpleClient class by extending it and execute queries asynchronously on a cluster.

Procedure

  1. Add a new class, AsynchronousExample, to your simple-cassandra-client project. It should extend the SimpleClient class.
    package com.example.cassandra;
    
    public class AsynchronousExample extends SimpleClient {
    }
    
  2. Add an instance method, getRows, and implement it.
    1. Implement the getRows method so it returns a ResultSetFuture object.
      public ResultSetFuture getRows() {}
      The ResultSetFuture class implements the java.util.concurrent.Future<V> interface. Objects which implement this interface allow for non-blocking computation. The calling code may wait for the completion of the computation or to check if it is done.
    2. Using the QueryBuilder class, build a SELECT query that returns all the rows for the song table for all columns.
      Query query = QueryBuilder.select().all().from("simplex", "songs");
    3. Execute the query asynchronously and return the ResultSetFuture object.
      return getSession().executeAsync(query);
  3. Add a class method, main, to your class implementation and add calls to create the schema, load the data, and then query it using the getRows method.
    public static void main(String[] args) {
       AsynchronousExample client = new AsynchronousExample();
       client.connect("127.0.0.1");
       client.createSchema();
       client.loadData();
       ResultSetFuture results = client.getRows();
       for (Row row : results.getUninterruptibly()) {
          System.out.printf( "%s: %s / %s\n",
                row.getString("artist"), 
                row.getString("title"), 
                row.getString("album") );
       }
       client.dropSchema("simplex");
       client.close();
    }
    
    Of course, in our implementation, the call to getUninterruptibly blocks until the result set future has completed execution of the statement on the session object. Functionally it is no different from executing the SELECT query synchronously.

AsynchronousExample code listing

package com.example.cassandra;

import com.datastax.driver.core.Query;
import com.datastax.driver.core.ResultSetFuture;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.querybuilder.QueryBuilder;

public class AsynchronousExample extends SimpleClient {
   public AsynchronousExample() {
   }
   
   public ResultSetFuture getRows() {
      Query query = QueryBuilder.select().all().from("simplex", "songs");
      return getSession().executeAsync(query);
   }

   public static void main(String[] args) {
      AsynchronousExample client = new AsynchronousExample();
      client.connect("127.0.0.1");
      client.createSchema();
      client.loadData();
      ResultSetFuture results = client.getRows();
      for (Row row : results.getUninterruptibly()) {
         System.out.printf( "%s: %s / %s\n",
               row.getString("artist"), 
               row.getString("title"), 
               row.getString("album") );
      }
      client.dropSchema("simplex");
      client.close();
   }
}
Show/hide