Dear Wiki user, You have subscribed to a wiki page or wiki category on "Tomcat Wiki" for change notification.
The "HowTo" page has been changed by KonstantinKolinko: http://wiki.apache.org/tomcat/HowTo?action=diff&rev1=134&rev2=135 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. == How do I obtain a heap dump? == See [[http://wiki.eclipse.org/index.php/MemoryAnalyzer#Getting_a_Heap_Dump|Getting a Heap Dump]] on the help pages of [[http://eclipse.org/mat/|Eclipse Memory Analysis Tool]]. - == How do I add my own custom MBean to monitor my application within Tomcat 5/6? == + == How do I add my own custom MBean to monitor my application within Tomcat 6? == First of all, you can read [[http://oss.wxnet.org/mbeans.html|this great tutorial]] from Christopher Blunck ( ch...@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.<<BR>> + 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.<<BR>> - 2. Build your custom MBean by following the Christopher Blunck's example : <<BR>> '''ServerMBean.java''' : + 2. Build your custom MBean by following the Christopher Blunck's example: + + '''ServerMBean.java''' : {{{ package org.wxnet.mbeans; @@ -882, +884 @@ public long getUptime(); } }}} + '''Server.java''' : {{{ @@ -936, +939 @@ 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.<<BR>> Secondly, take a look at your MBean constructor : <<BR>> + 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.<<BR>> Secondly, take a look at your MBean constructor: - a. First step is to get a reference to the Tomcat's MBeanServer with ''MBeanServer server = getServer();''.<<BR>> b. The ''getServer()'' method returns the default Tomcat's MBean server.<<BR>> + a. First step is to get a reference to the Tomcat's MBeanServer with ''MBeanServer server = getServer();''.<<BR>> + 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. - A good question right now could be : what happens if I decide to create my own MBeanServer? The answer is very simple : '''nothing'''.<<BR>> 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.<<BR>> You can replace the previous ''getServer()'' method by this one to test: - - {{{ - 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).<<BR>> - - 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).<<BR>> + 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 [[http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html|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 : + 3. Build your '''!ContextListener''': According to the [[http://tomcat.apache.org/tomcat-6.0-doc/config/listeners.html|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: {{{ package '''org.bonitasoft.context'''; @@ -1021, +993 @@ }}} 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.<<BR>> - 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. + 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 : + 5. The configuration should be over. You should have done those operations: - a. Build your MBean,<<BR>> b. Compile it and place the .jar archive in the Tomcat's /lib folder,<<BR>> c. Build your !ContextListener.java,<<BR>> d. Add a reference to your !ContextListener inside your WEB-INF/web.xml file,<<BR>> + a. Build your MBean,<<BR>> + b. Compile it and place the .jar archive in the Tomcat's /lib folder,<<BR>> + c. Build your !ContextListener.java,<<BR>> + 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.<<BR>>You can now use this MBean in your application by getting a reference to the Tomcat's MBean server : + 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.<<BR>>You can now use this MBean in your application by getting a reference to the Tomcat's MBean server: {{{ MBeanServer mbs = ManagementFactory.getPlatformMBeanServer(); --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscr...@tomcat.apache.org For additional commands, e-mail: dev-h...@tomcat.apache.org