Ruby-esque JMX -- Part 2
Below is the code from the JMX tinkering. I snake-ized the keys to be more Ruby-esque, per Ed's suggestion.
include Java
# Obviously stolen from Rails ActiveSupport for underscoring strings
self.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
gsub(/([a-z\d])([A-Z])/,'\1_\2').
tr("-", "_").
downcase
end
end
include_class 'javax.management.ObjectName'
include_package "java.lang.management"
include_package "javax.management"
include_package "javax.management.remote"
include_class "java.util.HashMap"
attr_accessor :name
@object_name = object_name
@name = @object_name.to_s
end
attrs = MBean.connection.getMBeanInfo(@object_name).attributes rescue []
attrs.inject({}) do |list, a|
list[a.name.underscore] = MBean.connection.getAttribute(@object_name, "") rescue "Unknown"
list
end
end
object_name = ObjectName.new(name)
beans = MBean.connection.queryMBeans(object_name,nil )
beans.collect {|bean| MBean.new(bean.get_object_name)}
end
#obviously inefficient
find_all_by_name(name).first
end
@@mbsc ||= begin
#load from some config file later maybe
url = "service:jmx:rmi:///jndi/rmi://localhost:1099/jmxrmi"
connector = JMXConnectorFactory::connect(JMXServiceURL.new(url), HashMap.new)
connector.getMBeanServerConnection
end
end
protected
attributes.keys.include?(method.to_s) ? attributes[method.to_s] : super
end
end
end
#Find all the MBeans matching some object name
mbeans = JMX::MBean.find_all_by_name("java.lang:*")
puts "Found beans"
#Find a bean, and get its attribute
bean = JMX::MBean.find_by_name("java.lang:type=ClassLoading")
puts "There are classes loaded in that VM"
3 comments:
Cool script. The Rails idioms make the code more readable (and more Ruby-esque than my version)
Do you plan to extend it to support writing attributes and invoking operations?
jeff
http://jmesnil.net/weblog/
Jeff,
I primarily needed to retrieve read only attributes, but adding method invocation shouldnt be too hard. As long as you can inspect the beans to determine method signatures. Feel free to grab the code and play around with the idea. The method missing approach will have to be slightly modified, but not significantly.
Aaron, fyi I added some of you code to my protoypes and released a very simple library: jmx4r[1].
I also took the opportunity to add suport for writable attributes & operations invocations[2].
--jeff
http://jmesnil.net/weblog/
[1] http://code.google.com/p/jmx4r/
[2] http://jmesnil.net/weblog/2007/06/11/jmx4r-a-jmx-libary-for-jruby/
Post a Comment