Notifying clients of map updates using continuous query

[Version 8.6 and later]You can be notified in your client Java™ virtual machine (JVM) when objects or entries are inserted or updated in the data grid.

Before you begin

If you want to use continuous query, then you must enable IBM® eXtremeIO, which is a transport mechanism, that is used to communicate between container servers and clients. For more information about enabling eXtremeIO, see Configuring IBM eXtremeIO (XIO).

About this task

When you develop client applications that interact with the data grid, you might require queries that retrieve automatic, real-time results when entries that match the filtering criteria are inserted, updated or deleted. For example, you develop a stock quote application that requires frequent updates. These updates reflect changes that occur in the stock market. Therefore, it is critical that your application is notified of changes instantly, so that you can supply accurate and timely results. Continuous query has a low-memory footprint that can proactively notify clients as changes in the data grid occur.
Use the following procedure to program your client applications to use continuous query.
Restriction: Queries that specify a value attribute path of null are not supported if the value object is not a primitive Java type, such as a string or integer. When null is specified, the query filter is used to query the entire value object.

Procedure

  1. Call the continuous query manager in your client application.
    For example, insert the following line of code:
    ContinuousQueryManager cqMan = ContinuousQueryManagerFactory.getManager(og);
  2. Define a filter or filter chain.
    You can implement your own filters, or you can use the following basic filters that are provided: AND, OR, LT, GT, EQ, and so on. Instantiated filters or filter chains are given unique identifies. For more information about all supported filters, see Accessing Java API documentation to find the continuous query APIs.
    The following code example demonstrates one way to use the equals (EQ) basic filter. Assume the data grid contains Customer objects with the field, firstName. The filter returns true when firstName equals Larry.
    EQFilter<String, String> equalsFilter = new EQFilter<String, String>("firstName", "Larry");
  3. Define a query using the filter that you created in the previous step; for example:
    ContinuousQueryTopic<String, Customer> topic = 
      cqMan.<String, Customer> defineContinuousQuery("myMapName", equalsFilter, true, true, true);
  4. Optional: Get the continuous query cache to access the client-side results of the continuous query.
    If the query is defined as a keys-only query, only the keys that satisfy the query are in the continuous query cache; for example:
    ContinuousQueryCache cache = topic.getCache();
  5. Optional: Additionally, you can register a class that implements the ContinuousQueryListener interface with a ContinuousQueryTopic instance to receive notifications when the results of the continuous query change.
    Invoke the addListener method to register the listener; for example:
    ContinuousQueryListener<String, Customer> listener = new MyCQListener<String, Customer>();
    topic.addListener(listener);

What to do next

See API documentation: Package com.ibm.websphere.objectgrid.continuousquery for more information about the continuous query API.