Well... I managed to get all of it working, following is what needs to be
done to get a custom MBean working

HelloMBean.java
---------------------------------
package org.example.mbeans;

public interface HelloMBean {
public void sayHello();
}

Hello.java
-----------------------------------
package org.example.mbeans;

public class Hello implements HelloMBean {

 public void sayHello() {
System.out.println("Saying Hello !!!");
}

}

mbeans-descriptors.xml
------------------------------------
<?xml version="1.0"?>
<mbeans-descriptors>
<mbean name="Hello"
  className="org.apache.tomcat.util.modeler.BaseModelMBean"
          description="Custom Hello"
          domain="Catalina"
          type="org.example.mbeans.Hello">
         <operation name="sayHello"
                    description="Saying Hello"
                    impact="ACTION" returnType="void">
          </operation>
</mbean>
</mbeans-descriptors>

Note:- All of the above should be in same package

Finally a class which loads and registers this MBean in Registry. In this
case i used a ServletContainerInitializer

package com.mycompany.mypackage;

import java.lang.management.ManagementFactory;
import java.net.URL;
import java.util.Set;

import javax.servlet.ServletContainerInitializer;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;

import org.apache.tomcat.util.modeler.ManagedBean;
import org.apache.tomcat.util.modeler.Registry;
import org.example.mbeans.Hello;


public class MBeansInitializer implements ServletContainerInitializer {

@Override
public void onStartup(Set<Class<?>> arg0, ServletContext arg1)
throws ServletException {
    URL url=
this.getClass().getResource("/org/example/mbeans/mbeans-descriptors.xml");
    Registry registry =
Registry.getRegistry(Registry.class.getClassLoader(),null);
    try {
    registry.loadMetadata(url);
     Hello managedObject = new Hello();
String oname="Catalina:type=Hello";
   registry.registerComponent( managedObject, oname,
"org.example.mbeans.Hello" );
} catch (Exception e) {
e.printStackTrace(System.err);
}
}
}

Thank you!

Regards,
-Yogesh


On Wed, Aug 6, 2014 at 9:34 PM, Yogesh Rao <yog...@gmail.com> wrote:

