Issues with dynamically registering an MBean

  3 posts   Feedicon  
Replies: 2 - Last Post: March 04, 2011 20:17
by: bpardee
« Older Topic » Newer Topic
showing 1 - 3 of 3
 
Posted: March 04, 2011 14:47 by bpardee
Cool stuff! I noticed a couple of issues though. I have the test code below where the first mbean has an operation to create the second mbean. The problems are that the first contains the operations for the second and when the second gets created, it has no operations.

Also, it would be nice if the attributes checked for accessor methods as well as instance variiables as I use my mbean as a proxy into another object that's actually doing the work. I can possibly add this and issue a pull if you think it's a good idea.

Thanks!

require 'rubygems'
require 'jmx'

$jmx_server = JMX::MBeanServer.new
$domain = 'MyDomain'

class MyDynamicMBean < RubyDynamicMBean
  operation "Doubles a value"
  parameter :int, "a", "Value to double"
  returns :int
  def double(a)
    a + a
  end

  operation "Doubles a string"
  parameter :string, "a", "Value to double"
  returns :string
  def string_double(a)
    a + a
  end

  operation "Creates a new MBean"
  returns :string
  def create_mbean
    bean = MyTrulyDynamicMBean.new($domain, "Created MBean")
    puts "Registering created mbean"
    $jmx_server.register_mbean(bean, "#{$domain}:type=Test2")
    "success"
  end
end

class MyTrulyDynamicMBean < RubyDynamicMBean
  operation "Give me foo"
  returns :string
  def foo
    "foo"
  end

  operation "Concatentates a list"
  parameter :list, "list", "List to concatenate"
  returns :string
  def concat(list)
    list.inject("") { |memo, element| memo << element.to_s }
  end
end

bean = MyDynamicMBean.new($domain, "Initial MBean")
puts "Registering initial mbean"
$jmx_server.register_mbean(bean, "#{$domain}:type=Test")
sleep 1000

 
Posted: March 04, 2011 19:22 by bpardee
Here's a patch for the problem where the attributes and operations show up in wrong MBeans:

--- /Users/bpardee/Documents/jmx_jruby/lib/jmx/dynamic_mbean.rb	2011-02-24 15:20:45.000000000 -0500
+++ ./dynamic_mbean.rb	2011-03-04 13:53:27.000000000 -0500
@@ -109,18 +109,18 @@
   # TODO: preserve any original method_added?
   # TODO: Error handling here when it all goes wrong?
   def self.method_added(name) #:nodoc:
-    return if Thread.current[:op].nil?
-    Thread.current[:op].name = name
-    operations << Thread.current[:op].to_jmx
-    Thread.current[:op] = nil
+    return if local_hash[:op].nil?
+    local_hash[:op].name = name
+    operations << local_hash[:op].to_jmx
+    local_hash[:op] = nil
   end
 
   def self.attributes #:nodoc:
-    Thread.current[:attrs] ||= []
+    local_hash[:attrs] ||= []
   end
   
   def self.operations #:nodoc:
-    Thread.current[:ops] ||= []
+    local_hash[:ops] ||= []
   end
 
   def self.define_getter(name, type)
@@ -182,7 +182,7 @@
   #++
   def self.operation(description)
     # Wait to error check until method_added so we can know method name
-    Thread.current[:op] = JMX::Operation.new description
+    local_hash[:op] = JMX::Operation.new description
   end
 
   # Used to declare a parameter (you can declare more than one in succession) that
@@ -192,7 +192,7 @@
   #     def start
   #     end
   def self.parameter(type, name=nil, description=nil)
-    Thread.current[:op].parameters << JMX::Parameter.new(type, name, description)
+    local_hash[:op].parameters << JMX::Parameter.new(type, name, description)
   end
 
   # Used to declare the return type of the operation
@@ -202,9 +202,14 @@
   #     def set_name
   #     end
   def self.returns(type)
-    Thread.current[:op].return_type = type
+    local_hash[:op].return_type = type
   end
   
+  # Thread local storage for the derived bean
+  def self.local_hash
+    Thread.current[self.name] ||= {}
+  end
+
   # when creating a dynamic MBean we need to provide it with a name and a description.
   def initialize(name, description)
     operations = self.class.operations.to_java MBeanOperationInfo
 
Posted: March 04, 2011 20:17 by bpardee
Here's a patch for my request in the initial post that attributes use accessor methods if they're available.

--- a/lib/jmx/dynamic_mbean.rb
+++ b/lib/jmx/dynamic_mbean.rb
@@ -129,7 +129,7 @@ class RubyDynamicMBean
     value_proc = java_type ? proc { |value| java_type.new value } : proc { |value| Java.ruby_to_java value }
 
     define_method("jmx_get_#{name.downcase}") do
-      javax.management.Attribute.new name, value_proc.call(instance_variable_get('@' + name))
+      javax.management.Attribute.new name, value_proc.call(send name)
     end
   end
 
@@ -137,7 +137,7 @@ class RubyDynamicMBean
     value_converter = JMX::JavaTypeAware.to_ruby(type)
 
     define_method("jmx_set_#{name.downcase}") do |value| 
-      instance_variable_set '@' + name, value_converter.call(value)
+      send name+'=', value_converter.call(value)
     end    
   end
   
Replies: 2 - Last Post: March 04, 2011 20:17
by: bpardee
« Older Topic » Newer Topic
  • Mysql
  • Glassfish
  • Jruby
  • Rails
  • Nblogo
Terms of Use; Privacy Policy;
© 2014, Oracle Corporation and/or its affiliates
(revision 20150626.29986a4)
 
 
Close
loading
Please Confirm
Close