Articles
Enterprise Management
by Pas Apicella
Published April 2011
The JMX technology, now included in the Java SE platform, provides the tools for building rich, modular, and dynamic solutions for managing and monitoring devices, applications, and service-driven networks.
This article shows how to use the JRuby programming language with JMX to query an Oracle WebLogic Server 11g Release 1 managed server to display runtime information on the instance.
The following software is presumed to be installed:
The examples should also work with some other versions of this software (but have not been tested on them).
$ java -version java version "1.6.0_23" Java(TM) SE Runtime Environment (build 1.6.0_23-b05) Java HotSpot(TM) 64-Bit Server VM (build 19.0-b09, mixed mode)
$ export JRUBY_HOME=$HOME/jruby-1.5.6 $ export PATH=$PATH:$JRUBY_HOME/bin $ jruby -v jruby 1.5.6 (ruby 1.8.7 patchlevel 249) (2010-12-03 9cf97c3) (Java HotSpot(TM) Client VM 1.6.0_22) [x86-java]
$ jruby -S gem install jmx4r JRuby limited openssl loaded. http://jruby.org/openssl gem install jruby-openssl for full support. Successfully installed jmx4r-0.1.3 1 gem installed Installing ri documentation for jmx4r-0.1.3... Installing RDoc documentation for jmx4r-0.1.3...
$ jruby -S gem list *** LOCAL GEMS *** columnize (0.3.1) jmx4r (0.1.3) rake (0.8.7) rspec (1.3.0) ruby-debug (0.10.3) ruby-debug-base (0.10.3.2) sources (0.0.1)

