How should I monitor a web application on tomcat u

2019-04-02 01:39发布

问题:

I would like to monitor web applications running under tomcat using JMX.

I don't want to just use the built in JMX implementation of Tomcat, I want to implement an mbean for the actual Web Application so I can get information on application-specific settings and monitor it.

The problem with web applications and online monitoring is that web applications are not always active but are "awaken" to handle requests by the server so monitoring them is not just plugging in with JMX as I would for a normal running process.

How do you make Tomcat run applications in the background (Like a Singleton) so I can connect to it at all time?

Is there a way to do this that is common and I am not aware of?

Thanks!

回答1:

You can create a class that implements ServletContextListener and then load that listener in your web.xml.

The class:

public class ServerListener implements ServletContextListener {

    public void contextDestroyed(ServletContextEvent pSce) {
    }

    public void contextInitialized(ServletContextEvent pSce) {
        // TODO Register MBean here.
    }
}

The web.xml:

<listener>
  <listener-class>com.example.ServerListener</listener-class>
</listener>


回答2:

In your app you need to register the MBean with the MBean server when the application is deployed. While the web application is deployed, the MBean will be exposed. I have used the Spring Framework JMX support to do this within Tomcat - but there are are ways to do this without Spring.



回答3:

if you are familiar with Nagios and your company is using it, that may be a better choice

These plugins do look helpful https://exchange.nagios.org/directory/Plugins/Java-Applications-and-Servers/Apache-Tomcat

Else as said by @teabot, use Spring JMX support. Makes it very easy.



标签: tomcat jmx