Search This Blog

Wednesday 9 November 2011

Accessing GemFire Regions from JRuby

Here is a quick demo showing how easy it is to access GemFire regions from JRuby clients. It's assumed you have cache servers started and in this example the cache servers and JRuby client use a locator for connection. It's worth noting the JRuby client is setup as a PROXY client with no client side cache.

Our cache server nodes use a config file as follows - server.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.6//EN" 
"http://www.gemstone.com/dtd/cache6_6.dtd">
<cache>
  <disk-store name="ds1" auto-compact="true" max-oplog-size="1024" queue-size="10000" time-interval="15">
 <disk-dirs>
  <disk-dir dir-size="4096">persistData</disk-dir>
 </disk-dirs>
  </disk-store>
  <region name="AllObjectRegion"> 
    <region-attributes refid="PARTITION_PERSISTENT" disk-store-name="ds1">
      <partition-attributes redundant-copies="1" />
      <eviction-attributes>
        <lru-heap-percentage action="overflow-to-disk" />
      </eviction-attributes>
    </region-attributes>
    <index name="ownerIdx">
      <functional from-clause="/AllObjectRegion" expression="owner"/>
    </index>
  </region>
  <function-service>
 <function>
   <class-name>vmware.au.se.demo.SizeFunction</class-name>
 </function>
  </function-service> 
  <resource-manager critical-heap-percentage="75" eviction-heap-percentage="65"/>
</cache>

Our JRuby client uses a client config as follows - client.xml
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE client-cache PUBLIC
    "-//GemStone Systems, Inc.//GemFire Declarative Caching 6.6//EN" 
"http://www.gemstone.com/dtd/cache6_6.dtd"> 
<client-cache>
 <pool name="client" subscription-enabled="true">
     <locator host="localhost" port="41111"/>
 </pool>
 <!-- No cache storage in the client region because of the PROXY client region shortcut setting. -->
    <region name="AllObjectRegion">
  <region-attributes refid="PROXY" />
    </region>
    <resource-manager critical-heap-percentage="75" eviction-heap-percentage="65"/>
</client-cache> 

Our JRuby code is as follows - gemfire-caching-proxy-client.rb
require 'java'
require File.dirname(__FILE__) + '/lib/gemfire.jar'
require File.dirname(__FILE__) + '/lib/antlr.jar'
require File.dirname(__FILE__) + '/lib/gemfire-quickstart.jar'

import java.util.Collection;
import java.util.Iterator;

import "vmware.au.se.demo.domain.AllDBObject";

import com.gemstone.gemfire.cache.Region;
import com.gemstone.gemfire.cache.client.ClientCache;
import com.gemstone.gemfire.cache.client.ClientCacheFactory;
import com.gemstone.gemfire.cache.query.Query;
import com.gemstone.gemfire.cache.query.QueryService;
import com.gemstone.gemfire.cache.query.SelectResults;

puts "*********************************************************"
puts "GemFire 6.6 Proxy Client Example from JRUBY"
puts "*********************************************************"

print "Started at ", Time.now, "\n"

begin

 ccf = ClientCacheFactory.new
 ccf.set("cache-xml-file", "client.xml")
 cache = ccf.create
 
 allObjectRegion = cache.getRegion("AllObjectRegion")
 queryService = cache.getQueryService
 
 query = queryService.newQuery("SELECT * FROM /AllObjectRegion where owner = 'SCOTT'")
 print "\n** All OBJECTS with owner = 'SCOTT'\n"
 
 result = query.execute
 collection = result.asList
 iter = collection.iterator
 i = 0
 
 while iter.hasNext
   i = i + 1
   entry = iter.next
   print "Entry ", i , " : " , entry , " \n"
   
 end
 
    cache.close
    
rescue 
  print "\n** Error occured **\n"
  print "Failed to obtian data from gemfire region ", $!, "\n\n"
  
end

print "\nEnded at ", Time.now, "\n"

Output as follows

Note: Some of the gemfire output is omitted to make it a little more readable.

