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'

10 comments:

Anonymous said...

i want more posts!!!

solutionhacker said...

very nice! It saved me time to dig out how to write a hazel client

Chandrika Madapudi said...

Hi, This is Chandrika from Chennai. I have read your blog and I got some knowledgeable information through this blog. Really useful blog. Keep update your blog.

Regards...
Java Training in Chennai

for IT the said...

Great Article
Java Online Training | Java EE course

Java Training in Chennai | J2EE Training in Chennai | java j2ee training institutes in chennai ~ Java Course in Chennai | Java Training Institutes in Chennai

Java 360 | IT Technical Articles |Java Training Institutes

Addison adolf said...

Nice article!!!

Contact us for Android Application Software

Addison adolf said...

Ivr solutions for your Business for call recording or forwarding and arrange the caller.....

John Alert said...

Java Training Institutes Java Training Institutes
Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

John Alert said...

Java Training Institutes Java Training Institutes
Java Spring Hibernate Training Institutes in Chennai J2EE Training Institutes in Chennai J2EE Training Institutes in Chennai Java Spring Hibernate Training Institutes in Chennai | Hibernate Training Institutes in Chennai Core Java Training Institutes in Chennai Core Java Training Institutes in Chennai

John Alert said...

Hibernate Online Training Hibernate Online Training Hibernate Training in Chennai Hibernate Training in Chennai Java Online Training Java Online Training Hibernate Training Institutes in ChennaiHibernate Training Institutes in Chennai

John Alert said...

JMS Training Institutes in Chennai JMS Training Institutes in Chennai | JSP Training Institutes in Chennai | Spring Training Institutes in Chennai Spring Training Institutes in ChennaiMicroServices Training Institutes In Chennai Java MicroServices Training Institutes In Chennai
Java EE Training Institutes in Chennai Java EE Training Institutes in Chennai