Search This Blog

Monday 6 December 2010

Access Coherence Cache from JRuby/Jython scripts

The following example show how to use JRuby or Jython to access Coherence caches. Although this is basic and simply using the cache config file out of coherence.jar it gives you enough to see how simple it is to join the cluster as member and access coherence.jar from these 2 scripting languages.

I could of easily used groovy here as well but decided to stick with jruby and jython for now. Also the script members that join the cluster are storage disabled meaning that they must connect to node which is storage enabled to add data to the cache.

For this demo I have the following installed.
  • Coherence 3.6
  • JDK 1.6
  • JRuby 1.5.6
  • Jython 2.5.1
PRE STEPS

1. Start a cache server as follows. We do this to ensure we have a storage enabled node. The cache config file being used is the one inside coherence.jar. 

C:\jdev\coherence\36\coherence\bin>cache-server.cmd

In the example below jy = jython and jrb = jruby executables.

JRUBY

1. Create a ruby script as follows.
require 'java'
require 'C:/jdev/coherence/36/coherence/lib/coherence.jar'

import com.tangosol.net.CacheFactory
import com.tangosol.net.NamedCache
import java.util.Date
import java.lang.System

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

print "Started at ", Date.new.toString, "\n"

begin

  # ensure we are storage disabled
  System.setProperty("tangosol.coherence.distributed.localstorage", "false")
  
  CacheFactory.ensureCluster

  # get named cache
  testcache = CacheFactory.getCache("Test")

  # add data to cache
  testcache.put("1", "pas apicella")

  # retrive data from cache
  puts "Cache entry 1 = " + testcache.get("1")
  
rescue 
 print "Failed to access Coherence Cluster ", $!, "\n"
 
ensure 
  CacheFactory.shutdown
  
end

print "Ended at ", Date.new.toString, "\n"

2. Run it as follows ensuring your reference coherence.jar correctly

> jrb coherence.rb

Output as follows


C:\jdev\scripting\demos\jruby>jrb coherence.rb
*********************************
Coherence 3.6 Example from JRUBY
*********************************
Started at Mon Dec 06 11:38:09 EST 2010
2010-12-06 11:38:09.939/0.855 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:
/jdev/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence.xml"
2010-12-06 11:38:09.942/0.858 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/jde
v/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
2010-12-06 11:38:09.943/0.859 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/tangosol-coherence-
override.xml" is not specified
2010-12-06 11:38:09.946/0.862 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml"
is not specified


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


2010-12-06 11:38:10.474/1.390 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): TCMP bound to /10.187.114.243:8090 using SystemSoc
ketProvider
2010-12-06 11:38:11.683/2.600 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): This Member(Id=2, Timestamp=2010-12-06 11:38:
11.675, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:3824, Role=JrubyMain, Edition=Grid Edition, Mode=De
velopment, CpuCount=4, SocketCount=4) joined cluster "cluster:0xC4DB" with senior Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10
.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, Role=CoherenceServer, Edition=Grid Edition, Mode=Development, C
puCount=4, SocketCount=4)
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Cluster with senior mem
ber 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Management with senior
member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service DistributedCache with s
enior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service ReplicatedCache with se
nior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service OptimisticCache with se
nior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service InvocationService with
senior member 1
2010-12-06 11:38:11.697/2.613 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): Started cluster Name=cluster:0xC4DB


Group{Address=224.3.6.0, Port=36000, TTL=4}


MasterMemberSet
  (
  ThisMember=Member(Id=2, Timestamp=2010-12-06 11:38:11.675, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,proces
s:3824, Role=JrubyMain)
  OldestMember=Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,proc
ess:1280, Role=CoherenceServer)
  ActualMemberSet=MemberSet(Size=2, BitSetCount=2
    Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, R
ole=CoherenceServer)
    Member(Id=2, Timestamp=2010-12-06 11:38:11.675, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:3824, R
ole=JrubyMain)
    )
  RecycleMillis=1200000
  RecycleSet=MemberSet(Size=0, BitSetCount=0
    )
  )


TcpRing{Connections=[1]}
IpMonitor{AddressListSize=0}


2010-12-06 11:38:11.727/2.643 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=2): Service Management joined the clust
er with senior service member 1
2010-12-06 11:38:11.788/2.704 Oracle Coherence GE 3.6.0.0 (thread=main, member=2): Loaded cache configuration from "jar:file:/C:/jdev
/coherence/36/coherence/lib/coherence.jar!/coherence-cache-config.xml"
2010-12-06 11:38:12.017/2.933 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=2): Service DistributedCache joined the clus
ter with senior service member 1
Cache entry 1 = pas apicella
2010-12-06 11:38:12.124/3.040 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=2): Service Management left the cluster


2010-12-06 11:38:12.129/3.045 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=2): Service DistributedCache left the cluste
r
2010-12-06 11:38:12.222/3.138 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=2): Service Cluster left the cluster
Ended at Mon Dec 06 11:38:12 EST 2010


C:\jdev\scripting\demos\jruby>


JYTHON

