GemFire Plugin - Reference Documentation
Authors: Jeff Brown, Graeme Rocher
Version: 1.0.0.BUILD-SNAPSHOT
Table of Contents
1 Introduction To The GemFire Plugin
The GemFire plugin provides integration with the GemFire in-memory distributed data management platform.This user guide describes details on configuring and using GemFire specifically in the context of a Grails application. Complete documentation on GemFire is available at http://www.gemstone.com/products/gemfire.1.1 Installing Gemfire
Installing GemFire
If you plan to use Gemfire only in peer-2-peer mode then you can skip this installation. If you want to use Gemfire in client/server mode then follow these instructions to install Gemfire:- Register at the SpringSource GemFire download site to access the download.
- Install GemFire Enterprise 6.5 according to the instructions on the download site.
Configure your environment for GemFire.
- Set the
JAVA_HOME
environment variable to point to your Java runtime installation. (There should be a bin directory underJAVA_HOME
.) - Set the
GEMFIRE
environment variable to point to your GemFire installation top-level directory. (There should be bin, lib, dtd, and other directories under GEMFIRE.) - Configure
GF_JAVA
and yourPATH
andCLASSPATH
as shown in these examples. (GF_JAVA
must point to the java executable file under yourJAVA_HOME
.)
Unix Bourne and Korn shells (sh, ksh, bash)
GF_JAVA=$JAVA_HOME/bin/java;export GF_JAVA PATH=$PATH:$JAVA_HOME/bin:$GEMFIRE/bin;export PATH CLASSPATH=$GEMFIRE/lib/gemfire.jar:$GEMFIRE/lib/antlr.jar:$GEMFIRE/lib/gfSecurityImpl.jar:$CLASSPATH;export CLASSPATH
Windows
set GF_JAVA=%JAVA_HOME%binjava.exe set PATH=%PATH%;%JAVA_HOME%bin;%GEMFIRE%bin set CLASSPATH=%GEMFIRE%libgemfire.jar;%GEMFIRE%libantlr.jar;%GEMFIRE%libgfSecurityImpl.jar;%CLASSPATH%
2 Cache Regions
GemFire allows your data to be organized within a cache using data regions. The Grails GemFire plugin provides a DSL for describing the regions available to the application and provides a simple convention based approach to accessing regions.2.1 Cache Region Configuration
GemFire regions may be described inConfig.groovy
by assigning a value to
the grails.gemfire.regions
property. The value should be a closure which
contains GemFire Region DSL code. Details about the DSL are described below.The code below declares 2 regions with the names region1
and region2
.// grails-app/conf/Config.groovy grails.gemfire.regions = { // declare region1 region1() // declare region2 region2() }
// grails-app/conf/Config.groovyimport com.gemstone.gemfire.cache.DataPolicygrails.gemfire.regions = { region1 { // configure region1… dataPolicy = DataPolicy.REPLICATE publisher = false } region2 { // configure region2… dataPolicy = DataPolicy.PARTITION }}
DataPolicy
. The previous
example could be written like this:// grails-app/conf/Config.groovygrails.gemfire.regions = { region1 {
// configure region1…
dataPolicy = REPLICATE
publisher = false
} region2 {
// configure region2…
dataPolicy = PARTITION
}}
Class | Property Name |
---|---|
com.gemstone.gemfire.cache.DataPolicy | EMPTY |
com.gemstone.gemfire.cache.DataPolicy | NORMAL |
com.gemstone.gemfire.cache.DataPolicy | PARTITION |
com.gemstone.gemfire.cache.DataPolicy | PERSISTENT_REPLICATE |
com.gemstone.gemfire.cache.DataPolicy | PRELOADED |
com.gemstone.gemfire.cache.DataPolicy | REPLICATE |
com.gemstone.gemfire.cache.ExpirationAction | DESTROY |
com.gemstone.gemfire.cache.ExpirationAction | INVALIDATE |
com.gemstone.gemfire.cache.ExpirationAction | LOCAL_DESTROY |
com.gemstone.gemfire.cache.ExpirationAction | LOCAL_INVALIDATE |
com.gemstone.gemfire.cache.Scope | DISTRIBUTED_ACK |
com.gemstone.gemfire.cache.Scope | DISTRIBUTED_NO_ACK |
com.gemstone.gemfire.cache.Scope | GLOBAL |
com.gemstone.gemfire.cache.Scope | LOCAL |
regionTimeToLive
, regionIdleTimeout
, entryTimeToLive
and entryIdleTimeout
. Configuring those
properties might look something like this:// grails-app/conf/Config.groovy import com.gemstone.gemfire.cache.ExpirationAction import com.gemstone.gemfire.cache.ExpirationAttributesgrails.gemfire.regions = { region1 { entryTimeToLive = new ExpirationAttributes(120) entryTimeToIdle = new ExpirationAttributes(200, ExpirationAction.DESTROY) }}
ExpirationAction
and ExpirationAttributes
classes
to be removed.// grails-app/conf/Config.groovygrails.gemfire.regions = { region1 { entryTimeToLive = expirationAttributes(120) entryTimeToIdle = expirationAttributes(200, DESTROY) }}
2.2 Accesing Cache Regions
For each configured cache region a bean is added to the Spring application context with a corresponding name. Those beans are the simplest way to interact with the cache region. The bean may be treated as a map of key value pairs.If a region were configured like this:// grails-app/conf/Config.groovygrails.gemfire.regions = { departmentData { entryTimeToLive = expirationAttributes(120) }}
// grails-app/controllers/com/demo/ReportingController.groovypackage com.democlass ReportingController { def departmentData def index = {
def hrData = departmentData['hr']
def accountData = departmentData['accounting'] // …
} def addToCache = {
def key = params.key
def value = params.value
departmentData[key] = value redirect action: 'list'
} // …
}
3 Cache Servers
Gemfire supports a client / server mode for caching which can unlock some of the nicer Gemfire features including:- Grid computing
- Continuous querying
- Advanced data partitioning
start-cache-server
command.grails start-cache-server server1
stop-cache-server
command:grails stop-cache-server server1
grails start-cache-server server1 --locators=localhost[4111] --port=41112 --mcast-port=0
start-locator
command:grails start-locator
grails start-locator --port=4111
3.1 Cache Server Clients
Once you have some cache servers up and running you need to configure the Grails application to act as a client to the servers.// grails-app/conf/Config.groovy grails.gemfire.servers = { myServer { properties = ['log-level': 'warning'] pool { addServer "localhost", 4111 setMinConnections 1 setMaxConnections 20 setRetryAttempts 10 setSubscriptionEnabled true } } }
// grails-app/conf/Config.groovy grails.gemfire.servers = { myServer { properties = ['log-level': 'warning'] pool { addLocator "localhost", 4111 setMinConnections 1 setMaxConnections 20 setRetryAttempts 10 setSubscriptionEnabled true } } }
subscriptionEnabled
is set to true on the pool. For all the configuration options see the PoolFactory API in the Gemfire documentation