HelloWorldProducer.java and helloworld/HelloWorldConsumer.java

HelloWorldProducer.java and helloworld/HelloWorldConsumer.java

These example programs are Java applications that use the GemFire distributed caching API to connect to other applications and share data in memory.

The files are in the GemFire helloworld examples package of classes. They both use the GemFire Cache, CacheFactory, and Region APIs.

Example Programs Common Package and Import Declarations

In addition to the packages shown, the consumer program imports a couple of Java APIs for detecting keystrokes.

package helloworld;

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

Creating Caches and Connecting to Each Other

The HelloWorldProducer and HelloWorldConsumer programs create their cache, specifying the XML file to use. The cache creation automatically creates the distributed system using the gemfire.properties file settings. The distributed system is what connects the two programs to each other.

Cache cache = new CacheFactory()
      .set("cache-xml-file", "xml/HelloWorld.xml")

Getting the Data Region

The programs get the example region that was declared in the XML file.

Region<String,String> exampleRegion = cache.getRegion("exampleRegion");

Working in the Cache

The producer puts data into the region and reports its activities to standard output.

   System.out.println("Putting entry: Hello, World");
    exampleRegion.put("Hello", "World");
    System.out.println("Putting entry: Hello, Moon!");
    exampleRegion.put("Hello", "Moon!");

Because of how their data regions are configured in the XML file, the data put by the producer is automatically pushed to the consumer's cache.

The consumer application code does nothing with the data, but its region's listener handles the data events.

Closing the Cache and Disconnecting

The applications close their cache and disconnect from the distributed system.