Created
February 25, 2017 03:45
-
-
Save eoconnell/6cb8f87f9df507d27e3c20731f9f661c to your computer and use it in GitHub Desktop.
Use Groovy Meta Object Programming to access fields on Geode PdxInstance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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