Wednesday, May 26, 2010

Using hazelcast with Jruby

So, i have been doing some reading on Hazelcast as I have been researching a bit on In-Memory Data grids. i spent some time with Gemfire (which is a very cool product) and then found this seemingly very close open source alternative. Its a linear scalable memory cluster that allows you to do a lot of very cool things in memory such as Hashmaps, Queues, Topics, Callbacks, etc. Since i have been toying with building some Java clients in Jruby i thought i would share some really simple examples of how i got up and running calling and interacting with the Hazelcast cluster from a Jruby client API.

To run hazelcast, simply download the latest version, cd into the bin/ directory and run "./run.sh". This should get your cluster up and running. My example uses the ip of 192.168.1.3:5701, you should substitute your own ip here.

Example 1: Basic Read/Write cache

This example simple connects to a hazelcast cluster and then writes 2 entries and then read 2 entries. Certainly not very valuable but shows how easily you could replace a memcache library, for example.

require 'java'
require 'hazelcast-client-1.8.4.jar'
import com.hazelcast.core.Hazelcast;
import com.hazelcast.client.HazelcastClient;
import java.util.Map;
import java.util.Collection;

class MyClass
def initialize
@client = HazelcastClient.newHazelcastClient("dev", "dev-pass", "192.168.1.3:5701");
@map = @client.getMap("default");
end

def write(k,v)
@map.put(k,v)
end

def read(k)
puts "Key: " + k + " Value: " + @map.get(k)
end

end

c = MyClass.new
c.write('key1','value1')
c.write('key2','value2')

c.read('key1')
c.read('key2')


When you run it here is the basic output as exected:

$ jruby test2.rb
Key: key1 Value: value1
Key: key2 Value: value2

Example 2: Enable event callbacks with hazelcast

This one i found a bit more interesting. Here, we register a callback for the "addEvent" method, which basically allows us to "subscribe" to the cluster interface that would give us a callback whenever an entry is added. There are also methods for eviction and updates as well, but kept it simple here for demo purposes. here i demo how when i add a new key on the console, i immediately get a callback in my jruby api script that a new item was added, very powerful stuff here.

require 'java'
require 'hazelcast-client-1.8.4.jar'
import com.hazelcast.core.Hazelcast;
import com.hazelcast.client.HazelcastClient;
import java.util.Map;
import java.util.Collection;

class MyListener
include com.hazelcast.core.EntryListener
include com.hazelcast.core.ItemListener

def initialize
end

def entryAdded(e)
puts "Event found : " + e.getKey + " value = " + e.getValue
end

end

class MyClass

def initialize
@client = HazelcastClient.newHazelcastClient("dev", "dev-pass", "192.168.1.3:5701");
end

def listen
sample = MyListener.new
map = @client.getMap("default")
map.addEntryListener(sample, true);
end

end


c = MyClass.new
c.listen


#######################
Hazelcast Server

hazelcast[default] > m.put 'key2' 'value2'
null
hazelcast[default] >


########################
Output

# jruby test.rb

Event found : 'key2' value = 'value2'