如何以编程方式在 java 网络应用程序中获取 tomcat 连接器属性

How to get tomcat connector attributes in a java web app programatically

我们在 tomcat 中的 server.xml 中有这样的连接器:

<Connector port="8080" protocol="HTTP/1.1" connectionTimeout="20000" 
           redirectPort="8443" maxConnections="500" maxThreads="150"/>

据我所知,我猜这些连接器是在 tomcat 启动时作为对象加载的。

那么我们能否在我的 java Web 应用程序中获得像 portmaxConnectionsconnectionTimeout 这样的属性,在 tomcat 中是 运行 =] 作为使用任何库的对象 ?

要是能在这里点点火花就好了

您可以通过MBeanServer获取服务器及其配置。

Interface MBeanServer: This is the interface for MBean manipulation on the agent side. It contains the methods necessary for the creation, registration, and deletion of MBeans as well as the access methods for registered MBeans. This is the core component of the JMX infrastructure.

您可以使用以下代码:

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