Yesterday I did an exploration of the Apache ZooKeeeper client library.
It turns out that Netflix created a wrapper around this client library to make it more fluent. The project is called Curator and it is now an Apache Project
I thought it may be fun to refactor yesterday’s challenge using Curator. In the process, I learned something really cool about Scala traits.
As you recall from yesterday, we defined a ZooKeeperHelperInterface Trait which defined our interactions with the ZooKeeper server.
It was a cinch to create a new class that extends that Trait and uses the fluent Curator library instead. Here it is:
But now here’s the really cool part. I was able to turn my basic “runner” code that does the ZooKeeper interactions into a method, and then just pass in a client param that extends the ZooKeeperHelperInterface. Check this out:
Which enable this:
The runZkInteractions method doesn’t care which instance gets passed in, only that it extend the ZooKeeperHelperInterface.
This ‘plugin’ style of inerface coding is common in Object-Oriented Design and it is really cool to see it in action.
Also, at these “hello world” level of ZooKeeper interactions, the Curator library doesn’t really shine. Its on the more advanced interactions where it really comes into its own. I will show more of that soon.