I've downloaded the JMXMP extensions并将它们安装在Tomcat的lib目录中。现在,我该如何使用它们,即让Tomcat接受JMXMP连接?

  • Oracle的示例显示了如何使用代码来实现此目的,为此我必须编写自己的侦听器,而我宁愿将其作为最后的选择。
  • Tomcat's JMX listener似乎不包括JMXMP支持。
  • 最佳答案

    好吧,我编写了自己的JMXMP Tomcat侦听器。随时使用:

    package webersg.tomcat;
    
    import java.lang.management.ManagementFactory;
    
    import javax.management.remote.JMXConnectorServer;
    import javax.management.remote.JMXConnectorServerFactory;
    import javax.management.remote.JMXServiceURL;
    
    import org.apache.catalina.Lifecycle;
    import org.apache.catalina.LifecycleEvent;
    import org.apache.catalina.LifecycleListener;
    
    public class JMXMPLifecycleListener implements LifecycleListener {
    
        private int port = 5555;
    
        private JMXConnectorServer cs;
    
        @Override
        public void lifecycleEvent(LifecycleEvent event) {
    
            try {
    
                // START
                if (Lifecycle.START_EVENT == event.getType()) {
    
                    System.out.println("Start JMXMP on port " + port);
    
                    cs = JMXConnectorServerFactory.newJMXConnectorServer(
                            new JMXServiceURL("jmxmp", "0.0.0.0", port),
                            null,
                            ManagementFactory.getPlatformMBeanServer()
                    );
                    cs.start();
    
                    System.out.println("Started JMXMP");
    
                }
    
                // STOP
                else if (Lifecycle.STOP_EVENT == event.getType()) {
    
                    System.out.println("Stop JMXMP");
    
                    cs.stop();
                }
    
            } catch (Exception e) {
                throw new RuntimeException(e);
            }
        }
    
    }
    

    因此,经过一天的努力,我现在可以在应用程序上使用VisualVM。

    08-05 20:35