Hazelcast Groovyness
Data distribution is a pretty cool topic. Recently, I have been working with Hazelcast, which is an open source clustering and data distribution platform for Java. Well, I really like what I have seen so far and I figured why not have some fun with Hazelcast and Groovy.
I started by adding the Hazelcast 1.7.1 jar to $GROOVY_HOME/lib. Hazelcast, at an introductory level, provides distributed implementations of java.util { Queue, List, Set, Map }. I can run a Groovy script on multiple JVM’s and I can share a Map of customers on each instance. For example:
def customersMap = Hazelcast.getMap("customers")
Now, I have an instance of Map and I can add values using Hazelcast’s distributed id generator:
def idGen = Hazelcast.getIdGenerator("customer-ids")
def id = idGen.newId()
customersMap.put(id, "Customer $id")
So, that was pretty simple, right? Here is the entire Groovy script HazelcastGroovynessAdd.groovy:
import com.hazelcast.core.Hazelcast
import com.hazelcast.core.IdGenerator
def customersMap = Hazelcast.getMap("customers")
def idGen = Hazelcast.getIdGenerator("customer-ids")
def id = idGen.newId()
customersMap.put(id, "Customer $id")
I can open up a few different command prompts and enter:
> groovy HazelcastGroovynessAdd.groovy
Now, the customers Map has a few customers in it and our Groovy scripts are still running. Let’s add an com.hazelcast.core.EntryListener to the customers Map so we can detect a com.hazelcast.core.EntryEvent. Here is HazelcastGroovyness.groovy:
import com.hazelcast.core.Hazelcast
import com.hazelcast.core.EntryListener
import com.hazelcast.core.EntryEvent
def listener = [
entryAdded: { EntryEvent ev ->
println "key $ev.key was added with value $ev.value to $ev.name"
Hazelcast.getMap("customers").values().each {
println it
}
},
entryUpdated: { EntryEvent ev -> },
entryRemoved: { EntryEvent ev -> },
entryEvicted: { EntryEvent ev -> }
] as EntryListener
def customersMap = Hazelcast.getMap("customers")
customersMap.addEntryListener(listener, true)
In the above code, we define listener which implements com.hazelcast.core.EntryListener. I now start up HazelcastGroovyness.groovy at a new command prompt(s):
> groovy HazelcastGroovyness.groovy
We can go back to our original HazelcastGroovynessAdd.groovy script and open (re-open) a few more command prompts and run the script that adds customers to the Map. Now in each running instance of HazelcastGroovyness.groovy we see something like:
key 2000001 was added with value Customer 2000001 to c:customers Customer 2000001 Customer 1000001 Customer 1
Hazelcast is very cool, easy to use technology that provides distributed data with a few lines of code, especially with Groovy. More information can be found at Hazelcast’s website and at the project site at Google Code.
This entry was posted on Thursday, December 10th, 2009 at 12:12 pm and is filed under Development. You can follow any responses to this entry through the RSS 2.0 feed. Both comments and pings are currently closed.
December 10th, 2009 at 2:19 pm
Tweets that mention Hazelcast Groovyness - thejavajar -- Topsy.com says:[...] This post was mentioned on Twitter by HowDo.us, groovyblogs.org. groovyblogs.org said: Hazelcast Groovyness — http://www.thejavajar.com/2009/12/10/hazelcast-groovyness/ — theJavaJar.com [...]