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
In the example below jy = jython and jrb = jruby executables.
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"
> 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
/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
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
override.xml" is not specified
2010-12-06 11:38:09.946/0.862 Oracle Coherence 3.6.0.0
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
ketProvider
2010-12-06 11:38:11.683/2.600 Oracle Coherence GE 3.6.0.0
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
ber 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0
member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0
enior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0
nior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0
nior member 1
2010-12-06 11:38:11.693/2.609 Oracle Coherence GE 3.6.0.0
senior member 1
2010-12-06 11:38:11.697/2.613 Oracle Coherence GE 3.6.0.0
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
er with senior service member 1
2010-12-06 11:38:11.788/2.704 Oracle Coherence GE 3.6.0.0
/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
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
2010-12-06 11:38:12.129/3.045 Oracle Coherence GE 3.6.0.0
r
2010-12-06 11:38:12.222/3.138 Oracle Coherence GE 3.6.0.0
Ended at Mon Dec 06 11:38:12 EST 2010
C:\jdev\scripting\demos\jruby>
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()
> 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
/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
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
override.xml" is not specified
2010-12-06 11:41:08.851/1.929 Oracle Coherence 3.6.0.0
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
ketProvider
2010-12-06 11:41:11.191/4.269 Oracle Coherence GE 3.6.0.0
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
ber 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0
member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0
enior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0
nior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0
nior member 1
2010-12-06 11:41:11.207/4.285 Oracle Coherence GE 3.6.0.0
senior member 1
2010-12-06 11:41:11.210/4.288 Oracle Coherence GE 3.6.0.0
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
er with senior service member 1
2010-12-06 11:41:11.300/4.378 Oracle Coherence GE 3.6.0.0
/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
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
2010-12-06 11:41:11.488/4.566 Oracle Coherence GE 3.6.0.0
r
2010-12-06 11:41:11.580/4.658 Oracle Coherence GE 3.6.0.0
Ended at Mon Dec 06 11:41:11 EST 2010
C:\jdev\scripting\demos\jython>
2 comments:
Java may be the main choice for enterprise development now, but it’s days are numbered as the only stalwart option to go with.
Let’s face it, many of these so called “enterprise applications” could easily have been written much faster and with less overhead using technologies like Python, PHP, et al.
uml training
Great writing! I want to see a follow up on this topic!!!
Yours Truly
Evan
Post a Comment