{"record":{"id":"dbd6f85a7c77a5a3","repo":"apache/shenyu","slug":"cannot-get-the-names-of-mbeans-controlled-by-the-mbean","errorCode":null,"errorMessage":"Cannot get the names of MBeans controlled by the MBean server.","messagePattern":"Cannot get the names of MBeans controlled by the MBean server\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/utils/PortUtils.java","lineNumber":95,"sourceCode":"            NoSuchMethodException, InvocationTargetException, IllegalAccessException {\n        final Class<?> clazz = Class.forName(className);\n        final Method method = clazz.getMethod(\"getPort\");\n        final Object bean = beanFactory.getBean(clazz);\n        return (int) method.invoke(bean);\n    }\n\n    /**\n     * get the current tomcat port number.\n     * Note: This method is not supported when there are multiple instances of external Tomcat.\n     *\n     * @return tomcat port number\n     * @throws Exception when failed to get port\n     */\n    public static Integer getPort() throws Exception {\n        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();\n        Set<ObjectName> objectNames = mBeanServer.queryNames(new ObjectName(\"*:type=Connector,*\"), null);\n        if (CollectionUtils.isEmpty(objectNames)) {\n            throw new IllegalStateException(\"Cannot get the names of MBeans controlled by the MBean server.\");\n        }\n        //This method is not supported when there are multiple instances of external Tomcat\n        if (objectNames.size() > 1) {\n            throw new IllegalStateException(\"Not supported when there are multiple instances of external Tomcat.\");\n        }\n        ObjectName objectName = objectNames.iterator().next();\n        String protocol = String.valueOf(mBeanServer.getAttribute(objectName, \"protocol\"));\n        String port = String.valueOf(mBeanServer.getAttribute(objectName, \"port\"));\n        // The property name is HTTP1.1, org.apache.coyote.http11.Http11NioProtocol under linux\n        if (\"HTTP/1.1\".equals(protocol) || \"org.apache.coyote.http11.Http11NioProtocol\".equals(protocol)) {\n            return Integer.parseInt(port);\n        }\n        throw new IllegalStateException(\"failed to get the HTTP port of the current tomcat\");\n    }\n\n}\n","sourceCodeStart":77,"sourceCodeEnd":112,"githubUrl":"https://github.com/apache/shenyu/blob/567142e07261b3e615ae8850b30f4421f455cc5d/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/utils/PortUtils.java#L77-L112","documentation":"PortUtils.getPort uses the platform MBeanServer to find Tomcat Connector MBeans (`*:type=Connector,*`) and read the port. If the query returns an empty set — no external Tomcat is running in this JVM — it throws IllegalStateException before reading the connector.","triggerScenarios":"getPort is called in a JVM without an external Tomcat (embedded server, non-Tomcat container, or plain unit test), so queryNames finds zero Connector MBeans.","commonSituations":"Spring Boot app with embedded Tomcat (MBeans named differently/not registered as expected), Jetty/Undertow deployments, tests calling findPort without any server; also related failure when multiple Tomcat instances run in one JVM (the next check throws).","solutions":["Use this path only with a standalone/external Tomcat; for embedded Spring Boot rely on the `local.server.port` environment strategy instead.","Configure the port explicitly in shenyu client props so MBean lookup is never used.","If you expect Tomcat, verify the JVM actually hosts it and JMX MBeans are registered (no custom MBeanServer filtering).","Ensure exactly one external Tomcat instance per JVM; multiple connectors also fail detection."],"exampleFix":"// before\nInteger port = PortUtils.findPort(); // MBean path fails in embedded apps\n// after\nInteger port = environment.containsProperty(\"local.server.port\")\n        ? environment.getProperty(\"local.server.port\", Integer.class)\n        : PortUtils.findPort();","handlingStrategy":"try-catch","validationCode":"boolean hasTomcatConnector = !ManagementFactory.getPlatformMBeanServer()\n    .queryNames(ObjectName.getInstance(\"*:type=Connector,*\"), null).isEmpty();","typeGuard":null,"tryCatchPattern":"try {\n    port = PortUtils.getPort();\n} catch (IllegalStateException e) {\n    // no (or multiple) Tomcat Connector MBeans — fall back to config\n    port = Integer.getInteger(\"server.port\", 8080);\n}","preventionTips":["Use PortUtils.getPort only for external-Tomcat deployments.","Prefer Spring's `local.server.port` property for embedded servers.","Ensure a single Tomcat instance per JVM."],"tags":["jmx","tomcat","port","detection"],"backgroundTag":"empty-result-set","analyzedSha":"567142e07261b3e615ae8850b30f4421f455cc5d","analyzedAt":"2026-09-12T10:08:21.293Z","contentChangedAt":"2026-09-12T10:08:21.293Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}