<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/css" href="/stylesheets/rss.css"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/">
  <channel>
    <title>RealityForge.org: PanMX: Exposing MBeans the easy way</title>
    <link>/articles/2006/02/27/panmx-exposing-mbeans-the-easy-way</link>
    <language>en-us</language>
    <ttl>40</ttl>
    <description>A little short for a storm trooper</description>
    <item>
      <title>PanMX: Exposing MBeans the easy way</title>
      <description>&lt;p&gt;PanMX removes the pain from developing MBeans. It was a toolkit I developed years ago to make things less painful. I have been cleaning up my subversion repositories and I figured it may be useful for others. So here it is released to the public at large.&lt;/p&gt;


	&lt;p&gt;The user can use Java 1.5 Annotations to define the MBean interface or allow the toolkit to derive the MBean interface using reflection.&lt;/p&gt;


	&lt;p&gt;The toolkit allows the user to define &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/javax/management/modelmbean/ModelMBean.html"&gt;ModelMBeans&lt;/a&gt; via the &lt;a href="http://www.realityforge.org/static/PanMX/api/panmx/model/ModelMBeanFactory.html"&gt;ModelMBeanFactory&lt;/a&gt; or &lt;span class="caps"&gt;RMX&lt;/span&gt;Beans which are very similar &lt;a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/management/ManagementFactory.html#MXBean"&gt;MXBeans&lt;/a&gt; via the &lt;a href="http://www.realityforge.org/static/PanMX/api/panmx/rmx/RMXBeanFactory.html"&gt;RMXBeanFactory&lt;/a&gt; .&lt;/p&gt;


	&lt;h3&gt;Annotating a Class&lt;/h3&gt;


	&lt;p&gt;An example of how to annotate a Java Class.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;@MBean(description = &amp;quot;Component to measure and clean out toxins&amp;quot;)
  public class Liver
{
  private float m_toxicity;

  @MxAttribute()
    public float getToxicity()
  {
    return m_toxicity;
  }

  @MxAttribute(description = &amp;quot;Level of toxicity&amp;quot;)
    public void setToxicity(final float toxicity)
  {
    m_toxicity = toxicity;
  }

  @MxOperation(description = &amp;quot;Clean out the toxins&amp;quot;)
    public void clean()
  {
    m_toxicity = 0;
  }
}&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;Exposing an MBean on the &amp;#8216;Server&amp;#8217;&lt;/h3&gt;


	&lt;p&gt;To expose an instance of the Liver class as a ModelMBean the user could add the following code;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;final Liver liver = new Liver();
final ObjectName liver = new ObjectName(&amp;quot;body:part=Liver&amp;quot;);
final ModelMBean mBean =
  ModelMBeanFactory.createAnnotatedModelMBean( liver );
final MBeanServer mBeanServer =
  ManagementFactory.getPlatformMBeanServer();
mBeanServer.registerMBean( mBean, objectName );&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;p&gt;To expose the object as an &lt;span class="caps"&gt;RMX&lt;/span&gt;Bean the user could use the following code;&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;final Liver liver = new Liver();
final ObjectName liver = new ObjectName(&amp;quot;body:part=Liver&amp;quot;);
final Object mBean =
  RMXBeanFactory.createAnnotatedRMXBean( liver );
final MBeanServer mBeanServer =
  ManagementFactory.getPlatformMBeanServer();
mBeanServer.registerMBean( mBean, objectName );&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;Accessing an MBean on the &amp;#8216;Client&amp;#8217;&lt;/h3&gt;


	&lt;p&gt;The user can access the MBeans using the standard mechanisms. Additionally if the user decides to use &lt;span class="caps"&gt;RMX&lt;/span&gt;Beans and has defined the management interface using a java interface then the object can be dynamically proxied. This means that the user can interact with the object as if it was a local object.&lt;/p&gt;


	&lt;p&gt;The following code example demonstrates accessing a &lt;span class="caps"&gt;RMX&lt;/span&gt;Bean as a dynamic proxy.&lt;/p&gt;


&lt;div class="typocode"&gt;&lt;pre&gt;&lt;code class="typocode_default "&gt;public interface LiverRMXBean
{
  @MxAttribute()
  float getToxicity();

  @MxAttribute(description = &amp;quot;Level of toxicity&amp;quot;)
  void setToxicity(float toxicity);

  @MxOperation(description = &amp;quot;Clean out the toxins&amp;quot;)
  void clean();
}

@MBean(description = &amp;quot;Component to measure and clean out toxins&amp;quot;, 
       interfaces = {LiverMBean.class})
  public class Liver
  implements LiverMBean
{
  private float m_toxicity;

  public float getToxicity()
  {
    return m_toxicity;
  }

  public void setToxicity(final float toxicity)
  {
    m_toxicity = toxicity;
  }

  public void clean()
  {
    m_toxicity = 0;
  }
}

...
// Registering the Liver MBean
final Liver liver = new Liver();
final ObjectName liver = new ObjectName(&amp;quot;body:part=Liver&amp;quot;);
final Object mBean =
  RMXBeanFactory.createAnnotatedRMXBean( liver );
final MBeanServer mBeanServer =
  ManagementFactory.getPlatformMBeanServer();
mBeanServer.registerMBean( mBean, objectName );
...

...
// Accessing and using the Liver MBean
final MBeanServerConnection connection = ...;
final ObjectName liver = new ObjectName(&amp;quot;body:part=Liver&amp;quot;);
final LiverRMXBean liver = (LiverRMXBean)
  RMXBeanFactory.newProxyInstance( liver );

System.out.println( &amp;quot;Current Toxicity Level: &amp;quot; + liver.getToxicity() );
liver.clean();
System.out.println( &amp;quot;Toxicity Level after cleaning: &amp;quot; + liver.getToxicity() )
...&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;

	&lt;h3&gt;Files&lt;/h3&gt;


	&lt;p&gt;To get your greasy mits on PanMX grab it from one of the following locations&lt;/p&gt;


	&lt;ul&gt;
	&lt;li&gt;&lt;strong&gt;Binary Jar&lt;/strong&gt;: &lt;a href="http://www.realityforge.org/static/PanMX/panmx-1.0.jar"&gt;http://www.realityforge.org/static/PanMX/panmx-1.0.jar&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Source Zip&lt;/strong&gt;: &lt;a href="http://www.realityforge.org/static/PanMX/panmx-1.0.zip"&gt;http://www.realityforge.org/static/PanMX/panmx-1.0.zip&lt;/a&gt;&lt;/li&gt;
		&lt;li&gt;&lt;strong&gt;Subversion&lt;/strong&gt;: svn co &lt;a href="http://www.realityforge.org/svn/code/PanMX"&gt;http://www.realityforge.org/svn/code/PanMX&lt;/a&gt;&lt;/li&gt;
	&lt;/ul&gt;</description>
      <pubDate>Mon, 27 Feb 2006 16:15:00 +1100</pubDate>
      <guid isPermaLink="false">urn:uuid:fe3425c0-2c33-4d36-997b-def023838688</guid>
      <author>Peter Donald</author>
      <link>http://www.realityforge.org/articles/2006/02/27/panmx-exposing-mbeans-the-easy-way</link>
      <category>Java</category>
      <category>java</category>
      <category>jmx</category>
      <category>mbean</category>
      <category>mxbean</category>
      <trackback:ping>http://www.realityforge.org/articles/trackback/26</trackback:ping>
    </item>
  </channel>
</rss>
