{"record":{"id":"6ce39132ac56d98c","repo":"apache/shenyu","slug":"not-supported-when-there-are-multiple-instances-of-external","errorCode":null,"errorMessage":"Not supported when there are multiple instances of external Tomcat.","messagePattern":"Not supported when there are multiple instances of external Tomcat\\.","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":99,"sourceCode":"        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":81,"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#L81-L112","documentation":"PortUtils.getPort() discovers the embedded/external Tomcat HTTP connector port via JMX MBeans. The MBean query '*:type=Connector,*' returned more than one Connector object name, meaning multiple Tomcat instances (or connectors) are running in this JVM, so there is no single unambiguous port to return. The utility deliberately refuses to guess.","triggerScenarios":"Calling PortUtils.getPort() when the MBean server reports multiple 'Connector' MBeans — e.g. several external Tomcat instances in the same JVM, or multiple connectors configured in server.xml (HTTP + AJP, HTTPS + HTTP).","commonSituations":"Running several embedded Tomcat instances in one application; a server.xml with AJP or additional HTTPS connectors alongside the HTTP connector; deploying the ShenYu client into a shared Tomcat hosting multiple webapps with separate connectors.","solutions":["Remove or disable extra Tomcat connectors so exactly one HTTP Connector remains","Configure the port explicitly in the client config (shenyu.client.<type>.props.port) instead of relying on auto-discovery","Ensure only one Tomcat instance runs in the JVM; start others in separate processes","Catch IllegalStateException and fall back to an explicitly configured port"],"exampleFix":"// before\nint port = PortUtils.getPort();\n// after\nint port = props.containsKey(\"port\")\n        ? Integer.parseInt(props.getProperty(\"port\"))\n        : PortUtils.getPort();","handlingStrategy":"try-catch","validationCode":"int connectorCount = ManagementFactory.getPlatformMBeanServer()\n        .queryNames(new ObjectName(\"*:type=Connector,*\"), null).size();\nif (connectorCount > 1) { /* use explicit port from config */ }","typeGuard":null,"tryCatchPattern":"int port;\ntry { port = PortUtils.getPort(); }\ncatch (IllegalStateException e) { port = Integer.parseInt(props.getProperty(\"port\")); }","preventionTips":["Keep exactly one Tomcat connector per JVM","Always provide an explicit port in client config as fallback","Avoid AJP connectors when using auto port discovery"],"tags":["tomcat","jmx","port-discovery","configuration"],"backgroundTag":"unsupported-operation","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"}