Pas-Apicellas-MacBook-Pro:quickstart-client papicella$ jruby gemfire-caching-proxy-client.rb 
*********************************************************
GemFire 6.6 Proxy Client Example from JRUBY
*********************************************************
Started at Wed Nov 09 12:10:29 +1100 2011

[info 2011/11/09 12:10:29.319 EST <main> tid=0x1] 
  ---------------------------------------------------------------------------
  
....

  udp-recv-buffer-size="1048576"
  udp-send-buffer-size="65535"
  writable-working-dir=""
  

[info 2011/11/09 12:10:29.335 EST <main> tid=0x1] Running in local mode since mcast-port was 0 and locators was empty.

[info 2011/11/09 12:10:29.392 EST <Thread-1 StatSampler> tid=0xf] Disabling statistic archival.

[info 2011/11/09 12:10:29.645 EST <poolTimer-client-2> tid=0x14] AutoConnectionSource discovered new locators [/10.117.85.62:41111]

[config 2011/11/09 12:10:29.646 EST <poolTimer-client-3> tid=0x15] Updating membership port.  Port changed from 0 to 50,522.

[config 2011/11/09 12:10:29.669 EST <main> tid=0x1] Pool client started with multiuser-authentication=false

[info 2011/11/09 12:10:29.671 EST <main> tid=0x1] Overridding MemoryPoolMXBean heap threshold bytes 0 on pool CMS Old Gen with 344,064,000

[info 2011/11/09 12:10:29.673 EST <main> tid=0x1] Overridding MemoryPoolMXBean heap threshold bytes 344,064,000 on pool CMS Old Gen with 344,064,000

[info 2011/11/09 12:10:29.673 EST <Cache Client Updater Thread  on Pas-Apicellas-MacBook-Pro(2183)<v2>:37232/49977> tid=0x16] Cache Client Updater Thread  on Pas-Apicellas-MacBook-Pro(2183)<v2>:37232/49977 (10.117.85.62:49987) : ready to process messages.

[config 2011/11/09 12:10:29.753 EST <main> tid=0x1] Cache initialized using "jar:file:/Users/papicella/vmware/scripting/demos/jruby/gemfire/quickstart-client/./lib/gemfire-quickstart.jar!/client.xml".

** All OBJECTS with owner = 'SCOTT'
Entry 1 : AllDBObject [owner=SCOTT, objectName=EMP, objectId=74211, objectType=TABLE, status=VALID] 
Entry 2 : AllDBObject [owner=SCOTT, objectName=PK_EMP, objectId=74212, objectType=INDEX, status=VALID] 
Entry 3 : AllDBObject [owner=SCOTT, objectName=JDBC_BATCH_TABLE, objectId=75753, objectType=TABLE, status=VALID] 
Entry 4 : AllDBObject [owner=SCOTT, objectName=PK_DEPT, objectId=74210, objectType=INDEX, status=VALID] 
Entry 5 : AllDBObject [owner=SCOTT, objectName=BONUS, objectId=74213, objectType=TABLE, status=VALID] 
Entry 6 : AllDBObject [owner=SCOTT, objectName=SALGRADE, objectId=74214, objectType=TABLE, status=VALID] 
Entry 7 : AllDBObject [owner=SCOTT, objectName=DEPT, objectId=74209, objectType=TABLE, status=VALID] 

[info 2011/11/09 12:10:29.962 EST <main> tid=0x1] GemFireCache[id = 1691463635; isClosing = true; created = Wed Nov 09 12:10:29 EST 2011; server = false; copyOnRead = false; lockLease = 120; lockTimeout = 60]: Now closing.

[info 2011/11/09 12:10:29.986 EST <main> tid=0x1] Resetting original MemoryPoolMXBean heap threshold bytes 0 on pool CMS Old Gen

[config 2011/11/09 12:10:30.015 EST <main> tid=0x1] Destroying connection pool client

Ended at Wed Nov 09 12:10:30 +1100 2011  

No comments: