Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Remove wrong part of r101("How do I add my own custom MBean..."). Someone confuses JVM API (calls it "Tomcat's" one) and uses wrong arguments.

...

See Getting a Heap Dump on the help pages of Eclipse Memory Analysis Tool.

How do I add my own custom MBean to monitor my application within Tomcat

...

6?

First of all, you can read this great tutorial from Christopher Blunck ( chris@wxnet.org ). I will just add my comments and improvements.

...

2. Build your custom MBean by following the Christopher Blunck's example:

ServerMBean.java :

No Format
  package org.wxnet.mbeans;

  public interface ServerMBean {
    public void setLoggingLevel(int level);
    public long getUptime();
  }

...

  1. First step is to get a reference to the Tomcat's MBeanServer with MBeanServer server = getServer();.

b. The getServer() method returns the

...

first MBean server in the list of MBean servers registered in JVM, which is the one used by Tomcat.

In my application architecture, I placed the 2 MBeans files (the interface and its implementation) in a particular package (I don't think its compulsary but definitely more aesthetic). Compile those one in a jar archive and place it in the Tomcat's library folder (/lib).

3. Build your ContextListener: According to the Tomcat's documentation, a Listener

A good question right now could be : what happens if I decide to create my own MBeanServer? The answer is very simple : nothing.
After many research in the (empty) Tomcat's documentation and on the internet, after many tests, I conclued that you can't create your custom MBean server. More precisely, you can create it but Tomcat won't keep any reference to it.
You can replace the previous getServer() method by this one to test:

No Format

private MBeanServer getServer() {
        MBeanServer mbserver = null;

        MBeanServer myMBServer = MBeanServerFactory.createMBeanServer("myMBServer");

        ArrayList<MBeanServer> mbservers = MBeanServerFactory.findMBeanServer(null);
        System.out.println("****** TOMCAT'S LIST OF REGISTERED MBEANSERVERS ********** ");
        System.out.println(mbservers);

        System.out.println("****** TRYING TO RETRIEVE MY OWN MBEANSERVER FROM ITS AgentId ********** ");
        ArrayList<MBeanServer> mbservers_2 = MBeanServerFactory.findMBeanServer("myMBServer");
        System.out.println(mbservers_2);


        if (mbservers.size() > 0) {
            mbserver = (MBeanServer) mbservers.get(0);
        }

        if (mbserver != null) {
            System.out.println("MBeanServer has been found!");
        } else {
            mbserver = MBeanServerFactory.createMBeanServer();
        }

        return mbserver;
    }

Here is a capture of the println() : https://picasaweb.google.com/lh/photo/jzVX9-NBGwF57A0m8qqv2Q?feat=directlink. Tomcat seems to register 2nd Mbean server but when I try to fetch mine from its AgentId (here "myMBServer"), nothing is found. In my opinion, Tomcat might re-implement the MBeanServerFactory java class to control the server creation. Then, it doesn't keep a reference to the newly created MBean server. Moreover, if Tomcat re-implement the MBeanFactory class, there is no method to directly add MBean (see the http://tomcat.apache.org/tomcat-6.0-doc/api/index.html).

In my application architecture, I placed the 2 MBeans files (the interface and its implementation) in a particular package (I don't think its compulsary but definitely more aesthetic). Compile those one in a jar archive and place it in the Tomcat's library folder (/lib).

3. Build your ContextListener : According to the Tomcat's documentation, a Listener is a a component that performs actions when specific events occur, usually Tomcat starting or Tomcat stopping.. We need to instantiate and load our MBean at Tomcat's start. So we build a ContextListener.java file which is placed wherever you want in your project architecture:

...

5. The configuration should be over. You should have done those operations:

  1. Build your MBean,

b. Compile it and place the .jar archive in the Tomcat's /lib folder,

c. Build your ContextListener.java,

d. Add a reference to your ContextListener inside your WEB-INF/web.xml file

...

You can try to run your project. Open the http://localhost:8080/manager/jmxproxy page and find your custom MBean (with a simple ctrl+f). You can see its domain, name, type and its attributes and methods.
You can now use this MBean in your application by getting a reference to the Tomcat's MBean server:

...