Interest Registration Example

With client interest registration, you can get automatic updates from the server for individual keys, key lists, and all keys matching a regular expression.

Running the Example

This example expands the Server Managed Caching Example. The default behavior for the example program is to register interest in all data in the region. This example modifies the program's interest registration to show explicit key registration and the use of regular expressions.

The example has one worker client, which gets and puts data in its cache, and one consumer client, which receives events from the single cacheserver. The consumer client registers interest in some or all keys in the data region. The worker client updates its cache, and the updates are automatically forwarded to the server cache. The server forwards to the consumer only those events in which the consumer has registered interest. The clients have an asynchronous listener that reports local cache activity to standard out, so you can see what is happening. The server uses a cache loader to load data it does not already have in its cache when the worker client requests it.

Note: To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up Your Environment.
You will run this example twice, once registering interest in a specific set of keys, the second time, registering interest in keys matching a regular expression.
  1. In one session, start the cacheserver:

    $ cacheserver start cache-xml-file=xml/Server.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 consumer client:

    $ java quickstart.ClientConsumer keyset
  3. When the consumer client asks you to do so, start the worker client in another session:

    $ java quickstart.ClientWorker
  4. Follow the instructions on the screens, noting the listener output from each client. After both clients exit, stop the cacheserver:

    $ cacheserver stop
  5. To run the example using a regular expression for interest registration, run the example again, replacing "keyset" in the ClientConsumer startup with "regex".

Example Source Files

Program and cache configuration source files for the clients and the server, including the loader and listener declared in the Server.xml and Client.xml files, respectively. (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
Server.xml Configures a cache to serve caching clients. The example region also is configured with a loader.
Client.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 A client program that exercises the server cache. A consumer client that registers interest in events on the server. The server sends automatic updates for the events. A very simple CacheLoader implementation. A CacheListener that reports cache events.

Related Javadocs

See also:
  • com.gemfire.cache.Region.registerInterest