0

我发现了这个使用 JMX 和 Java 进行 JBoss 监控的简单示例

public class JMXExample {

    public static void main(String[] args) throws Exception {
        //Get a connection to the JBoss AS MBean server on localhost
        String host = "localhost";
        int port = 9999;  // management-native port
        String urlString =
            System.getProperty("jmx.service.url","service:jmx:remoting-jmx://" + host + ":" + port);
        JMXServiceURL serviceURL = new JMXServiceURL(urlString);
        JMXConnector jmxConnector = JMXConnectorFactory.connect(serviceURL, null);
        MBeanServerConnection connection = jmxConnector.getMBeanServerConnection();

        //Invoke on the JBoss AS MBean server
        int count = connection.getMBeanCount();
        System.out.println(count);
        jmxConnector.close();
    }
}

我想每 3 秒调用一次此代码以获取实时性能数据。

有没有办法打开一个到服务器的连接并发送频繁的请求?

4

1 回答 1

0

如果将此代码部署为 EJB,则可以将其设置为 ,并@Singleton @Startup在方法中设置连接@PostConstruct,同时定期收集指标,根据@Schedule. 例如:

@Singleton
@Startup
public class MetricsGathering {
    @PostConstruct
    public void init() {
        // setup the connection
    }

    @Schedule(second="*/5")
    public void collect() {
        // get the data and do something with it
    }
}
于 2015-12-14T16:04:17.527 回答