Pivotal GemFire® v8.1

Continuous Querying Example

Continuous Querying Example

The client registers a query with the server and chooses whether to receive an initial query result set. After initialization, the query runs continuously against server cache changes, and the client receives events for any changes to the query result set.

Here's a typical use case: A client application displays active stock trades to a screen. The client creates a CQ on the server's trading data set with a WHERE clause specifying "status='active'". The client requests an initial result set for the CQ and initializes its screen display with those results. From then on, the client updates its screen display with the CQ events as they arrive from the server. An entry whose status goes from 'active' to 'inactive' on the server is reported to the client as a query destroy event for the entry, and the client application removes the entry from the active trades display. Newly 'active' entries are reported as additions to the result set and are added to the display.

Running the Example

This example has one client that connects to a cache server, creates a continuous query, and then updates the server with data that changes the query result set. The client reports the updates to standard out. It also has a CQ listener that reports all CQ events, so you can see the effects on the result set. The server uses a cache loader to load any data requested by the client that it does not already have in its cache.

Note: To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up Your Environment for Running the Examples.
  1. In one session, start the cache server:

    $ gfsh start server --name=server_cq --cache-xml-file=xml/CqServer.xml --classpath="$GEMFIRE/SampleCode/quickstart/classes"

    The gfsh command starts a cache server in the background, displays the status of the server and some environment information, and then exits to the command prompt.

  2. In the same session, start the CQ client:

    $ java -cp "$GEMFIRE/SampleCode/quickstart/classes:$GEMFIRE/lib/server-dependencies.jar" quickstart.CqClient

    The client runs a CQ on the server, then creates cache events that modify the CQ result set, prompting the server to send it CQ events.

  3. When the client exits, stop the cache server:

    $ gfsh stop server --dir=server_cq

Example Source Files

Program and cache configuration source files for the client and server, including the loader and listener declared in CqServer.xml. (The server is a GemFire cacheserver process and does not have an example source file.)

Table 1. Cache configuration files, located in SampleCode/quickstart/xml
CqServer.xml Configures a cache to serve CQ clients.
CqClient.xml Configures a region as a client region in a client/server cache. The region's pool connects to the cacheserver.

Table 2. Java program files, located in SampleCode/quickstart/quickstart A client that creates and executes continuous queries on the server and waits for the events. A very simple CacheLoader implementation. A CacheListener that reports cache events. A CqListener that reports continuous query events.

Related Topics
Delta Propagation

Related Javadocs

See also:
  • com.gemstone.gemfire.cache.query.QueryService
  • com.gemstone.gemfire.cache.query.CqQuery
  • com.gemstone.gemfire.cache.query.CqListener
  • com.gemstone.gemfire.cachge.query.CqEvent