In this topic, you run a simple GemFire data distribution example and then take a look at the example's configuration and application files.
This example shows data distribution between two GemFire Java applications.
- Both applications create a GemFire cache with a GemFire data region in it. Data regions hold and manage data entries, written in key/value pairs.
- The data region in both applications is configured to receive all data written to the region from anywhere in the GemFire system.
- One application is a producer, writing data to the cache, and the other is a consumer, receiving what the producer writes.
- Both applications report their caching activities to your screen. The producer reports on its writing activities. Both applications run a listener that reports on events in the application's cache.
If you have not already done so, install and configure GemFire Enterprise and the product examples:
- Start two terminal sessions with their environments set up for GemFire and the product examples.
- In both sessions, change directory (cd) to the helloworld directory under your examples installation.
- In one terminal session, start the Consumer application:
The application starts, creates the cache and region and then asks you to start the producer:
- In the other terminal session, start the Producer application:
The application starts, creates the cache and region, puts data entry key/values pairs into its cache, and then exits:
The entry key is "Hello" for both puts, but the value changes. For each entry the producer puts, its listener receives the event and reports to your screen. The first put results in an entry creation. The second put results in an entry update, since the key, "Hello", is already in the cache.
- Look at the consumer session:
Only the producer makes changes to its cache, but because of how the data region is configured, GemFire automatically distributes the changes to the consumer, so both caches receive the same events and end up with the same data in their region.
- Press Enter in the Consumer session, and it exits.
The Hello World example use standard GemFire configuration and application files.
These are the files, which you can find under the helloworld directory in your examples installation:
- helloworld/HelloWorldProducer.java and helloworld/HelloWorldConsumer.java
GemFire system configuration file. Use this file to define how the processes in a GemFire system (in this example, two Java applications) find each other and communicate. Also use the file to configure logging, security, statistics gathering, and more. Each member has its own gemfire.properties file.
This example file sets non-default values for:
- Multicast port location used by the applications to establish communication. In this example:
- Level of logging the system should provide. In this example:
Declarative XML configuration for the GemFire cache. Use this file to set up general cache facilities and behavior and to create and initialize cached data regions. Although it can have any name, this file is generally referred to as cache.xml.
- XML version and DOCTYPE declarations (required).
- <cache> element. Highest level element for peer and server caches. These are peer caches. (Client caches are defined by <client-cache>.)
- <region> element. Defines the data region used for the example and specifies all non-default region configuration settings.
- This region is configured as a replicate, meaning that any data either application writes into it is sent automatically to the other application's cache.
- The region also has a listener installed named SimpleCacheListener.
Java applications that use the GemFire distributed caching API to connect to other applications and share data in memory.
The example programs HelloWorldConsumer.java and HelloWorldProducer.java are in GemFire's helloworld examples package of classes. They both use the GemFire Cache, CacheFactory, and Region APIs.
(Not shown: the consumer program also imports a couple of Java APIs for detecting keystrokes.)
When they start, both 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.
The programs get the example region that was declared in the XML file.
The producer puts data into the region and reports its activities to standard output.
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.
GemFire Java application plug-in written to handle cache region events. You can code application plug-ins to respond to a variety of events in the GemFire system. This listener is installed on the region in both applications, through the XML file declarations.
This is the code that is called when the entry is put into the cache.