« Return to Thread: can you create an object in ruby and register it in the JMX server?

Re: can you create an object in ruby and register it in the JMX server?

by Thomas E Enebo :: Rate this Message:

Reply to Author | View in Thread

I wrote something which gets you a good part of the way to what you
want:  jruby -S gem install jmx

For making beans in Ruby it looks something like this:

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

I only support advertising operations at this point, but plan on
attributes and notifications on a really rainy day (of course
contributions are always welcome).  Slightly more details can be found
here (besides the source code + test/examples):

http://ruby.dzone.com/news/jmx-gem-released

-Tom

On Wed, Jun 25, 2008 at 2:51 PM, Jay McGaffigan <hooligan495@...> wrote:

> Hi all,
>
> I'm relatively new (6 months) to ruby and jruby.  At work we are
> transitioning off of the .net stack to use java, jruby, rails and glassfish
> for some of our video on demand products (also I'm picking java up again
> after 4 years on .net) .  As part of this transition I am creating a number
> of infrastructure/utility components for my team to use.  Specifically , I
> am in the middle of using a service wrapper (wrapper.tanukisoftware.org) to
> create manageable ruby services.  So far I can create a generic service
> framework in ruby that calls out to the Java stuff (and likewise the Java
> stuff can call into the ruby stuff.  Mostly this is just to support
> start/stop/restart type of functionality).
>
> Next on my plate is trying to get my Ruby code to hook up to JMX.
> Specifically I would like to be able to create a ruby management bean and be
> able to register it with the platform mbean server.  However I'm not sure
> I'll be able to do it so before I go much further I was wondering if anyone
> can offer any thoughts on a) whether my approach is completely untenable,
> and is there a better approach that you would recommend, or b) what I am
> trying to do is just not possible at the moment.
>
> From reading about JMX it looks like there are lots of ways to skin the cat.
> I've looked around online and found a jmx4r gem but that only appears to
> focus on being able to consume and display jmx endpoints.   I'd like to be
> able to create ruby objects that bind themselves into the JMX management
> server so they can be viewed. (if I can get this working I am hoping to make
> it generic enough that I'd like to add it to the jmx4r gem or other
> opensource project :) if people think it might be useful)
>
> If I were to go on the assumption that there is nothing out there I've
> thought of a couple of ways I could attempt to do this but I'm not sure they
> will work.  (however if there is something out there that someone knows
> about please shout out )
>
> My first thought is that it would be nice to create a ruby object that
> implements the DynamicMBean interface.  From there people could mixin a
> module that exposes the management information.  The problem I am running
> into is the types aren't aligning when I register my ruby class as an MBean
> :P (I could share code if you are interested).
>
> So I created the following example.  (it's gone through a couple of
> incarnations) And let me be the first to say that I don't completely
> understand how to bridge from java to jruby and back so if you can point me
> to places I could get a deeper understanding that'd be great (Ive read the
> examples at the wiki).
>
> I've created a ruby class that inherits from a DynamicBean I've created
> (this example is loosely based on the example on calling Jruby from java
> that's on the Wiki :
>
> Java:
> public class ManageableMBean implements DynamicMBean {
>
>        public Object getAttribute(String arg0) throws
> AttributeNotFoundException,
>                        MBeanException, ReflectionException {
>                return null;
>        }
>
>        public AttributeList getAttributes(String[] arg0) {
>                return null;
>        }
>
>        public MBeanInfo getMBeanInfo() {
>                return null;
>        }
>
>        public Object invoke(String arg0, Object[] arg1, String[] arg2)
>                        throws MBeanException, ReflectionException {
>                return null;
>        }
>
>        public void setAttribute(Attribute arg0) throws
> AttributeNotFoundException,
>                        InvalidAttributeValueException, MBeanException,
> ReflectionException {
>
>        }
>
>        public AttributeList setAttributes(AttributeList arg0) {
>                return null;
>        }
>
> }
>
>
> Here is my Ruby code:
>
> require 'java'
>
> class Manageable < Java::ManageableMBean
>    include javax.management.DynamicMBean   #if I could get the call to
> registerMBean to see my object as the java object it represents I might be
> able to remove this include
>   def getAttribute(attribute)
>     "name"
>   end
>   def getAttributes(attributes)
>     ["name"]
>   end
>   def getMBeanInfo
>     mbi = javax.management.MBeanInfo.new(self.class, "Sample Dynamic
> MBean", null, null, null)
>     puts "#{mbi.class}"
>     mbi
>   end
>   def invoke(actionName, params, signature)
>   end
>   def setAttribute(attribute)
>   end
>   def setAttributes(attributes)
>   end
>   def self.register_bean(bean)
>     mbs = java.lang.management.ManagementFactory.getPlatformMBeanServer()
>     on = javax.management.ObjectName.new("ruby:type=#{bean.class}")
>     mbs.registerMBean(bean.java_class, on)  #it would be nice to be able to
> coerce the ruby object in to the base java class
>   end
> end
>
> bean = Manageable.new
>
> puts "name is #{bean.class}"
>
> Manageable.register_bean(bean)
>
> c = getc.chomp
>
>
>
> Even if I could register the bean with the server I'm not sure this is going
> to work but I was thinking that creating a dsl in ruby would let me hide the
> dynamic nature of the mbean.
>
>
> I might have to go back to the drawing board.  Are there any
> pointers/alternate paths I should be exploring?
>
> Thanks!
> Jay
>
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>    http://xircles.codehaus.org/manage_email
>
>
>



--
Blog: http://www.bloglines.com/blog/ThomasEEnebo
Email: enebo@... , tom.enebo@...

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


 « Return to Thread: can you create an object in ruby and register it in the JMX server?