Simple Continuous Query Example

The continous query example shows how a callback rule function is used to gather results generated by the query.

An example callback rule function is as follows:

MyRF(ID, isBatchEnd, hasEnded, row, closure)
if (hasEnded) {
    System.debugOut(ID + ": ========");
} else if (isBatchEnd) {
    System.debugOut(ID + ": --------");
} else {
    Object[] columns = row;
    String agent = columns[0];
    double sales = columns[1];
    String zip = columns[2];
    System.debugOut(id
        + ": Agent " + agent
        + " sold $" + sales
        + " in " + zip
        + ". " + closure);
}

Create the Query

Query.create("report853", "select agent_name, total_sales, zipcode from /Concepts/Sales");

Open and Execute the Query Statement

String id = requestEvent@extId;
String stmt = "S" + id;
String clbk = "C" + id;
Query.Statement.open("report853", stmt);
Query.Statement.executeWithCallback(
stmt, clbk, "/MyRF", true, "@@@@");

Where requestEvent is an event, and "/MyRF" is the path to the rule function shown at the beginning of the section. The true parameter indicates that this is a continuous query.

Sample Output

In the following sample output , each row of data (generated when a relevant change occurs in the cache) is one batch, because the query does not involve ordering or aggregation. The last line in the sample indicates that the query has ended. For example, someone closed the statement (not shown in the code sample).

C123: Agent Mary Smith sold $15063.28 in 94304. @@@@
C123: --------
Time passes…
C123: Agent Robert Ng sold $14983.05 in 94304. @@@@
C123: --------
Time passes…
C123: Agent Jose Ortiz sold $16244.78 in 94304. @@@@
C123: --------
C123: ========

Function Calls

The following example shows the parameter values for each function call.

As a reminder: the first Boolean indicates whether the batch has ended or not; the second Boolean indicates whether the execution has ended or not.

  • Mary Smith makes a sale.
    MyRF(clbk, false, false, ["Mary Smith", 15063.28, 94304], "@@@@")
    MyRF(clbk, true, false, null, "@@@@")
  • Time passes… Robert Ng makes a sale.
    MyRF(clbk, false, false, ["Robert Ng", 14983.05, 94304], "@@@@")
    MyRF(clbk, true, false, null, "@@@@")
  • Time passes… Jose Ortiz makes a sale.
    MyRF(clbk, false, false, ["Jose Ortiz", 16244.78, 94304], "@@@@")
    MyRF(clbk, true, false, null, "@@@@")
    MyRF(clbk, true, true, null, "@@@@")