Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

See TomcatDataSourceRealms

How do I add my own custom MBean to monitor my application within Tomcat 5/6?

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

  1. Start your Tomcat and check that you have access to http://localhost:8080/manager/jmxproxy/. It means that JMX is enabled on your Tomcat configuration (if not, check if the following line is in your /conf/server.xml file :

<Listener className="org.apache.catalina.mbeans.ServerLifecycleListener" />. Otherwise, check the Tomcat documentation to activate it). Let this page opened to check further if your custom Mbean is detected by Tomcat.

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();
  }

Server.java :

No Format

package org.wxnet.mbeans;

  import javax.management.MBeanServer;
  import javax.management.MBeanServerFactory;
  import javax.management.ObjectName;

  import java.util.ArrayList;

  public class Server implements ServerMBean {
    private int _logLevel = 1;
    private long _startTime = 0L;

    public Server() {
      MBeanServer server = getServer();

      ObjectName name = null;
      try {
        name = new ObjectName("Application:Name=Server,Type=Server");
        server.registerMBean(this, name);
      } catch (Exception e) {
        e.printStackTrace();
      }

      _startTime = System.currentTimeMillis();
    }


    private MBeanServer getServer() {
      MBeanServer mbserver = null;

      ArrayList mbservers = MBeanServerFactory.findMBeanServer(null);

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

      if (mbserver != null) {
        System.out.println("Found our MBean server");
      } else {
        mbserver = MBeanServerFactory.createMBeanServer();
      } 

      return mbserver;
    }


    // interface method implementations
    public void setLoggingLevel(int level) { _logLevel = level; }
    public long getUptime() { return System.currentTimeMills() - _startTime; }
  }

In this implementation, firstly notice the ObjectName representing the MBean (in the constructor) : name = new ObjectName("Application:Name=Server,Type=Server"); Do not hesitate to change the domain name (the first parameter) by your own to easily find your MBean reference in the http://localhost:8080/manager/jmxproxy page.

Secondly, take a look at your MBean constructor :

  1. First step is to get a reference to the Tomcat's MBeanServer with MBeanServer server = getServer();.
    b. The getServer() method returns the default Tomcat's MBean server.


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.toString());
        
        System.out.println("****** TRYING TO RETRIEVE MY OWN MBEANSERVER FROM ITS AgentId ********** ");
        ArrayList<MBeanServer> mbservers_2 = MBeanServerFactory.findMBeanServer("myMBServer");
        System.out.println(mbservers_2.toString());
        
        
        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() : Tomcat's javadoc]. Tomcat seems to register 2 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|[http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html, 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 :

No Format

package '''org.bonitasoft.context''';

/**
 * @author Christophe Havard
 *
 */

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;

import org.bonitasoft.mbeans.Server;

public final class ContextListener  implements ServletContextListener {

  public void contextInitialized(ServletContextEvent event) {
    Server mbean = new Server();
  }

  public void contextDestroyed(ServletContextEvent event) { }

}

Take a look especially at the contextInitialized method. It just instantiates our custom MBean. Don't forget that the MBean register itself in the Tomcat's MBeanServer in its constructor. DO NOT FORGET to change the package line according to your application architecture.

Then, you have to modify your WEB-INF/web.xml file to make Tomcat execute your ContextListener.

No Format

<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
    "http://java.sun.com/dtd/web-app_2_3.dtd">

<web-app>
  <display-name>My Web Application</display-name>
 '''''bla bla bla...'''''
  <listener>
    <listener-class>org.bonitasoft.context.ContextListener</listener-class>
  </listener>
</web-app>

In his tutorial, Christopher Blunck suggests to compile the ContextListener.java file in a jar archive and then place it into our WEB-INF/lib folder. In my own experiments, I never found any difference without doing this.

4. The mbeans-descriptor.xml file : The only entry in the Tomcat documentation about custom MBean is about this file. It says "You may also add MBean descriptions for custom components in a mbeans-descriptor.xml file, located in the same package as the class files it describes.". Unfortunately, instead of reading this file, Tomcat applied its own templates to replace my MBeans attributes and operations descriptions... I really didn't figure out what is the correct way of using and placing this file. So I don't use it.

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 :

No Format

MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); 
//call operations with invoke(...) and attributes with getAttributes(...)

Do not hesitate to check the ManagementFactory class javadoc.

...

CategoryFAQ