Thursday, 16 December 2010

Access Coherence Cache from JRuby as an Extend client

In my previous example below I wanted to quickly join a coherence cluster from JRuby and basically did it the easiest way possible by simply becoming a member in the cluster.

Access Coherence Cache from JRuby/Jython scripts

In the example below we will we use JRuby as a Coherence*Extend client using POF (Portable Object Format) and we do this for the following reasons.

1. Being a console application we want to connect/disconnect many times a day and even though we are a storage disabled member it results in overhead which an extend client avoids.
2. Being an extend client we are storage disabled by default.
3. Using POF has many advantages ranging from performance benefits to language independence, although in this example we are using it from a Java enabled client.

So our JRuby code is now as follows.

require 'java'
require 'C:/jdev/scripting/demos/jruby/extendclient-coherence/extenddemo.jar'
require 'C:/jdev/coherence/36/coherence/lib/coherence.jar'

include_class ""
import java.util.Date
import java.lang.System
import java.math.BigDecimal

puts "***********************************************"
puts "Coherence 3.6 Extend Client Example from JRUBY"
puts "***********************************************"

print "Started at ",, "\n"


  # setup required properties to connect to proxy server as extend client
  System.setProperty("tangosol.coherence.cacheconfig", "client-cache-config.xml")
  System.setProperty("tangosol.pof.enabled", "true")
  System.setProperty("tangosol.pof.config", "extend-pof-config.xml")
  System.setProperty("", "")

  # get named cache alldbobjs
  alldbobjs = CacheFactory.getCache("alldbobjs")

  #retrieve size of cache
  print "\nCache [alldbobjs] size  = " + alldbobjs.size().to_s + "\n\n"

  #retrieve one record
  objectid =

  objectrecord =
  objectrecord = alldbobjs.get(objectid)

  puts "Record 54 = " + objectrecord.to_s

  print "\n** Error occured **\n"
        print "Failed to access Coherence Cluster from proxy server ", $!, "\n\n"


print "Ended at ",, "\n"
From the code we have done the following.

1. Used a client cache config file to connect as an extend client, basically connect to an extend proxy which is a cluster member.

2. We provide a client JAR file which contains our domain objects and config files that being extenddemo.jar

3. The client cache config is defined as follows.
<!DOCTYPE cache-config SYSTEM "cache-config.dtd">

              <address system-property="">
              <port system-property="proxy.port">

So when we run this we get output as follows.

C:\jdev\scripting\demos\jruby\extendclient-coherence>jrb coh-extend-client.rb
Coherence 3.6 Extend Client Example from JRUBY
Started at Thu Dec 16 14:08:06 EST 2010
2010-12-16 14:08:06.114/1.033 Oracle Coherence (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:
2010-12-16 14:08:06.118/1.037 Oracle Coherence (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/jde
2010-12-16 14:08:06.118/1.037 Oracle Coherence (thread=main, member=n/a): Optional configuration override "/tangosol-coherence-
override.xml" is not specified
2010-12-16 14:08:06.121/1.040 Oracle Coherence (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml"
is not specified

Oracle Coherence Version Build 17229
 Grid Edition: Development mode
Copyright (c) 2000, 2010, Oracle and/or its affiliates. All rights reserved.

2010-12-16 14:08:06.304/1.223 Oracle Coherence GE (thread=main, member=n/a): Loaded cache configuration from "jar:file:/C:/jd
2010-12-16 14:08:06.472/1.391 Oracle Coherence GE (thread=RemoteCache:TcpInitiator, member=n/a): Loaded POF configuration fro
m "jar:file:/C:/jdev/scripting/demos/jruby/extendclient-coherence/extenddemo.jar!/extend-pof-config.xml"
2010-12-16 14:08:06.475/1.394 Oracle Coherence GE (thread=RemoteCache:TcpInitiator, member=n/a): Loaded included POF configur
ation from "jar:file:/C:/jdev/coherence/36/coherence/lib/coherence.jar!/coherence-pof-config.xml"
2010-12-16 14:08:06.567/1.486 Oracle Coherence GE (thread=RemoteCache:TcpInitiator, member=n/a): Started: TcpInitiator{Name=Rem
oteCache:TcpInitiator, State=(SERVICE_STARTED), ThreadCount=0, Codec=Codec(Format=POF), PingInterval=0, PingTimeout=0, RequestTimeout=0, Con
nectTimeout=0, SocketProvider=SystemSocketProvider, RemoteAddresses=[]}
2010-12-16 14:08:06.572/1.491 Oracle Coherence GE (thread=main, member=n/a): Opening Socket connection to
2010-12-16 14:08:06.580/1.499 Oracle Coherence GE (thread=main, member=n/a): Connected to

Cache [alldbobjs] size  = 99927

Record 54 = AllDbObject - owner: SYS ,objectName: I_CDEF2 ,subObjectName: null ,objectId: 54 ,dataObjectId: 54 ,objectType: INDEX ,created:
2009-08-15 ,lastDDLTime: 2009-08-15 ,timestamp: 2009-08-15:00:16:51 ,status: VALID ,temporrary: N ,generated: N ,secondary: N ,namespace: 4
,editionName: null

Ended at Thu Dec 16 14:08:06 EST 2010

No comments: