Skip to content

Instantly share code, notes, and snippets.

@eoconnell
Created February 25, 2017 03:45
Show Gist options
  • Save eoconnell/6cb8f87f9df507d27e3c20731f9f661c to your computer and use it in GitHub Desktop.
Save eoconnell/6cb8f87f9df507d27e3c20731f9f661c to your computer and use it in GitHub Desktop.
Use Groovy Meta Object Programming to access fields on Geode PdxInstance
apply plugin: 'groovy'
repositories {
jcenter()
}
dependencies {
compile 'org.codehaus.groovy:groovy:2.4.8'
compile 'org.apache.geode:geode-core:1.1.0'
}
task runScript (dependsOn: 'classes', type: JavaExec) {
main = 'main'
classpath = sourceSets.main.runtimeClasspath
}
import org.apache.geode.cache.*
import org.apache.geode.pdx.*
class Foo {
String bar, baz, qux
}
class DataBag implements GroovyInterceptable {
PdxInstance pdx
void setProperty(String field, value) {
println "Set ${field} to ${value}"
}
def getProperty(String field) {
pdx.getField(field)
}
}
def cache = new CacheFactory().create()
PdxInstance instance = cache.createPdxInstanceFactory("Foo")
.writeString("bar", "1")
.writeString("baz", "2")
.writeString("qux", "3")
.create()
def o = new DataBag(pdx: instance)
println o.bar // prints 1
println o.baz // prints 2
println o.qux // prints 3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment