OneDB Change Streams API for Java

This topic demonstrates how to use the OneDB Change Streams API to create data streams and capture changing data from the server using the Java programming language.

The OneDB Change Streams client API allows you to easily stream changes made from a logged database in Informix into your Java application. The Change Streams client provides a high-level API that abstracts the details of the underlying streaming system. This allows you to get triggered events when there is a change to a specific table you instruct the API to watch. The API will trigger events in your application for changes to a database table. For more information, see Change Data Capture records for a description of the types of events the client can receive.

OneDB Change Streams API currently allows you to configure the underlying OneDB Change Data Capture feature (see Change data capture) and receive events from this system in well-defined objects. You can subscribe to events from one or more tables, specifying which columns of data you are interested in receiving.

The client API is built into a Java library (JAR file) with a dependency on the OneDB JDBC database driver. Both libraries are packaged as part of the JDBC installation and the client API is available on Maven central alongside the latest JDBC drivers.

The Javadoc for the API is distributed alongside the Java library. An example of a simple Java application using the new Change Streams API is shown below:
import com.informix.jdbcx.IfxDataSource;
import com.informix.stream.api.IfmxStreamRecord;
import com.informix.stream.cdc.IfxCDCEngine;
import com.informix.stream.cdc.records.IfxCDCOperationRecord;

public class CDCExample {
  public static void main(String[] args) throws Exception {
    String url = args.length > 0 ? args[0]
        : "jdbc:informix-sqli://localhost:20290/syscdcv1:user=informix;password=informix";
    IfxDataSource ds = new IfxDataSource(url);
    IfxCDCEngine.Builder builder = new IfxCDCEngine.Builder(ds);
    builder.watchTable("testdb:informix.cdcTable", "a", "b");

    builder.timeout(5); // default 5 second timeout

    // Build the engine
    try (IfxCDCEngine engine = builder.build()) {

      // initialize the engine (creates the connections and begins listening for
      // changes)
      engine.init();
      IfmxStreamRecord record = null;

      // This loop is where you can inject logic that compiles
      // transactions, look for commits, throw away rollbacks
      // The data here is all Java typed, so it can be easily then
      // sent to MQTT, other JDBC drivers, streaming engines, or anything
      // else you can think of.
      while ((record = engine.getRecord()) != null) {
        // Print out the basic record information
        System.out.println(record);

        // If it is an insert/update/delete, print the column data
        if (record.hasOperationData()) {
          System.out.println(((IfxCDCOperationRecord) record).getData());
        }
      }
    }
  }
}