Replicated Caching Example

Error formatting macro: composition-setup: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil

Replicated caching gives you a complete copy of your data in each JVM in the distributed system that hosts the replicated region. Any change made by any distributed system member is automatically forwarded into the replicated region. You can use this to keep a complete copy in one place for such things as high availability and synchronization with an outside data store. In a client/server installation, backup servers that replicate primary server data can take over when the primary fails, with no data loss. Read more about replicated regions.

Running the Example

This example shows a producer/consumer system. The producer acts as a data feed, putting data into the region using a replicated proxy. A replicated proxy does not store anything in its own JVM but sends it to any replicated regions it finds in its distributed system.

The consumer region is configured as replicated so anything the producer adds or updates is automatically copied to the consumer. The consumer region has a listener that reports all updates to standard out, so you can see when the copies arrive. A GemFire cache listener handles changes to the data region.

To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up the Environment.
  1. In one terminal session, start the consumer:
    $ java quickstart.PushConsumer
    
  2. After that session starts, in another session, start the producer:
    $ java quickstart.PushProducer
    

Example Listings

Program and cache configuration file listings for the producer and consumer, including the listener, SimpleCacheListener.java, declared in the PushConsumer.xml file.

Error formatting macro: deck: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;

/**
 * This example shows a producer/consumer system with the consumer configured 
 * as a replicate of the producer (push model, complete data replication). Please
 * refer to the quickstart guide for instructions on how to run this example. 
 * 
 * @author GemStone Systems, Inc.
 * 
 * @since 4.1.1
 */
public class PushProducer {

  public static void main(String[] args) throws Exception {
    System.out.println("\nConnecting to the distributed system and creating the cache.");
    
    // Create the cache which causes the cache-xml-file to be parsed
    Cache cache = new CacheFactory()
      .set("name", "PushProducer")
      .set("cache-xml-file", "xml/PushProducer.xml")
      .create();

    // Get the exampleRegion
    Region<String,String> exampleRegion = cache.getRegion("exampleRegion");
    System.out.println("Example region, " + exampleRegion.getFullPath() + ", created in cache. ");

    // Create 5 entries and then update those entries
    for (int iter = 0; iter < 2; iter++) {
      for (int count = 0; count < 5; count++) {
        String key = "key" + count;
        String value =  "value" + (count + 100*iter);
        System.out.println("Putting entry: " + key + ", " + value);
        exampleRegion.put(key, value);
      }
    }
    
    // Close the cache and disconnect from GemFire distributed system
    System.out.println("\nClosing the cache and disconnecting.");
    cache.close();
    
    System.out.println("\nPlease press Enter in the PushConsumer.");
  }
}
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">

<!--
  | PushProducer.xml
  |Configures a producer region for a data replication example.
  | 
  | The producer region is not replicated - the consumer region is.
 -->
<cache>
	<region name="exampleRegion" refid="REPLICATE_PROXY" />
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import com.gemstone.gemfire.cache.Cache;
import com.gemstone.gemfire.cache.CacheFactory;
import com.gemstone.gemfire.cache.Region;

/**
 * This example shows a producer/consumer system with the consumer configured 
 * as a replicate of the producer (push model, complete data replication). Please
 * refer to the quickstart guide for instructions on how to run this example. 
 * 
 * @author GemStone Systems, Inc.
 * 
 * @since 4.1.1
 */
public class PushConsumer {

  public static void main(String[] args) throws Exception {
    System.out.println("\nThis example shows how a distributed region works with replication enabled. I'll create a replicate region, then the producer will create the same region and put entries into it. Because my region is a replicate, all of the producer's puts are automatically pushed into my region. ");
    System.out.println("\nConnecting to the distributed system and creating the cache.");
    
    // Create the cache which causes the cache-xml-file to be parsed
    Cache cache = new CacheFactory()
      .set("name", "PushConsumer")
      .set("cache-xml-file", "xml/PushConsumer.xml")
      .create();

    // Get the exampleRegion
    Region<String,String> exampleRegion = cache.getRegion("exampleRegion");
    System.out.println("Example region, " + exampleRegion.getFullPath() + ", created in cache. ");
 
    System.out.println("\nPlease start the PushProducer.\n");
    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
    bufferedReader.readLine();
    
    // Close the cache and disconnect from GemFire distributed system
    System.out.println("Closing the cache and disconnecting.");
    cache.close();
  }
}
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">

<!--
  | PushConsumer.xml
  |
  | Configures a consumer region for a data replication example.
-->
<cache>
	<region name="exampleRegion">
		<region-attributes refid="REPLICATE">
			<cache-listener>
				<class-name>quickstart.SimpleCacheListener</class-name>
			</cache-listener>
		</region-attributes>
	</region>
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import com.gemstone.gemfire.cache.Declarable;
import com.gemstone.gemfire.cache.EntryEvent;
import com.gemstone.gemfire.cache.RegionEvent;
import com.gemstone.gemfire.cache.util.CacheListenerAdapter;
import java.util.Properties;

public class SimpleCacheListener<K,V> extends CacheListenerAdapter<K,V> implements Declarable {

  public void afterCreate(EntryEvent<K,V> e) {
    System.out.println("    Received afterCreate event for entry: " +
      e.getKey() + ", " + e.getNewValue());
  }
  
  public void afterUpdate(EntryEvent<K,V> e) {
    System.out.println("    Received afterUpdate event for entry: " +
      e.getKey() + ", " + e.getNewValue());
  }
  
  public void afterDestroy(EntryEvent<K,V> e) {
    System.out.println("    Received afterDestroy event for entry: " +
      e.getKey());
  }

  public void afterInvalidate(EntryEvent<K,V> e) {
    System.out.println("    Received afterInvalidate event for entry: " +
      e.getKey());
  }

  public void afterRegionLive(RegionEvent e) {
    System.out.println("    Received afterRegionLive event, sent to durable clients after \nthe server has finished replaying stored events.  ");
  }

  public void init(Properties props) {
    // do nothing
  }

}

Related Javadocs

CacheFactory, Cache, Region, REPLICATE and REPLICATE_PROXY in RegionShortcut

Enter labels to add to this page:
Please wait 
Looking for a label? Just start typing.