Continuous Querying Example

With continuous querying (CQ), 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 cacheserver, 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.

  1. In one session, start the cacheserver:

    $ cacheserver start cache-xml-file=xml/CqServer.xml
    
    The server starts in the background, sending information to the screen:
    Starting CacheServer with pid: 11283
    CacheServer pid: 11283 status: running
    $
    
  2. In the same session, start the CQ client:

    $ java 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 cacheserver:

    $ cacheserver stop
    

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 $SamplesDirectory/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 $SamplesDirectory/quickstart/quickstart
CqClient.java A client that creates and executes continuous queries on the server and waits for the events.
SimpleCacheLoader.java A very simple CacheLoader implementation.
SimpleCacheListener.java A CacheListener that reports cache events.
SimpleCqListener.java 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