Class: JMX::MBeanProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/jmx/mbean_proxy.rb

Overview

Create a Ruby proxy based on the MBean represented by the object_name This proxy will be able to dispatch to the actual MBean to allow it to execute operations and read/update attributes. The primary mechanism for calling attributes or operations is to just call them as if they represented methods on the MBean. For example:

memory = client["java.lang:type=Memory"]
memory.gc
memory.heap_memory_usage.used

Here we first call an operation on this Memory heap called ‘gc’ and then we access an attribute ‘heap_memory_usage’ (Note: we can use snake-cased naming instead of actual ‘HeapMemoryUsage’). In the case of a naming conflict (existing Ruby method, or same-named attribute as MBean operation), there there are long hand mechanisms:

memory = client["java.lang:type=Memory"]
memory.invoke(:gc)
memory[:heap_memory_usage][:used]

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(server, object_name) ⇒ MBeanProxy

Returns a new instance of MBeanProxy.



42
43
44
45
46
47
48
# File 'lib/jmx/mbean_proxy.rb', line 42

def initialize(server, object_name)
  @server, @object_name = server, object_name
  @info = @server.getMBeanInfo(@object_name)

  define_attributes
  define_operations
end

Class Method Details

.generate(server, object_name) ⇒ Object

Generate a friendly Ruby proxy for the MBean represented by object_name



29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/jmx/mbean_proxy.rb', line 29

def self.generate(server, object_name)
  parent, class_name = MBeans.parent_for object_name.info(server).class_name

  if parent.const_defined? class_name, false
    proxy = parent.const_get(class_name, false)
  else
    proxy = Class.new MBeanProxy
    parent.const_set class_name, proxy
  end

  proxy.new(server, object_name)
end

Instance Method Details

#[](name) ⇒ Object

Get MBean attribute specified by name. If it is just a plain attribute then unwrap the attribute and just return the value.



61
62
63
64
65
# File 'lib/jmx/mbean_proxy.rb', line 61

def [](name)
  attribute = @server.getAttribute(@object_name, name.to_s)
  return attribute.value if attribute.kind_of? javax.management.Attribute
  attribute
end

#[]=(name, value) ⇒ Object

Set MBean attribute specified by name to value



69
70
71
# File 'lib/jmx/mbean_proxy.rb', line 69

def []=(name, value) 
  @server.setAttribute @object_name, javax.management.Attribute.new(name.to_s, value)
end

#add_notification_listener(filter = nil, handback = nil, &listener) ⇒ Object



87
88
89
# File 'lib/jmx/mbean_proxy.rb', line 87

def add_notification_listener(filter=nil, handback=nil, &listener)
  @server.addNotificationListener @object_name, listener, filter, handback
end

#attributesObject



50
51
52
# File 'lib/jmx/mbean_proxy.rb', line 50

def attributes
  @attributes ||= @info.attributes.inject([]) { |s,attr| s << attr.name }
end

#invoke(name, *params) ⇒ Object

Invoke an operation. A NoMethodError will be thrown if this MBean cannot respond to the operation.

FIXME: Add scoring to pick best match instead of first found

Raises:

  • (NoMethodError)


78
79
80
81
82
83
84
85
# File 'lib/jmx/mbean_proxy.rb', line 78

def invoke(name, *params)
  op = @info.operations.find { |o| o.name == name.to_s }
  
  raise NoMethodError.new("No such operation #{name}") unless op

  jargs, jtypes = java_args(op.signature, params)
  @server.invoke @object_name, op.name, jargs, jtypes
end

#operationsObject



54
55
56
# File 'lib/jmx/mbean_proxy.rb', line 54

def operations
  @operations ||= @info.operations.inject([]) { |s,op| s << op.name }
end

#remove_notification_listener(listener) ⇒ Object



91
92
93
# File 'lib/jmx/mbean_proxy.rb', line 91

def remove_notification_listener(listener)
  @server.removeNotificationListener @object_name, listener
end