apache/shenyu · error · IllegalStateException

failed to get the HTTP port of the current tomcat

Error message

failed to get the HTTP port of the current tomcat

What it means

PortUtils.getPort() found exactly one Tomcat Connector MBean, but its 'protocol' attribute was neither 'HTTP/1.1' nor 'org.apache.coyote.http11.Http11NioProtocol'. That means the sole connector is not an HTTP connector (e.g. AJP), so there is no HTTP port to report and the method throws.

Solutions

  1. Ensure a standard HTTP/1.1 (NIO) connector is configured in server.xml or embedded setup
  2. Explicitly set the port in the ShenYu client config (shenyu.client.<type>.props.port) to bypass JMX discovery
  3. Check the Connector protocol attribute in server.xml; change AJP-only setups to include an HTTP connector
  4. Catch IllegalStateException and use a configured default port
Defensive patterns

Strategy: validation

Validate before calling

Set<ObjectName> names = ManagementFactory.getPlatformMBeanServer()
        .queryNames(new ObjectName("*:type=Connector,*"), null);
boolean hasHttp = names.stream().anyMatch(n -> {
    try { String p = String.valueOf(ManagementFactory.getPlatformMBeanServer().getAttribute(n, "protocol"));
          return p.equals("HTTP/1.1") || p.equals("org.apache.coyote.http11.Http11NioProtocol"); }
    catch (Exception e) { return false; }
});
if (!hasHttp) { /* configure explicit port */ }

Try / catch

try { port = PortUtils.getPort(); }
catch (IllegalStateException e) { port = configuredPort; }

Prevention

When it happens

Trigger: Calling PortUtils.getPort() when the only Connector MBean is an AJP connector or a non-standard protocol connector, so the protocol attribute check fails and the fall-through throw executes.

Common situations: Tomcat configured with only an AJP connector (mod_jk/proxy_ajp setups); custom protocol connector classes; older/newer Tomcat versions reporting an unexpected protocol string.

Understand the failure class

Background: "missing required config value" errors: why libraries refuse to start when a configuration key is empty, unset, or blank — this error's family across 48 libraries.

Related errors


AI-assisted analysis of apache/shenyu@567142e072 (2026-09-12). Data as JSON: /api/errors/c612964d3129c64d. Report an issue: GitHub.

Appendix: source

Thrown at shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/utils/PortUtils.java:108

     */
    public static Integer getPort() throws Exception {
        MBeanServer mBeanServer = ManagementFactory.getPlatformMBeanServer();
        Set<ObjectName> objectNames = mBeanServer.queryNames(new ObjectName("*:type=Connector,*"), null);
        if (CollectionUtils.isEmpty(objectNames)) {
            throw new IllegalStateException("Cannot get the names of MBeans controlled by the MBean server.");
        }
        //This method is not supported when there are multiple instances of external Tomcat
        if (objectNames.size() > 1) {
            throw new IllegalStateException("Not supported when there are multiple instances of external Tomcat.");
        }
        ObjectName objectName = objectNames.iterator().next();
        String protocol = String.valueOf(mBeanServer.getAttribute(objectName, "protocol"));
        String port = String.valueOf(mBeanServer.getAttribute(objectName, "port"));
        // The property name is HTTP1.1, org.apache.coyote.http11.Http11NioProtocol under linux
        if ("HTTP/1.1".equals(protocol) || "org.apache.coyote.http11.Http11NioProtocol".equals(protocol)) {
            return Integer.parseInt(port);
        }
        throw new IllegalStateException("failed to get the HTTP port of the current tomcat");
    }

}

View on GitHub (pinned to 567142e072)