如何查询JBoss Wildfly 的活跃会话数?

How to query the number of active sessions with JBoss Wildfly?

我们曾经使用以下代码查询活动会话数,最多 JBoss 7:

ObjectName name = new ObjectName(jboss.web:type=Manager,path=/MyWebApp,host=default-host);
MBeanServer jboss = MBeanServerLocator.locateJBoss();
this.sessions = new Long((Integer) jboss.getAttribute(name, "activeSessions"));

这不再适用于 JBoss Wildfly。我真的找不到合适的文档来访问当前版本的 JBoss AS 的相同信息。

我遇到了一些像 http://blog.akquinet.de/2014/09/15/monitoring-the-jboss-eap-wildfly-application-server-with-the-command-line-interface-cli/ 这样的链接,这意味着该信息的位置完全不同 – 但我不知道如何通过 Java 代码访问它。

/deployment=example.ear/subdeployment=example-web.war/subsystem=under

JBoss 7 中对 -Dorg.apache.tomcat.util.ENABLE_MODELER=true 的旧引用也无济于事。我通过 MBean "jboss.web:type=Manager,path=/,host=localhost" not found 提出的旧问题不再有效。

假设您正在部署一个 WAR 文件; ObjectName 应该类似于 jboss.as:deployment=YourWAR.war,subsystem=undertow.

请注意,对于 WildFly - 管理本机端口是 9990 而不是 9999

如果您使用的是maven,请添加以下依赖项。

    <dependency>
        <groupId>org.jboss.remotingjmx</groupId>
        <artifactId>remoting-jmx</artifactId>
        <version>2.0.0.Final</version>
    </dependency>

示例:

public static void main(String[] args) throws Exception {

    ObjectName mBeanName = new ObjectName("jboss.as:deployment=wildfly-helloworld-rs.war,subsystem=undertow");

    String host = "localhost";
    int port = 9990;  // management-native port

    String urlString = System.getProperty("jmx.service.url", "service:jmx:http-remoting-jmx://" + host + ":" + port);
    JMXServiceURL serviceURL = new JMXServiceURL(urlString);
    JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
    MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

    System.out.println("Value via JMX: activeSessions: " + connection.getAttribute(mBeanName, "activeSessions"));
    System.out.println("Value via JMX: contextRoot: " + connection.getAttribute(mBeanName, "contextRoot"));
}

如果您的 WAR 文件捆绑在 EAR 文件中;那么 ObjectName 应该类似于 jboss.as:deployment=YourEAR.ear,subdeployment=YourWAR.war,subsystem=undertow"

有关详细信息,请参阅 https://docs.jboss.org/author/display/WFLY8/JMX+subsystem+configuration