I'm trying to run two embedded Tomcat servers within the same application but I keep getting an exception that the "Tomcat" name has already been registered as an MBean. Running two Tomcat servers in two different processes works just fine. It's just when I try to run two Tomcat servers within a single process.
I have my reasons for not wanting to create two connectors in the same Tomcat server instance, in short, I'm running unit tests that need to start three servers and I want to run three separate servers because that's the way it would be running in the real world an not in a single server with multiple connectors.
I'm wondering whether there is any way to either change the name that the Tomcat server uses for the MBean registration or even just turn the whole MBean registering off completely?
For the record, below is the stack trace that I get when I start two Tomcat instances in the same process:
SEVERE: Error registering Tomcat:j2eeType=Servlet,name=gor,WebModule=//localhost/gorserver,J2EEApplication=none,J2EEServer=none
javax.management.InstanceAlreadyExistsException: Tomcat:j2eeType=Servlet,name=gor,WebModule=//localhost/gorserver,J2EEApplication=none,J2EEServer=none
at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
at org.apache.tomcat.util.modeler.Registry.registerComponent(Registry.java:742)
at org.apache.catalina.util.LifecycleMBeanBase.register(LifecycleMBeanBase.java:158)
at org.apache.catalina.util.LifecycleMBeanBase.initInternal(LifecycleMBeanBase.java:61)
at org.apache.catalina.core.ContainerBase.initInternal(ContainerBase.java:1080)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:136)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5508)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Aug 30, 2016 2:20:57 AM org.apache.catalina.util.LifecycleMBeanBase register
WARNING: Failed to register object [StandardEngine[Tomcat].StandardHost[localhost].StandardContext[/gorserver].StandardWrapper[gor]] with name [Tomcat:j2eeType=Servlet,name=gor,WebModule=//localhost/gorserver,J2EEApplication=none,J2EEServer=none] during component initialisation
javax.management.InstanceAlreadyExistsException: Tomcat:j2eeType=Servlet,name=gor,WebModule=//localhost/gorserver,J2EEApplication=none,J2EEServer=none
at com.sun.jmx.mbeanserver.Repository.addMBean(Repository.java:437)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerWithRepository(DefaultMBeanServerInterceptor.java:1898)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerDynamicMBean(DefaultMBeanServerInterceptor.java:966)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerObject(DefaultMBeanServerInterceptor.java:900)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.registerMBean(DefaultMBeanServerInterceptor.java:324)
at com.sun.jmx.mbeanserver.JmxMBeanServer.registerMBean(JmxMBeanServer.java:522)
at org.apache.tomcat.util.modeler.Registry.registerComponent(Registry.java:742)
at org.apache.catalina.util.LifecycleMBeanBase.register(LifecycleMBeanBase.java:158)
at org.apache.catalina.util.LifecycleMBeanBase.initInternal(LifecycleMBeanBase.java:61)
at org.apache.catalina.core.ContainerBase.initInternal(ContainerBase.java:1080)
at org.apache.catalina.util.LifecycleBase.init(LifecycleBase.java:102)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:136)
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5508)
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1571)
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1561)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Eventually found out that this can be fixed by giving each context that is added to a Tomcat server a unique name (for example, adding the port number of the server it's being added to):
context.setName("MyServerContext_" + httpPort);
This results in the MBeans having different names.