Search This Blog

Wednesday, January 14, 2009

How to run a Simple JMX-Groovy Example with OC4J

This note explains how to run a simple JMX script using the groovy scripting language.
Software Requirements/Prerequisites
Download groovy software from http://dist.groovy.codehaus.org/distributions/groovy-binary-1.5.6.zip
Extract this to a directory say D:\groovy-1.5.6
Configuring the Sample Code
Make sure following startup options are set when either starting the OC4J standalone or the Oracle Application Server:
com.sun.management.jmxremote
com.sun.management.jmxremote.port=9004
com.sun.management.jmxremote.authenticate=false
com.sun.management.jmxremote.ssl=false
If you are using OAS , then please set these parameters in the configuration file $ORACLE_HOME/opmn/config/opmn.xml, for example:
<category id="start-parameters">
<data id="java-options" value="-server -XX:MaxPermSize=128M -ms512M -mx1024M -XX:AppendRatio=3 -Djava.security.policy=$ORACLE_HOME/j2ee/home/config/java2.policy -Djava.awt.headless=true -Dhttp.webdir.enable=false -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false "/>

For OC4J standalone the startup command would look like:
java -Dcom.sun.management.jmxremote -Dcom.sun.management.jmxremote.port=9004 -Dcom.sun.management.jmxremote.authenticate=false -Dcom.sun.management.jmxremote.ssl=false -jar oc4j.jar
Running the Sample Code
Copy and Paste the sample code (see below) in a text file and save this file as say test.groovy

Set the following environment variables:
set ORACLE_HOME=c:\oc4j10133
set GROOVY_HOME=D:\groovy-1.5.6
set classpath=%GROOVY_HOME%\lib\commons-cli-1.0.jar;%GROOVY_HOME%\lib\groovy-1.5.6.jar;%ORACLE_HOME%\j2ee\home\admin_client.jar;%ORACLE_HOME%\j2ee\home\oc4jclient.jar;%GROOVY_HOME%\lib\asm-2.2.jar;%GROOVY_HOME%\lib\antlr-2.7.6.jar

Run the example using the command:
java -classpath %CLASSPATH% groovy.lang.GroovyShell test.groovy
Sample Code
import javax.management.remote.*
import oracle.oc4j.admin.jmx.remote.api.JMXConnectorConstant
import javax.management.remote.JMXConnectorFactory as JmxFactory
def serverUrl = new JMXServiceURL('service:jmx:rmi://localhost:23791')
def serverPath = 'oc4j:j2eeType=J2EEServer,name=standalone'
def jvmPath = 'oc4j:j2eeType=JVM,name=single,J2EEServer=standalone'
def provider = 'oracle.oc4j.admin.jmx.remote'
def credentials = [ (JMXConnectorConstant.CREDENTIALS_LOGIN_KEY): 'oc4jadmin'
, (JMXConnectorConstant.CREDENTIALS_PASSWORD_KEY): 'welcome1']
def env = [ (JMXConnectorFactory.PROTOCOL_PROVIDER_PACKAGES): provider
, (JMXConnector.CREDENTIALS): credentials ]
def server = JmxFactory.connect(serverUrl, env).mBeanServerConnection
def serverInfo = new GroovyMBean(server, serverPath)
def jvmInfo = new GroovyMBean(server, jvmPath)
println """Connected to $serverInfo.node. \
Server started ${new Date(serverInfo.startTime)}.
OC4J version: $serverInfo.serverVersion from $serverInfo.serverVendor
JVM version: $jvmInfo.javaVersion from $jvmInfo.javaVendor
Memory usage: $jvmInfo.freeMemory bytes free, \
$jvmInfo.totalMemory bytes total
"""
def query = new javax.management.ObjectName('oc4j:*')
String[] allNames = server.queryNames(query, null)
def dests = allNames.findAll{ name ->
name.contains('j2eeType=JMSDestinationResource')
}.collect{ new GroovyMBean(server, it) }
println "Found ${dests.size()} JMS destinations. Listing ..."
dests.each{ d -> println "$d.name: $d.location" }

Sample Code Output
This is the expected output from the above groovy script
Connected to localhost.idc.oracle.com. Server started Tue Jul 22 12:32:55 IST 2008.
OC4J version: 10.1.3.3.0 from Oracle Corp.
JVM version: 1.5.0_15 from Sun Microsystems Inc.
Memory usage: 3657056 bytes free, 20709376 bytes total
Found 8 JMS destinations. Listing ...
jms/queues/ErrorQueue: jms/queues/ErrorQueue
jms/PSSPBasicInfo: jms/PSSPBasicInfo
jms/myTopic: jms/myTopic
Demo Queue: jms/demoQueue
jms/queues/ReceiveQueue: jms/queues/ReceiveQueue
Demo Topic: jms/demoTopic
jms/Oc4jJmsExceptionQueue: jms/Oc4jJmsExceptionQueue
jms/RAExceptionQueue: jms/RAExceptionQueue

1 comment:

Unknown said...

Hi Ravi,

Can u please tell me how to use Jython script to configure Oracle application server??

I searched in many sites for the solution. But, I could not find any. So, thought you might give some hint on how to use Jython for scripting

Thanks,
Mamatha