Server Managed Caching Example

Server-managed caching is the model for independent scalability of data and application tiers. You isolate data management in a number of cache servers and connect to them from application server clients, so many clients benefit from the management resources of the servers.

GemFire's client/server caching gives you dynamic server pool management, server load balancing and conditioning, logical server grouping, and automatic server failover for high availability.

The servers run in one distributed system. Each client runs in its own separate distributed system. The clients forward data requests to the servers that their caches cannot fulfill. The clients also forward client-side updates. Additionally, clients can register interest in server-side data, either by specifying the data keys or by registering queries that run continuously. In both cases, updates on the server side are automatically forwarded to the client.

Running the Example

This example shows a simple client/server installation with one worker client, that gets and puts data in its cache, and one consumer client, that just receives events from the server. Both clients are connected to a single cache server. The worker client requests data from the server. If the server does not have the data cached, it automatically runs a data loader to get it. The server returns the data to the worker client. The worker client then updates the data in its cache. The update is automatically distributed to the server. The server forwards all changes made by the worker client to the consumer client. Both clients have an asynchronous listener that reports local caching activity to standard out, so you can see what is happening.

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 (see vFabric GemFire cacheserver for more information).

    $ 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 all-keys
  3. When the consumer client asks you to start the worker client, start it in another session:

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

    $ cacheserver stop

Example Source Files

Program and cache configuration files for the clients and the server, including the loader and listener declared in the Server.xml and Client.xml files. (The server is a GemFire cacheserver process and does not have a Java 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
ClientWorker.java A client program that exercises the server cache.
ClientConsumer.java A consumer client that registers interest in events on the server. The server sends automatic updates for the events.
SimpleCacheLoader.java A very simple CacheLoader implementation.
SimpleCacheListener.java A CacheListener that reports cache events.

Related Javadocs