# verify JMX connection to WebLogic 11g Release 1
require 'rubygems'
require 'jmx4r'
require '/u01/fmw/1034/wlserver_10.3/server/lib/wljmxclient.jar'
java_import java.lang.System
class VerifyWeblogicJMX
def initialize(user, passwd, url)
@user, @passwd, @url = user, passwd, url
System.setProperty("jmx.remote.protocol.provider.pkgs", "weblogic.management.remote")
@conn = JMX::MBean.establish_connection :url => url, :username => user, :password => passwd
end
# add getters and setters for all attrributes we wish to expose
attr_reader :user, :passwd, :url, :conn
def close
@conn.close unless !@conn
end
def to_s
"JMXConnection [user=#{@user}, passwd=#{@passwd}, " +
"url=#{@url}]"
end
end
username = "weblogic"
password = "welcome1"
url = "service:jmx:iiop://papicell-au.au.oracle.com:7003/jndi/weblogic.management.mbeanservers.runtime"
print "Run at #{Time.now} using JRuby #{RUBY_VERSION}\n\n"
begin
test = VerifyWeblogicJMX.new(username, password, url)
puts test
puts "\nSuccessfully connected to Oracle Weblogic 10.3.x from JRuby using JMX "
rescue
puts "\n** Error occured **\n"
puts "Failed executing Oracle Weblogix JMX demo from JRuby ", $!, "\n"
ensure
test.close
end
print "Ended at #{Time.now}"
service:jmx:iiop://{hostname}:{managed-server-port}/jndi/weblogic.management.mbeanservers.runtime
username = "weblogic" password = "welcome1" url = "service:jmx:iiop://papicell-au.au.oracle.com:7003/jndi/weblogic.management.mbeanservers.runtime"
$ jruby test_jmx_weblogic.rb Run at Thu Mar 31 08:09:49 +1100 2011 using JRuby 1.8.7 JMXConnection [user=weblogic, passwd=welcome1, url=service:jmx:iiop://papicell-au.au.oracle.com:7003/jndi/weblogic.management.mbeanservers.runtime] Successfully connected to Oracle Weblogic 10.3.x from JRuby using JMX Ended at Thu Mar 31 08:09:50 +1100 2011
require 'rubygems'
require 'jmx4r'
require '/u01/fmw/1034/wlserver_10.3/server/lib/wljmxclient.jar'
java_import 'javax.management.ObjectName'
java_import java.lang.System
def display_array (value)
data = ""
value.each do |x|
data += "\n\t" + x.to_s
end
return data
end
# method used to check if the attribute data contains an array of data and make some sort of effort
# to display it as text. This is quick and dirty way to do this but generally works for most array
# attribute values. If not an array it simply display the attribute value as a string
def display_attribute_data(conn, object_name, attribute)
s = conn.get_attribute object_name, attribute
search_str = s.to_s
if (/^\[Ljava.lang.String/.match(search_str)) or
(/^\[I/.match(search_str)) or
(/^\[Ljavax.management.ObjectName/.match(search_str))
# we have a array with data
return display_array s
end
return s;
end
class JMXConnection
def initialize(user, passwd, url)
@user, @passwd, @url = user, passwd, url
System.setProperty("jmx.remote.protocol.provider.pkgs", "weblogic.management.remote")
@conn = JMX::MBean.establish_connection :url => url, :username => user, :password => passwd
end
# add getters and setters for all attrributes we wish to expose
attr_reader :user, :passwd, :url, :conn
def close
@conn.close unless !@conn
end
def to_s
"JMXConnection [user=#{@user}, passwd=#{@passwd}, " +
"url=#{@url}]"
end
end
username = "weblogic"
password = "welcome1"
url = "service:jmx:iiop://papicell-au.au.oracle.com:7003/jndi/weblogic.management.mbeanservers.runtime"
print "Run at #{Time.now} using JRuby #{RUBY_VERSION}\n\n"
begin
jmx_conn = JMXConnection.new(username, password, url)
puts jmx_conn
puts "\nSuccessfully connected to Oracle Weblogic 10.3.x from JRuby using JMX "
wls_mbean_name = "com.bea:ServerRuntime=apple,Name=apple,Type=JVMRuntime"
mbean = JMX::MBean.find_by_name wls_mbean_name
java_object_name = ObjectName.new wls_mbean_name
while (true)
# display attributes key/values
mbean.attributes.each do |key, value|
puts "Name: #{value}, Value: #{display_attribute_data jmx_conn.conn, java_object_name, value}\n"
end
# sleep for 30 seconds
puts "\nSleeping for 30 seconds....\n"
sleep 30
end
rescue
puts "\n** Error occured **\n"
puts "Failed executing Oracle Weblogix JMX demo from JRuby ", $!, "\n"
ensure
jmx_conn.close
end
print "Ended at #{Time.now}\n"
require '/u01/fmw/1034/wlserver_10.3/server/lib/wljmxclient.jar'Be sure to specify the correct connection credentials for your server.
username = "weblogic" password = "welcome1" url = "service:jmx:iiop://papicell-au.au.oracle.com:7003/jndi/weblogic.management.mbeanservers.runtime"
$ jruby monitor-mbean.rb Run at Mon Apr 04 10:26:46 +1000 2011 using JRuby 1.8.7 JMXConnection [user=weblogic, passwd=welcome1, url=service:jmx:iiop://papicell-au.au.oracle.com:7003/jndi/weblogic.management.mbeanservers.runtime] Successfully connected to Oracle Weblogic 10.3.x from JRuby using JMX Name: Parent, Value: com.bea:Name=apple,Type=ServerRuntime Name: Uptime, Value: 2772452 Name: HeapFreePercent, Value: 90 Name: JavaVersion, Value: 1.6.0_22 Name: Type, Value: JVMRuntime Name: ThreadStackDump, Value: "[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'" waiting for lock weblogic.work.ExecuteThread@1f2caf4 WAITING java.lang.Object.wait(Native Method) java.lang.Object.wait(Object.java:485) weblogic.work.ExecuteThread.waitForRequest(ExecuteThread.java:160) weblogic.work.ExecuteThread.run(ExecuteThread.java:181) "DynamicListenThread[Default[1]]" RUNNABLE native java.net.PlainSocketImpl.socketAccept(Native Method) java.net.PlainSocketImpl.accept(PlainSocketImpl.java:390) ..... Name: OSVersion, Value: 5.10 Name: JavaVendor, Value: Sun Microsystems Inc. Name: HeapFreeCurrent, Value: 210922280 Name: OSName, Value: SunOS Name: Name, Value: apple Name: HeapSizeMax, Value: 518979584 Name: JavaVMVendor, Value: Sun Microsystems Inc. Name: HeapSizeCurrent, Value: 259522560 Sleeping for 30 seconds.... Name: Parent, Value: com.bea:Name=apple,Type=ServerRuntime Name: Uptime, Value: 2802565 Name: HeapFreePercent, Value: 90 Name: JavaVersion, Value: 1.6.0_22 Name: Type, Value: JVMRuntime Name: ThreadStackDump, Value: "[STANDBY] ExecuteThread: '3' for queue: 'weblogic.kernel.Default (self-tuning)'" waiting for lock weblogic.work.ExecuteThread@1f2caf4 WAITING java.lang.Object.wait(Native Method) ....... Name: OSVersion, Value: 5.10 Name: JavaVendor, Value: Sun Microsystems Inc. Name: HeapFreeCurrent, Value: 210225232 Name: OSName, Value: SunOS Name: Name, Value: apple Name: HeapSizeMax, Value: 518979584 Name: JavaVMVendor, Value: Sun Microsystems Inc. Name: HeapSizeCurrent, Value: 259522560 Sleeping for 30 seconds....
Congratulations, you have just learned how to access a Weblogic Server using JRuby with JMX. You could use such a setup to dynamically check attribute values and alert an administrator should they reach higher or lower then expected values.