Bootstrapping GemFire through the Spring container One of the earlier tasks when using GemFire ad Spring framework is configuring the data grid through the IoC container. While this is possible out of the box, the configuration tends to be verbose and address only the basic cases. To address this problem, the Spring/GemFire project provides several classes that allow configuration of distributed caches or regions in a variaty of scenarios with minimal effort.
Configuring the GemFire <interfacename>Cache</interfacename> In order to use the GemFire Fabric, one needs to either create a new Cache or connect to an existing one. As in the current version of GemFire, there can be only one opened cache per VM (or classloader to be technically correct), in most cases the cache is created once and then all other consumers connect to it. In its simplest form, a cache can be defined in one line: ]]> Here, the default-cache, using the defaults, will try to connect to an existing cache and, in case one does not exist, create it. Especially in environments with opened caches, this basic configuration can go a long way. For scenarios where the cache needs to be configured, the user can pass in a GemFire configuration: ]]> In this example, if the cache needs to be created, it will use the file named cache.xml located in the classpath root. Note that the configuration makes use of Spring's Resource abstraction to locate the file. This allows various search patterns to be used, depending on the running environment or the prefix specified (if any) by the value. Additionally to an external configuration, one can specify GemFire settings directly through Java Properties. This can be quite handy when just a certain setting or default needs to be changed: 127.0.0.1 ]]> So far our examples relied on the primary Spring names (beans). However one is free to add other namespaces to simplify or enhance the configuration. Let's do the same thing to the configuration above by using the util namespace and externalize the properties from the configuration (a best practice). ]]> It is worth pointing out again, that the cache settings apply only if the cache needs to be created, there is no opened cache in existence otherwise the existing cache will be used (and the configuration will simply be discarded).
Configuring a GemFire <interfacename>Region</interfacename> Once the Cache is configured, one needs to configure one or more Regions for interacting with the data fabric. In a similar manner to the CacheFactoryBean, the RegionFactoryBean allows existing Regions to retrieved or, in case they don't exist, created using various settings. One can specify the Region name, whether it will be destroyed on shutdown (acting as a temporary cache), the associated CacheLoaders, CacheListeners and CacheWriters and if needed, the RegionAttributes for full customization. Let us start with a simple region declaration, named basic using a nested cache declaration: ]]> Since the region bean definition name is usually the same with that of the cache, the name property can be omitted (the bean name will be used automatically). Additionally by using the name the p namespace, the configuration can be simplified even more: ]]> It is worth pointing out, that for the vast majority of cases configuring the cache loader, listener and writer through the Spring container is preferred since the same instances can be reused across multiple regions and additionally, the instances themselves can benefit from the container rich feature set: ]]>
Configuring a <emphasis>client</emphasis> <interfacename>Region</interfacename> For scenarios where a CacheServer is used and clients need to be configured, SGI offers a dedicated configuration class named: ClientRegionFactoryBean. This allows client interests to be registered in both key and regex form through Interest and RegexInterest classes in the org.springframework.data.gemfire package: ]]>