Class: JMX::DynamicMBean
- Inherits:
-
Object
- Object
- JMX::DynamicMBean
- Includes:
- JavaTypeAware
- Defined in:
- lib/dynamic_mbean.rb
Overview
Creators of Ruby based MBeans must inherit this class (DynamicMBean
) in their own bean classes and then register them with a JMX mbean server.
Here is an example:
class MyMBean < DynamicMBean
rw_attribute :status, :string, "Status information for this process"
operation "Shutdown this process"
parameter :string, "user_name", "Name of user requesting shutdown"
returns :string
def shutdown(user_name)
"shutdown requests more time"
end
end
Once you have defined your bean class you can start declaring attributes and operations. Attributes come in three flavors: read, write, and read write. Simmilar to the attr*
helpers, there are helpers that are used to create management attributes. Use r_attribute
, w_attribute
, and rw_attribute
to declare attributes, and the operation
, returns
, and parameter
helpers to define a management operation. Creating attributes with the *_attribute convention ALSO creates ruby accessors (it invokes the attr_accessor/attr_reader/attr_writer ruby helpers) to create ruby methods like: user_name= and username. So in your ruby code you can treat the attributes as “regular” ruby accessors
Constant Summary
Constants included from JavaTypeAware
Class Method Summary collapse
-
.attributes ⇒ Object
:nodoc:.
-
.inherited(cls) ⇒ Object
NOTE this will not be needed when JRuby-3164 is fixed.
- .mbean_attributes ⇒ Object
-
.method_added(name) ⇒ Object
TODO: preserve any original method_added? TODO: Error handling here when it all goes wrong?.
-
.operation(description = nil) ⇒ Object
Use the operation method to declare the start of an operation It takes as an optional argument the description for the operation Example: operation “Used to start the service” def start end – Last operation wins if more than one ++.
-
.operations ⇒ Object
:nodoc:.
-
.parameter(type, name = nil, description = nil) ⇒ Object
Used to declare a parameter (you can declare more than one in succession) that is associated with the currently declared operation.
-
.r_attribute(name, type, description) ⇒ Object
the
r_attribute
method is used to declare a JMX read only attribute. -
.returns(type) ⇒ Object
Used to declare the return type of the operation operation “Used to update the name of a service” parameter :string, “name”, “Set the new name of the service” returns :void def do_stuff …
-
.rw_attribute(name, type, description = nil) ⇒ Object
the
rw_attribute
method is used to declare a JMX read write attribute. -
.w_attribute(name, type, description) ⇒ Object
the
w_attribute
method is used to declare a JMX write only attribute.
Instance Method Summary collapse
-
#getAttribute(attribute) ⇒ Object
Retrieve the value of the requested attribute.
- #getAttributes(attributes) ⇒ Object
- #getMBeanInfo ⇒ Object
-
#initialize(description = "") ⇒ DynamicMBean
constructor
A new instance of DynamicMBean.
- #inspect ⇒ Object
- #invoke(actionName, params = nil, signature = nil) ⇒ Object
- #setAttribute(attribute) ⇒ Object
- #setAttributes(attributes) ⇒ Object
- #to_s ⇒ Object
- #toString ⇒ Object
Methods included from JavaTypeAware
Constructor Details
#initialize(description = "") ⇒ DynamicMBean
Returns a new instance of DynamicMBean.
241 242 243 244 245 246 |
# File 'lib/dynamic_mbean.rb', line 241 def initialize(description="") name = self.class.to_s operations = self.class.operations.to_java(MBeanOperationInfo) attributes = self.class.attributes.to_java(MBeanAttributeInfo) @info = MBeanInfo.new name, description, attributes, nil, operations, nil end |
Class Method Details
.attributes ⇒ Object
:nodoc:
126 127 128 |
# File 'lib/dynamic_mbean.rb', line 126 def self.attributes #:nodoc: self.mbean_attributes[:attrs] ||= [] end |
.inherited(cls) ⇒ Object
NOTE this will not be needed when JRuby-3164 is fixed.
109 110 111 |
# File 'lib/dynamic_mbean.rb', line 109 def self.inherited(cls) cls.send(:include, DynamicMBean) end |
.mbean_attributes ⇒ Object
113 114 115 |
# File 'lib/dynamic_mbean.rb', line 113 def self.mbean_attributes @mbean_attributes ||= {} end |
.method_added(name) ⇒ Object
TODO: preserve any original method_added? TODO: Error handling here when it all goes wrong?
119 120 121 122 123 124 |
# File 'lib/dynamic_mbean.rb', line 119 def self.method_added(name) #:nodoc: return if self.mbean_attributes[:op].nil? self.mbean_attributes[:op].name = name operations << self.mbean_attributes[:op].to_jmx self.mbean_attributes[:op] = nil end |
.operation(description = nil) ⇒ Object
Use the operation method to declare the start of an operation It takes as an optional argument the description for the operation Example:
operation "Used to start the service"
def start
end
– Last operation wins if more than one ++
212 213 214 215 |
# File 'lib/dynamic_mbean.rb', line 212 def self.operation(description=nil) # Wait to error check until method_added so we can know method name self.mbean_attributes[:op] = JMX::Operation.new description end |
.operations ⇒ Object
:nodoc:
130 131 132 |
# File 'lib/dynamic_mbean.rb', line 130 def self.operations #:nodoc: self.mbean_attributes[:ops] ||= [] end |
.parameter(type, name = nil, description = nil) ⇒ Object
Used to declare a parameter (you can declare more than one in succession) that is associated with the currently declared operation. The type is mandatory, the name and description are optional. Example:
operation "Used to update the name of a service"
parameter :string, "name", "Set the new name of the service"
def start(name)
...
end
226 227 228 |
# File 'lib/dynamic_mbean.rb', line 226 def self.parameter(type, name=nil, description=nil) self.mbean_attributes[:op].parameters << JMX::Parameter.new(type, name, description) end |
.r_attribute(name, type, description) ⇒ Object
the r_attribute
method is used to declare a JMX read only attribute. see the JavaSimpleTypes
module for more information about acceptable types usage:
r_attribute :attribute_name, :string, "Description displayed in a JMX console"
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 |
# File 'lib/dynamic_mbean.rb', line 173 def self.r_attribute(name, type, description) attributes << JMX::Attribute.new(name, type, description, true, false).to_jmx attr_reader name #create a "java" oriented accessor method define_method("jmx_get_#{name.to_s.downcase}") do begin #attempt conversion java_type = to_java_type(type) value = eval "#{java_type}.new(@#{name.to_s})" rescue #otherwise turn it into a java Object type for now. value = eval "Java.ruby_to_java(@#{name.to_s})" end attribute = javax.management.Attribute.new(name.to_s, value) end end |
.returns(type) ⇒ Object
Used to declare the return type of the operation
operation "Used to update the name of a service"
parameter :string, "name", "Set the new name of the service"
returns :void
def do_stuff
...
end
237 238 239 |
# File 'lib/dynamic_mbean.rb', line 237 def self.returns(type) self.mbean_attributes[:op].return_type = type end |
.rw_attribute(name, type, description = nil) ⇒ Object
the rw_attribute
method is used to declare a JMX read write attribute. see the JavaSimpleTypes
module for more information about acceptable types usage: rw_attribute :attribute_name, :string, “Description displayed in a JMX console”
The name and type parameters are mandatory The description parameter is optional (defaults to the same value than the env: ruby: No such file or directory name parameter in that case) – methods used to create an attribute. They are modeled on the attrib_accessor patterns of creating getters and setters in ruby ++
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/dynamic_mbean.rb', line 146 def self.rw_attribute(name, type, description=nil) description ||= name.to_s attributes << JMX::Attribute.new(name, type, description, true, true).to_jmx attr_accessor name #create a "java" oriented accessor method define_method("jmx_get_#{name.to_s.downcase}") do begin #attempt conversion java_type = to_java_type(type) value = eval "#{java_type}.new(@#{name.to_s})" rescue #otherwise turn it into a java Object type for now. value = eval "Java.ruby_to_java(@#{name.to_s})" end attribute = javax.management.Attribute.new(name.to_s, value) end define_method("jmx_set_#{name.to_s.downcase}") do |value| blck = to_ruby(type) send "#{name.to_s}=", blck.call(value) end end |
.w_attribute(name, type, description) ⇒ Object
the w_attribute
method is used to declare a JMX write only attribute. see the JavaSimpleTypes
module for more information about acceptable types usage:
w_attribute :attribute_name, :string, "Description displayed in a JMX console"
194 195 196 197 198 199 200 201 |
# File 'lib/dynamic_mbean.rb', line 194 def self.w_attribute(name, type, description) attributes << JMX::Attribute.new(name, type, description, false, true).to_jmx attr_writer name define_method("jmx_set_#{name.to_s.downcase}") do |value| blck = to_ruby(type) eval "@#{name.to_s} = #{blck.call(value)}" end end |
Instance Method Details
#getAttribute(attribute) ⇒ Object
Retrieve the value of the requested attribute
249 250 251 |
# File 'lib/dynamic_mbean.rb', line 249 def getAttribute(attribute) send("jmx_get_"+attribute.downcase).value end |
#getAttributes(attributes) ⇒ Object
253 254 255 256 257 |
# File 'lib/dynamic_mbean.rb', line 253 def getAttributes(attributes) attrs = javax.management.AttributeList.new attributes.each { |attribute| attrs.add send("jmx_get_"+attribute.downcase) } attrs end |
#getMBeanInfo ⇒ Object
259 |
# File 'lib/dynamic_mbean.rb', line 259 def getMBeanInfo; @info; end |
#inspect ⇒ Object
274 |
# File 'lib/dynamic_mbean.rb', line 274 def inspect; toString; end |
#invoke(actionName, params = nil, signature = nil) ⇒ Object
261 262 263 |
# File 'lib/dynamic_mbean.rb', line 261 def invoke(actionName, params=nil, signature=nil) send(actionName, *params) end |
#setAttribute(attribute) ⇒ Object
265 266 267 |
# File 'lib/dynamic_mbean.rb', line 265 def setAttribute(attribute) send("jmx_set_#{attribute.name.downcase}", attribute.value) end |
#setAttributes(attributes) ⇒ Object
269 270 271 |
# File 'lib/dynamic_mbean.rb', line 269 def setAttributes(attributes) attributes.each { |attribute| setAttribute attribute} end |
#to_s ⇒ Object
273 |
# File 'lib/dynamic_mbean.rb', line 273 def to_s; toString; end |
#toString ⇒ Object
275 |
# File 'lib/dynamic_mbean.rb', line 275 def toString; "#@info.class_name: #@info.description"; end |