This was originally forked from github.com/davidgiffin/zookeeper It has heavy modifications

INSTALLING:

MRI

gem build zookeeper.gemspec gem install zookeeper-0.3.gem

JRUBY

jruby -S gem build zookeeper.gemspec
jruby -S gem install zookeeper-0.3-java.gem

(this fork) Event Handling

Both the C and jruby versions expose the same API for watchers. Default is no watcher (:watch => true) will be ignored.

You first register your path with the event handler (if you're using the default).

client = ZooKeeper.new("localhost:2181", :watcher => :default)
client.watcher.register("/mypath") do |event, zk|
  # here the event will be a ZooKeeper::WatcherEvent object and zk
  # will be the original client
  $stderr.puts "got an event on #{event.path}"
end
# then we register a watch
client.exists?("/mypath", :watch => true) #false
client.create("/mypath", "mydata", :mode => :ephemeral)
# the registered block above will fire within a few hundred miliseconds
sleep 0.8

<< got an event on "/mypath"

(this fork) Locking

client1 = ZooKeeper.new("localhost:2181", :watcher => :default)
client2 = ZooKeeper.new("localhost:2181", :watcher => :default)

lock1 = client1.locker("/mypath")
lock1.lock #true

lock2 = client2.locker("/mypath")
lock2.lock #false

lock1.unlock #true
lock2.lock #true

# locks are also released on a client close/crash
lock1.lock #false
client2.close!
lock1.lock #true

(this fork) Message Queue

client1 = ZooKeeper.new("localhost:2181", :watcher => :default)
client2 = ZooKeeper.new("localhost:2181", :watcher => :default)

publisher = client1.queue("myqueue")
receiver = client2.queue("myqueue")

receiver.subscribe do |title, data|
  # data will be whatever was published, title will be the node name
  # for the message

  $stderr.puts "got a message with: #{data}"

  # having a true state from the block will mark the message as 'answered'
  # sending back a false will requeue

  true
end

publisher.publish("my data!")

<< got a message with: my data!

(this fork) Connection Pools

For rails like apps - to do single writes… we can make a pool with checkin and checkout capabilities.

pool = ZooKeeper::ConnectionPool.new("localhost:2181", 10, :watcher => false)
pool.checkout do |client|
  #do stuff with the client
  client.exists?("blah")
end
# client is checked back into the pool
# if there are no clients available - checkout blocks
# but you can override that like so:

connection = pool.checkout(false)
# now if there are no clients available - checkout will simply return
# false

zookeeper

by Shane Mingins
http://github.com/smingins/zookeeper/tree/master

DESCRIPTION:

Ruby Interface to Apache's ZooKeeper zookeeper.wiki.sourceforge.net/

LICENSE:

(The MIT License)

Copyright © 2008 Shane Mingins and Topping Bowers

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.