Adam Rabung
Adam Rabung

Reputation: 5224

Is it possible to enable JMX in embedded Tomcat 7?

I would like to access Server and Connectors from within my webapp. Furthermore, I need to launch this webapp from embedded Tomcat. Here is how I boot tomcat:

Tomcat tomcat = new Tomcat();
tomcat.setPort(port);
StandardServer server = (StandardServer) tomcat.getServer();
server.addLifecycleListener(new AprLifecycleListener());
tomcat.addWebapp("/", webapp.getAbsolutePath());
tomcat.start();
tomcat.getServer().await();

When I use the typical example for accessing the Server via JMX1:

import javax.management.*;
import org.apache.catalina.Server;

MBeanServer mBeanServer = MBeanServerFactory.findMBeanServer(null).get(0);
ObjectName name = new ObjectName("Catalina", "type", "Server");
Server server = (Server) mBeanServer.getAttribute(name, "managedResource");

I get:

Caused by: javax.management.InstanceNotFoundException: Catalina:type=Server
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getMBean(DefaultMBeanServerInterceptor.java:1094)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.getAttribute(DefaultMBeanServerInterceptor.java:662)
at com.sun.jmx.mbeanserver.JmxMBeanServer.getAttribute(JmxMBeanServer.java:638)

I assume this is because I am failing to bootstrap JMX somehow.

Upvotes: 3

Views: 2180

Answers (2)

Vinoth Sakthivadivel
Vinoth Sakthivadivel

Reputation: 11

It is possible by using the PR_JVMOPTIONS. I got it resolved by using the following configuration parameters in my embedded tomcat installation service file.

set PR_JVMOPTIONS=-Dcom.sun.management.jmxremote.authenticate=false;-Dcom.sun.management.jmxremote.ssl=false;-Dcom.sun.management.jmxremote.port=9111

Upvotes: 0

Adam Rabung
Adam Rabung

Reputation: 5224

Looks like JMX is running fine, it's the name I used for lookup that is the problem. Should have been:

ObjectName name = new ObjectName("Tomcat", "type", "Server");

instead.

Upvotes: 2

Related Questions