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 Cache
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).
]]>
Configuring a GemFire Region
Once the Cache is configured, one needs to configure one or more Regions for interacting with the distributed fabric.