Persisting Data to Disk Example

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

Persistence creates a complete backup of the region data on disk, which gives you a highly available copy of your data. At region creation time, if the disk files are already present, the data is recovered from disk and used to initialize the region. Read more about persistence.

Running the Example

In this example, region data is persisted to the subdirectory persistData1. You run the example twice so you can see how the persisted data is loaded into the region on the second run.

To run this example, you must have terminal sessions configured for the QuickStart examples, as described in Setting Up the Environment.
  1. In a single session, run the example:
    $ java quickstart.DataPersistence
    

    No data is in the region on the first run, so the get operation returns null and the program reports that no entry was found. Then the program puts the entry with value, 'value1'.

  2. When the program exits, look in the persistData1 directory. You will see the data files used for persistence.
  3. Run the program again with the same command.
    The second time, at creation, the region is initialized from disk, so the get operation returns 'value1'.

Example Listings

Program and cache configuration file listings:

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
<?xml version="1.0"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.5//EN"
    "http://www.gemstone.com/dtd/cache6_5.dtd">

<!--
  | DataPersistence.xml
  |
  | Configures a region to persist its data to disk.  
  | The data files are written to the directories specified
  | in the disk-dirs elements. 
 -->
<cache>
    <disk-store name="ds1" auto-compact="true" max-oplog-size="1024" queue-size="10000" time-interval="15">
	<disk-dirs>
		<disk-dir dir-size="4096">persistData1</disk-dir>
	</disk-dirs>
    </disk-store>

    <region name="exampleRegion">
	<region-attributes refid="REPLICATE_PERSISTENT" disk-store-name="ds1" disk-synchronous="false">
		<!-- Write buffer data to disk every 10Kb or 15 milliseconds,
                     whichever comes first -->
	</region-attributes>
    </region>
</cache>
Error formatting macro: card: java.lang.NoClassDefFoundError: net/customware/confluence/plugin/composition/CompositionUtil
package quickstart;

import java.io.File;

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

/**
 * This example shows the persistence of cached data to disk. Persisted data provides a backup of the cached data and
 * can be used to initialize a data region at creation time. Please refer to the quickstart guide for instructions on
 * how to run this example.
 *
 * @author GemStone Systems, Inc.
 * @since 4.1.1
 */
public class DataPersistence {

	public static void main(String[] args) throws Exception {

		System.out.println("This example shows persistence of region data to disk.");

		// create the directory where data is going to be stored
		File dir = new File("persistData1");
		dir.mkdir();

		System.out.println();
		System.out.println("Connecting 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", "DataPersistence")
                  .set("cache-xml-file", "xml/DataPersistence.xml")
                  .create();

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

		// Get persistDir from attributes of exampleRegion
		String diskStoreName = exampleRegion.getAttributes().getDiskStoreName();
		DiskStore ds1 = cache.findDiskStore(diskStoreName);
		if (ds1 == null) {
		  return;
		}
		File[] persistDir = ds1.getDiskDirs();
		String persistDirString = "";
		for (int i = 0; i < persistDir.length; i++) {
			if (i > 0) {
				persistDirString += ", ";
			}
			persistDirString += persistDir[i];
		}

		System.out.println();
		System.out.println("Look in " + persistDirString + " to see the files used for region ");
		System.out.println("persistence.");

		// Try to obtain the value for the "key"
		String key = "key1";
		System.out.println();
		System.out.println("Getting value for " + key);
		String value = exampleRegion.get(key);

		if (value == null) {
			System.out.println("No value found for key " + key + ". Get operation returned null.");
			// Create initial value to put...
			value = "value1";
		} else {
			System.out.println("Get returned value: " + value);
			// Increment value to update...
			value = "value" + (Integer.parseInt(value.substring(5)) + 1);
		}

		System.out.println();
		System.out.println("Putting entry: " + key + ", " + value);
		exampleRegion.put(key, value);

		// Close the cache and disconnect from GemFire distributed system
		System.out.println();
		System.out.println("Closing the cache and disconnecting.");
		cache.close();

		System.out.println();
		System.out.println("Each time you run this program, if the disk files are available, they");
		System.out.println("will be used to initialize the region.");
	}
}

Related Javadocs

DiskStoreFactory, DiskStore, options with PERSISTENT in RegionShortcut

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