> Hi,
>
> Did some further search on this and came across the API document for
> modeler where an excellent HowTo is given...The only code missing further
> to what i had written was to bind the MBean with the registry which is done
> using below fragment
>
>     URL url= this.getClass().getResource
>       ("/com/example/mbean/mbeans-descriptors.xml");
>     Registry registry = Registry.getRegistry(null,null);
>     registry.loadMetadata(url);
>
>
> The detailed information with steps can be found @ 
> https://tomcat.apache.org/tomcat-7.0-doc/api/org/apache/tomcat/util/modeler/package-summary.html
>  (The Registry API needs to be updated in the document as it still refers to 
> a no arg method)
>
>
> Now my I am able to register my MBeans and retrieve them. Programatically i 
> see that the custom MBean is registered with the PlatformMBeanServer.
>
>
> However the MBeans still do not show up on JConsole but accessible in the 
> Registry.. Any ideas why ?
>
> Any pointers would be really helpful.
>
> Thank you!
>
> Regards,
>
> -Yogesh
>
>
>
> On Wed, Aug 6, 2014 at 6:19 AM, Yogesh Rao <yog...@gmail.com> wrote:
>
>> Hi chris,
>>
>> Thank you for the quick reply. I will certainly look at the link
>> mentioned by you... Also let me know if you planning to upload the source
>> code anytime soon.
>>
>> Meanwhile any tomcat developers on the mailing list can help me with this
>> ???
>>
>> I can slso volunteer in contributing to this part of the documentation.
>>
>> Regards,
>> -yogesh
>>
>> On Wednesday, August 6, 2014, Christopher Schultz <
>> ch...@christopherschultz.net> wrote:
>>
>>> -----BEGIN PGP SIGNED MESSAGE-----
>>> Hash: SHA256
>>>
>>> Yogesh,
>>>
>>> On 8/5/14, 10:18 AM, Yogesh Rao wrote:
>>> > I am trying to write a MBean using the Modeler API in tomcat. The
>>> > details of my environment are as below:-
>>> >
>>> > Tomcat version : 7.0.54 JDK version : 1.7 OS : windows 7
>>> >
>>> > The MBean written using Modeler API is not getting deployed and
>>> > hence not showing up on JConsole. Following is the simple MBean
>>> > code which has been written with its mbeans-descriptors.xml
>>> >
>>> > HelloMBean.java -------------------------
>>> >
>>> > package com.example.mbean;
>>> >
>>> > public interface HelloMBean {
>>> >
>>> > void sayHello(); }
>>> >
>>> >
>>> > Hello.java --------------------------- package com.example.mbean;
>>> >
>>> > import javax.management.MBeanException; import
>>> > javax.management.RuntimeOperationsException;
>>> >
>>> > import org.apache.catalina.mbeans.MBeanUtils; import
>>> > org.apache.tomcat.util.modeler.BaseModelMBean; import
>>> > org.apache.tomcat.util.modeler.ManagedBean; import
>>> > org.apache.tomcat.util.modeler.Registry;
>>> >
>>> > public class Hello extends BaseModelMBean implements HelloMBean  {
>>> >
>>> >
>>> > protected Registry registry = MBeanUtils.createRegistry();
>>> > protected ManagedBean managed =
>>> > registry.findManagedBean("Custom");
>>> >
>>> > protected Hello() throws MBeanException, RuntimeOperationsException
>>> > { super(); }
>>> >
>>> > @Override public void sayHello() {
>>> > System.out.println("sayHello()"); }
>>> >
>>> > }
>>> >
>>> > mbeans-descriptors.xml ------------------------------ <?xml
>>> > version="1.0"?> <mbeans-descriptors> <mbean name="Custom"
>>> > className="com.example.mbean.Hello" description="Custom Hello"
>>> > domain="Catalina" group="Custom"
>>> > type="com.example.mbean.HelloMBean">
>>> >
>>> > <operation name="sayHello" description="Saying Hello"
>>> > impact="ACTION" returnType="void"> </operation> </mbean>
>>> > </mbeans-descriptors>
>>> >
>>> >
>>> > All the above files are in the same package and bundled as jar. I
>>> > have tried placing this jar in server\lib directory as well as in a
>>> > webapp.
>>> >
>>> > Sadly there is very little documentation on how to go about writing
>>> > a MBean using this API.
>>> >
>>> > Any help on this would be greatly appreciated.
>>>
>>> I never had any luck getting Tomcat to auto-load my beans: I had to
>>> load them myself by manually-registering the bean with the local server.
>>>
>>> Take a look at my presentation from ApacheCon NA 2014:
>>>
>>> http://people.apache.org/~schultz/ApacheCon%20NA%202014/Monitoring%20Apache%20Tomcat%20with%20JMX.pdf
>>>
>>> Check page 28 for a slide containing code where I manually-register
>>> the bean. Note that I don't use any Tomcat-specific APIs.
>>>
>>> I keep forgetting that I never posted the full source code, etc. for
>>> my presentation. I really need to do that...
>>>
>>> - -chris
>>> -----BEGIN PGP SIGNATURE-----
>>> Version: GnuPG v1
>>> Comment: GPGTools - http://gpgtools.org
>>> Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/
>>>
>>> iQIcBAEBCAAGBQJT4XalAAoJEBzwKT+lPKRYIt0QAKxJ1mt1tkylY9HUkGGKcpaa
>>> kHXauvdCtC0RFqG/Gqrm73CO/f1//nrBqMr1n/HU8zK+6a1ygA0+u1tNtiU0nLBl
>>> sXop34Ks/3Y1lKc7Nht5GcpB4+0CGTIyXwfHqdCMc9wQLaxpuSoS7VTsOWSpaLSZ
>>> PY9IMdiFL0cGv301gsfDI7xn7pGzDSfGvCoakM+tGMMYywTidJNNxR1OyEZ070Wu
>>> 2AH3sydY2Xbbc/9Mlx0FRVjz7U+nQw/KDd3RZkyi/3EFCNt6Nh4zOwXQyY6Tm3Ik
>>> +Mjff+5t1ggz7UM0sUFvbQqc+HaP7y45UWstWEKMsbHVX/KJIDIeLKJq6A44SV2i
>>> og/JLrtOjLneu5D388hfX7CVwXaThXNuDQdyISXBai8i33YXGBwRUS9bLDAXOFRi
>>> u1vroin2B2FTVNX1zkQNt+BkkuJCNrRfxmk5FSGsr3bWaNO4udrqqQFeImcj7msg
>>> GDzs7vGmI3w599/tudo0o63G2DzquTwrIPq3BtbSGRn/SSYA8uEqJPjnWmpTBXjg
>>> IUtMzW0VyFyJIuS9IjzfpwClNm2MOCehLsfZDeozsGDEl+Wku9PUUg1YW9tmbP/I
>>> ai6so+eyChdD1XewZNIIbb8P8hSsAs6qfp3zlZrCA4gNkcvAbJahTsz/t3SkoBTn
>>> xlY4+lKQnMoXgrzmRRrI
>>> =O7gF
>>> -----END PGP SIGNATURE-----
>>>
>>> ---------------------------------------------------------------------
>>> To unsubscribe, e-mail: users-unsubscr...@tomcat.apache.org
>>> For additional commands, e-mail: users-h...@tomcat.apache.org
>>>
>>>
>

Reply via email to