JBrisbin.com

ZooKeeper Helper Plugin for Grails

07
Oct

ZooKeeper Helper Plugin for Grails

Since I'm using ZooKeeper to coordinate my Groovy-based Map/Reduce framework, I've written a helper class that should be able to be used by any Groovy code to make interacting with ZooKeeper a little more Groovylicious. Here's an example of how to make working with ZooKeeper easier within a Grails app:

class MyController {

  // Auto-injected by the plugin
  def zooKeeper

  def action = {
    zooKeeper.onDataChanged = { evt ->
      // We're interested in data changing while we're watching
      println "data changed: ${evt}"
    }
    if (!zooKeeper.exists("/my/node/parent")) {
      zooKeeper.createPersistentNodeAndParents("/my/node/parent")
    }
    node = zooKeeper.createPersistentNode("/my/node/parent/child")
    node.data = 12345
  }

}

The thing I like about this versus using the API directly is that I'm using Closures everywhere I can. I've implemented some core Watchers that call the closures you set on the "zooKeeper" object. All methods also take either a plain String or GString, so you can use the full power of Groovy strings (including embedding variable references for building up paths).

There is a small wrapper class for dealing with a Node directly, rather than by calling getData() and setData() (though you're certainly free to call those methods directly).

Automatic serialization and deserialization occur when setting and getting data. You don't need to worry about it yourself. Any Serializable class can be set as the data on a ZooKeeper node.

The code, as always, is Apache 2.0 licensed and available on GitHub:

http://github.com/jbrisbin/grails-zookeeper-plugin

blog comments powered by Disqus