1. Create a jython script as follows
from com.tangosol.net import CacheFactory, NamedCache
from java.util import Date
from java.lang import System as javasystem

print "*********************************"
print "Coherence 3.6 Example from JYTHON"
print "*********************************"

print "Started at " + Date().toString()

try:

 javasystem.setProperty("tangosol.coherence.distributed.localstorage", "false")
  
 CacheFactory.ensureCluster()
 
 # get named cache
 testcache = CacheFactory.getCache("Test")
 
 # add data to cache
 testcache.put("1", "pas apicella")

 # retrive data from cache
 print "Cache entry 1 = " + testcache.get("1")

except:
    print "Unexpected error:", sys.exc_info()[0]
    raise
    
finally:
    CacheFactory.shutdown()
    
print "Ended at " + Date().toString()
    
    
2. Add coherence.jar to the classpath as shown below.

> set CLASSPATH=C:\jdev\coherence\36\coherence\lib\coherence.jar

3. Run it as follows ensuring your reference coherence.jar correctly

> jy coherence.py

Output as follows


C:\jdev\scripting\demos\jython>jy coherence.py
*********************************
Coherence 3.6 Example from JYTHON
*********************************
Started at Mon Dec 06 11:41:08 EST 2010
2010-12-06 11:41:08.838/1.916 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational configuration from "jar:file:/C:
/jdev/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence.xml"
2010-12-06 11:41:08.844/1.922 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Loaded operational overrides from "jar:file:/C:/jde
v/coherence/36/coherence/lib/coherence.jar!/tangosol-coherence-override-dev.xml"
2010-12-06 11:41:08.845/1.923 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/tangosol-coherence-
override.xml" is not specified
2010-12-06 11:41:08.851/1.929 Oracle Coherence 3.6.0.0 (thread=main, member=n/a): Optional configuration override "/custom-mbeans.xml"
is not specified


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


2010-12-06 11:41:10.012/3.090 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): TCMP bound to /10.187.114.243:8090 using SystemSoc
ketProvider
2010-12-06 11:41:11.191/4.269 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): This Member(Id=3, Timestamp=2010-12-06 11:41:
11.172, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:6172, Role=PythonUtilJython, Edition=Grid Edition,
Mode=Development, CpuCount=4, SocketCount=4) joined cluster "cluster:0xC4DB" with senior Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Add
ress=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, Role=CoherenceServer, Edition=Grid Edition, Mode=Develop
ment, CpuCount=4, SocketCount=4)
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Cluster with senior mem
ber 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service Management with senior
member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service DistributedCache with s
enior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service ReplicatedCache with se
nior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service OptimisticCache with se
nior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=n/a): Member 1 joined Service InvocationService with
senior member 1
2010-12-06 11:41:11.210/4.288 Oracle Coherence GE 3.6.0.0 (thread=main, member=n/a): Started cluster Name=cluster:0xC4DB


Group{Address=224.3.6.0, Port=36000, TTL=4}


MasterMemberSet
  (
  ThisMember=Member(Id=3, Timestamp=2010-12-06 11:41:11.172, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,proces
s:6172, Role=PythonUtilJython)
  OldestMember=Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,proc
ess:1280, Role=CoherenceServer)
  ActualMemberSet=MemberSet(Size=2, BitSetCount=2
    Member(Id=1, Timestamp=2010-12-06 11:37:53.952, Address=10.187.114.243:8088, MachineId=50163, Location=machine:paslap-au,process:1280, R
ole=CoherenceServer)
    Member(Id=3, Timestamp=2010-12-06 11:41:11.172, Address=10.187.114.243:8090, MachineId=50163, Location=machine:paslap-au,process:6172, R
ole=PythonUtilJython)
    )
  RecycleMillis=1200000
  RecycleSet=MemberSet(Size=0, BitSetCount=0
    )
  )


TcpRing{Connections=[1]}
IpMonitor{AddressListSize=0}


2010-12-06 11:41:11.243/4.321 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=3): Service Management joined the clust
er with senior service member 1
2010-12-06 11:41:11.300/4.378 Oracle Coherence GE 3.6.0.0 (thread=main, member=3): Loaded cache configuration from "jar:file:/C:/jdev
/coherence/36/coherence/lib/coherence.jar!/coherence-cache-config.xml"
2010-12-06 11:41:11.438/4.516 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=3): Service DistributedCache joined the clus
ter with senior service member 1
Cache entry 1 = pas apicella
2010-12-06 11:41:11.484/4.562 Oracle Coherence GE 3.6.0.0 (thread=Invocation:Management, member=3): Service Management left the cluster


2010-12-06 11:41:11.488/4.566 Oracle Coherence GE 3.6.0.0 (thread=DistributedCache, member=3): Service DistributedCache left the cluste
r
2010-12-06 11:41:11.580/4.658 Oracle Coherence GE 3.6.0.0 (thread=Cluster, member=3): Service Cluster left the cluster
Ended at Mon Dec 06 11:41:11 EST 2010


C:\jdev\scripting\demos\jython>

1 comment:

Anonymous said...

Great writing! I want to see a follow up on this topic!!!

Yours Truly
Evan